-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotipy_api.py
234 lines (199 loc) · 9.39 KB
/
spotipy_api.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import json
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials # For Spotify API use
import sqlite3
username = 'Emiton Alves'
client_id = os.environ['SPOTIFY_CLIENT_ID']
client_secret = os.environ['SPOTIFY_CLIENT_SECRET']
client_credentials_manager = SpotifyClientCredentials(
client_id=client_id,
client_secret=client_secret
)
all_playlists = {}
# Might need redirect if trouble hosting locally
# redirect_uri = 'http://localhost/5000'
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
connection = sqlite3.connect('spotify_analysis_db')
cursor = connection.cursor()
query_string = (
"CREATE TABLE IF NOT EXISTS spotify_songs ("
"track_uri TEXT NOT NULL, "
"name TEXT, "
"genre TEXT, "
"playlist TEXT, "
"duration_ms INTEGER, "
"key INTEGER, "
"mode INTEGER, "
"time_signature INTEGER, "
"acousticness REAL, "
"danceability REAL, "
"energy REAL, "
"instrumentalness REAL, "
"liveness REAL, "
"loudness REAL, "
"speechiness REAL, "
"tempo REAL, "
"valence REAL, "
" PRIMARY KEY (track_uri))"
)
cursor.execute(query_string)
def main():
with open('playlists_to_work_with.json') as json_file:
playlists_to_work_with = json.load(json_file)
for genre in playlists_to_work_with:
for plist in playlists_to_work_with[genre]:
get_playlist_audio_features(plist[0], plist[1], genre)
cursor.close()
connection.close()
# manually inspect all of the values to determine whether the median or mean is a better metric to plot
for playlist1 in all_playlists:
print("–" * 70)
print(playlist1)
for feature in all_playlists[playlist1]:
if feature != 'name' and feature != 'track uri' and feature != 'playlist' and feature != 'genre' and feature != 'track uri':
print(feature.upper(),
"| median:", np.median(all_playlists[playlist1][feature]),
"| mean:", np.mean(all_playlists[playlist1][feature]))
labels = ['acousticness', 'danceability', 'energy', 'valence', 'instrumentalness', 'tempo', 'speechiness']
num_vars = len(labels)
# Split the circle into even parts and save the angles so we know where to put each axis.
angles = np.linspace(0, 2 * np.pi, num_vars, endpoint=False).tolist()
angles += angles[:1]
# ax = plt.subplot(polar=True)
fig, ax = plt.subplots(figsize=(6, 6), subplot_kw=dict(polar=True))
# Helper function to plot each playlist on the radar chart.
def add_to_radar(playlist, color):
values = [np.median(all_playlists[playlist]['acousticness']), np.median(all_playlists[playlist]['danceability']),
np.median(all_playlists[playlist]['energy']),
np.median(all_playlists[playlist]['valence']), np.mean(all_playlists[playlist]['instrumentalness']),
np.median(all_playlists[playlist]['tempo']),
np.median(all_playlists[playlist]['speechiness'])]
# tempo values typically range from 50-220 --> squash range to 0-1
values[-2] = values[-2] / 220
# speechiness values values are highly concentrated between 0 and 0.25-ish --> expand range to 0-1
values[-1] = values[-1] * 4
values += values[:1]
ax.plot(angles, values, color=color, linewidth=1, label=playlist)
ax.fill(angles, values, color=color, alpha=0.25)
# Add each additional playlist to the chart.
add_to_radar('RapCaviar', 'red')
add_to_radar('Hot Country', 'green')
add_to_radar('Morning Classical', 'blue')
# polar coordinates math for radar graph
ax.set_theta_offset(np.pi / 2)
ax.set_theta_direction(-1)
# Draw axis lines for each angle and label.
ax.set_thetagrids(np.degrees(angles), labels)
# Go through labels and adjust alignment based on where it is in the circle.
for label, angle in zip(ax.get_xticklabels(), angles):
if angle in (0, np.pi):
label.set_horizontalalignment('center')
elif 0 < angle < np.pi:
label.set_horizontalalignment('left')
else:
label.set_horizontalalignment('right')
# Set position of y-labels (0-100) to be in the middle of the first two axes.
ax.set_ylim(0, 1)
ax.set_rlabel_position(180 / num_vars)
# Add some custom styling.
ax.tick_params(colors='#222222') # color of tick labels
ax.tick_params(axis='y', labelsize=8) # y-axis labels
ax.grid(color='#AAAAAA') # color of circular gridlines
ax.spines['polar'].set_color('#222222') # color of outermost gridline (spine)
ax.set_facecolor('#FAFAFA') # background color inside the circle itself
# Give the chart a title and a legend
ax.set_title('Playlist Comparison', y=1.08)
ax.legend(loc='upper right', bbox_to_anchor=(1.1, 1.1))
fig.savefig('playlist_comp.png')
plt.show()
def get_playlist_audio_features(playlist, uri, genre):
playlist_id = uri.split(':')[2]
results = sp.user_playlist(user=username, playlist_id=playlist_id)
playlist_name = results['name']
all_playlists[playlist_name] = {}
all_playlists[playlist_name]['track uri'] = []
all_playlists[playlist_name]['name'] = []
all_playlists[playlist_name]['genre'] = []
all_playlists[playlist_name]['playlist'] = []
all_playlists[playlist_name]['duration_ms'] = []
all_playlists[playlist_name]['key'] = []
all_playlists[playlist_name]['mode'] = []
all_playlists[playlist_name]['time_signature'] = []
all_playlists[playlist_name]['acousticness'] = []
all_playlists[playlist_name]['danceability'] = []
all_playlists[playlist_name]['energy'] = []
all_playlists[playlist_name]['instrumentalness'] = []
all_playlists[playlist_name]['liveness'] = []
all_playlists[playlist_name]['loudness'] = []
all_playlists[playlist_name]['speechiness'] = []
all_playlists[playlist_name]['tempo'] = []
all_playlists[playlist_name]['valence'] = []
for track_metadata in results['tracks']['items']:
# DEBUG STATEMENT
# print(json.dumps(track, indent=4))
if track_metadata['track'] is not None:
# save metadata
name = track_metadata['track']['name']
print(name)
track_uri = track_metadata['track']['uri']
all_playlists[playlist_name]['track uri'].append(track_uri)
all_playlists[playlist_name]['name'].append(name)
all_playlists[playlist_name]['genre'].append(genre)
all_playlists[playlist_name]['playlist'].append(playlist)
# extract features
features = sp.audio_features(track_uri)
if features != [None]:
all_playlists[playlist_name]['duration_ms'].append(features[0]['duration_ms'])
all_playlists[playlist_name]['key'].append(features[0]['key'])
all_playlists[playlist_name]['mode'].append(features[0]['mode'])
all_playlists[playlist_name]['time_signature'].append(features[0]['time_signature'])
all_playlists[playlist_name]['acousticness'].append(features[0]['acousticness'])
all_playlists[playlist_name]['danceability'].append(features[0]['danceability'])
all_playlists[playlist_name]['energy'].append(features[0]['energy'])
all_playlists[playlist_name]['instrumentalness'].append(features[0]['instrumentalness'])
all_playlists[playlist_name]['liveness'].append(features[0]['liveness'])
all_playlists[playlist_name]['loudness'].append(features[0]['loudness'])
all_playlists[playlist_name]['speechiness'].append(features[0]['speechiness'])
all_playlists[playlist_name]['tempo'].append(features[0]['tempo'])
all_playlists[playlist_name]['valence'].append(features[0]['valence'])
query = (
"INSERT OR REPLACE INTO spotify_songs ("
"track_uri, name, genre, playlist, duration_ms, key, mode, time_signature, "
"acousticness, danceability, energy, instrumentalness, liveness, loudness, speechiness, "
"tempo, valence) "
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
)
cursor.execute(
query,
(
track_uri,
name,
genre,
playlist,
features[0]['duration_ms'],
features[0]['key'],
features[0]['mode'],
features[0]['time_signature'],
features[0]['acousticness'],
features[0]['danceability'],
features[0]['energy'],
features[0]['instrumentalness'],
features[0]['liveness'],
features[0]['loudness'],
features[0]['speechiness'],
features[0]['tempo'],
features[0]['valence']
)
)
connection.commit()
return results
def visualize_playlist_data(playlist):
df = pd.DataFrame.from_dict(playlist) # wont work if name and track_uri are in df
df.boxplot()
plt.show()
if __name__ == '__main__':
main()