-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI-python-code.py
More file actions
64 lines (51 loc) · 1.94 KB
/
Copy pathAPI-python-code.py
File metadata and controls
64 lines (51 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#pip install spotipy
#pip install python-dotenv
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import os
import matplotlib.pyplot as plt
import pandas as pd
from dotenv import load_dotenv
import numpy as np
############# loading the .env file variables################
load_dotenv()
client_id = os.getenv("SPOTIPY_CLIENT_ID")
client_secret = os.getenv("SPOTIPY_CLIENT_SECRET")
#################### Creating the Spotify client ####################
con = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=client_id, client_secret=client_secret))
################### Making the API request #######################
#Getting artist ID
fatima_id='6jZSXmTCxZhFfYELtp78Ci'
#importing songs
output=con.artist_top_tracks(fatima_id)
songs=[]
if output:
for track in output['tracks']:
tracks = {}
tracks['name']=track['name']
tracks['popularity']=track['popularity']
tracks['duration']=track['duration_ms']/60000
songs.append(tracks)
############### Creating DataFrame #############
songs_df=pd.DataFrame(songs)
songs_df
############# Plotting scatter plot ###########
# Create the trendline
coefficients = np.polyfit(songs_df['duration'], songs_df['popularity'], 1) # Fit a first-degree (linear) polynomial
trendline = np.poly1d(coefficients)
y_fit = trendline(songs_df['duration'])
#Creating plot
plt.scatter(songs_df['duration'], songs_df['popularity'])
plt.plot(songs_df['duration'], y_fit, linestyle='--', color='red')
plt.title('Relationship between duration and popularity')
plt.xlabel('Song duration in minutes')
plt.ylabel('Popularity ranking')
plt.show()
print("""
Conclusion: There is a possitive association between
duration and popularity, based on the trendline.
However, given the few data points available, it is not possible to
determine whether this association is statistically significant.
Even if this association was statistically significant, correlation
is not causation.
""")