-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
552 lines (446 loc) · 18.4 KB
/
app.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
from flask import Flask, request, url_for, session, redirect, render_template
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
import sys
import pandas as pd
import math
import matplotlib
import matplotlib.pyplot as plt
matplotlib.use('Agg')
import seaborn as sns
from io import BytesIO
import base64
from wordcloud import WordCloud
import matplotlib.cm
import matplotlib.colors
import secrets
import os
import shutil
app = Flask(__name__)
clientID = '65d88f8d3c8d409da1893e3caa0c833f'
clientSecret = 'eb61ba04ff4f4ea3a921b8ed6c66b521'
app.secret_key = "abcdefg"
app.config['Session_Cookie_Name'] = "Ajai's Cookie"
TOKEN_INFO = "token_info"
@app.route('/home')
def login():
sp_oauth = create_spotify_oauth()
auth_url = sp_oauth.get_authorize_url()
return redirect(auth_url)
@app.route('/redirect')
def redirectPage():
sp_oauth = create_spotify_oauth()
session.clear()
code = request.args.get('code')
token_info = sp_oauth.get_access_token(code, check_cache=False)
session[TOKEN_INFO] = token_info
print("Token Info:", token_info)
return redirect(url_for('homePage', _external=True))
@app.route('/homePage')
def homePage():
return(render_template('index.html'))
@app.route('/critiquePage')
def critiquePage():
try:
token_info = get_token()
except:
print("user not logged in")
return redirect("/")
sp = spotipy.Spotify(auth=token_info['access_token'])
current_playlists = sp.current_user_playlists()['items']
playlists = []
for playlist in current_playlists:
playlists.append(playlist['id'])
popularity_scores = []
artists = []
def getPopularity():
start=0
while True:
items= sp.current_user_saved_tracks(limit=50, offset=start*50)
for song in items['items']:
popularity = song['track']['popularity']
artist = song['track']['artists'][0]['name']
if artist in artists:
None
else:
artists.append(artist)
if popularity == 0 or popularity == 1:
None
else:
popularity_scores.append(popularity)
start += 1
if (len((items['items'])) < 50):
break
global_artists = []
def getArtists(playlist_id):
start=0
while True:
items= sp.playlist_items(playlist_id, limit=100, offset=start*50)
for song in items['items']:
artist = song['track']['artists'][0]['name']
global_artists.append(artist)
start += 1
if (len((items['items'])) < 100):
break
def compare_intersect(x, y):
return frozenset(x).intersection(y)
getPopularity()
getArtists('spotify:playlist:6UeSakyzhiEt4NB3UAd6NQ')
avg_pop = round(sum(popularity_scores) / len(popularity_scores))
same_artists = len(compare_intersect(artists, global_artists))
num_artists = len(artists)
return(render_template('critique.html', **locals()))
@app.route('/getTracks')
def getTracks():
try:
token_info = get_token()
except:
print("user not logged in")
return redirect("/")
sp = spotipy.Spotify(auth=token_info['access_token'])
current_playlists = sp.current_user_playlists()['items']
playlists = []
for playlist in current_playlists:
playlists.append(playlist['id'])
def msToMin(ms):
return(str(round(ms/60000, 2)))
song_uris=[]
#Use this: https://medium.com/analytics-vidhya/your-top-100-songs-2020-in-python-and-plotly-2e803d7e2990
##MAKE GRAPH OF LEAST POPULAR SONGS (without zeroes )
def allPlaylistSongs():
f = open('songs.csv', 'r+')
f.truncate(0)
filename = 'songs.csv'
f = open(filename, 'a', encoding="utf-8")
headers = 'Name,Artist,Popularity,Length,Release,Date_Added\n'
f.write(headers)
start=0
while True:
items= sp.current_user_saved_tracks(limit=50, offset=start*50)
for song in items['items']:
name = song['track']['name']
name = name.replace(",", "")
artist = song['track']['artists'][0]['name']
popularity = song['track']['popularity']
length = song['track']['duration_ms']
release = song['track']['album']['release_date']
added = song['added_at']
#maybe add followers
f.write(name+', '+artist+', '+str(popularity)+', '+msToMin(length)+', '+release[:4]+', '
+added[:7]+'\n')
start += 1
if (len((items['items'])) < 50):
break
f.close()
allPlaylistSongs()
df = pd.read_csv('songs.csv', encoding="ISO-8859-1", on_bad_lines='skip')
top_10_artists = df['Artist'].value_counts().nlargest(10)
bottom_10_artists = df['Artist'].value_counts().nsmallest(10)
top_10_pop = df.nlargest(10, 'Popularity')
top_10_length = df.nlargest(10, 'Length')
sns.set(style="whitegrid")
ax = sns.histplot( data=df, x='Popularity', color="lightgreen", alpha=1.0)
plt.xlabel('Popularity')
plt.ylabel('Count')
plt.title('Sample Seaborn Plot')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Popularity of all Songs', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
# Save the Seaborn plot to a BytesIO object
popularity_hist_buf = BytesIO()
plt.savefig(popularity_hist_buf, format='png', bbox_inches="tight")
popularity_hist_buf.seek(0)
popularity_hist_base64 = base64.b64encode(popularity_hist_buf.read()).decode('utf-8')
plt.clf()
ax = sns.histplot(df[df['Artist'].isin(top_10_artists.index)], x='Artist', color="lightgreen", alpha=1.0)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
plt.xlabel('Artists')
plt.ylabel('Count')
plt.title('Top Artist Seaborn Plot')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Song Distribution of Top 10 Artists', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
artists_buf = BytesIO()
plt.savefig(artists_buf, format='png', bbox_inches="tight")
artists_buf.seek(0)
artists_base64 = base64.b64encode(artists_buf.read()).decode('utf-8')
plt.clf()
ax = sns.histplot(df[df['Artist'].isin(bottom_10_artists.index)], x='Artist', color="lightgreen", alpha=1.0)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
plt.xlabel('Artists')
plt.ylabel('Count')
plt.title('Bottom Artist Seaborn Plot')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Song Distribution of Bottom 10 Artists', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
bot_artists_buf = BytesIO()
plt.savefig(bot_artists_buf, format='png', bbox_inches="tight")
bot_artists_buf.seek(0)
bot_artists_base64 = base64.b64encode(bot_artists_buf.read()).decode('utf-8')
plt.clf()
ax = sns.histplot(data=df, x='Release', color="lightgreen", alpha=1.0)
plt.xlabel('Years')
plt.ylabel('Count')
plt.title('Release Seaborn Plot')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Distribution of All Song Release Dates', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
release_date_buf = BytesIO()
plt.savefig(release_date_buf, format='png', bbox_inches='tight')
release_date_buf.seek(0)
release_date_base64 = base64.b64encode(release_date_buf.read()).decode('utf-8')
plt.clf()
ax = sns.scatterplot(data=df, x='Length', y='Popularity', color="lightgreen", alpha=1.0)
plt.xlabel('Song Length')
plt.ylabel('Popularity')
plt.title('Length vs Popularity Seaborn Plot')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Song length vs Popularity', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
length_v_pop_buf = BytesIO()
plt.savefig(length_v_pop_buf, format='png', bbox_inches='tight')
length_v_pop_buf.seek(0)
length_v_pop_base64 = base64.b64encode(length_v_pop_buf.read()).decode('utf-8')
plt.clf()
ax = sns.barplot(data=df[df['Artist'].isin(top_10_artists.index)], x='Artist', y='Length')
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
plt.xlabel('Artist')
plt.ylabel('Song Length')
plt.title('Length vs Artist Seaborn Plot')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Average Song Lengths of Top 10 Artists', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
artist_len_buf = BytesIO()
plt.savefig(artist_len_buf, format='png', bbox_inches='tight')
artist_len_buf.seek(0)
artist_len_base64 = base64.b64encode(artist_len_buf.read()).decode('utf-8')
plt.clf()
ax = sns.histplot(data=df, x="Date_Added", color="lightgreen", alpha=1.0)
plt.xlabel('Date Added')
plt.ylabel('Count')
plt.title('How Many Songs Added Per Month')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Distribution of Dates - Songs Added', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
songs_added_buf = BytesIO()
plt.savefig(songs_added_buf, format='png', bbox_inches="tight")
songs_added_buf.seek(0)
songs_added_base64 = base64.b64encode(songs_added_buf.read()).decode('utf-8')
plt.clf()
ax = sns.barplot(data=top_10_pop, x='Popularity', y='Name')
plt.xlabel('Popularity')
plt.ylabel('Song Name')
plt.title('Top 10 Most Popular Songs')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Top 10 Most Popular Artists', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
song_pop_buf = BytesIO()
plt.savefig(song_pop_buf, format='png', bbox_inches="tight")
song_pop_buf.seek(0)
song_pop_base64 = base64.b64encode(song_pop_buf.read()).decode('utf-8')
plt.clf()
sns.set(style="whitegrid")
ax = sns.barplot(data=top_10_length, x='Name', y='Length')
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
plt.xlabel('Song Name')
plt.ylabel('Length')
plt.title('Top 10 Longest Songs')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Top 10 Longest Songs', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
song_len_buf = BytesIO()
plt.savefig(song_len_buf, format='png', bbox_inches="tight")
song_len_buf.seek(0)
song_len_base64 = base64.b64encode(song_len_buf.read()).decode('utf-8')
plt.clf()
return render_template('data.html', popularity_hist_base64=popularity_hist_base64 ,artists_base64=artists_base64,
release_date_base64=release_date_base64, length_v_pop_base64=length_v_pop_base64,
artist_len_base64=artist_len_base64, songs_added_base64=songs_added_base64, song_pop_base64=song_pop_base64,
bot_artists_base64=bot_artists_base64, song_len_base64=song_len_base64,)
def get_token():
token_info = session.get(TOKEN_INFO, None)
if not token_info:
raise Exception("Token not found in session")
now = int(time.time())
is_expired = token_info['expires_at'] - now <60
if (is_expired):
sp_oauth = create_spotify_oauth()
token_info = sp_oauth.refresh_access_token(token_info['refresh_token'])
session[TOKEN_INFO] = token_info
return token_info
def create_spotify_oauth():
return SpotifyOAuth(
client_id='65d88f8d3c8d409da1893e3caa0c833f',
client_secret='eb61ba04ff4f4ea3a921b8ed6c66b521',
redirect_uri=url_for('redirectPage', _external=True),
scope = "user-library-read playlist-read-private playlist-read-collaborative")
@app.route('/getGenres')
def getGenres():
try:
token_info = get_token()
except:
print("user not logged in")
return redirect("/")
sp = spotipy.Spotify(auth=token_info['access_token'])
def msToMin(ms):
return(str(round(ms/60000, 2)))
start = 0
def allPlaylistGenres():
f = open('genres.csv', 'r+')
f.truncate(0)
filename = 'genres.csv'
f = open(filename, 'a', encoding="utf-8")
headers = 'Genre,Popularity,Length,Release\n'
f.write(headers)
start = 0
while True:
items = sp.current_user_saved_tracks(limit=50, offset=start*50)
for song in items['items']:
artist_id= song['track']['artists'][0]['id']
artist = sp.artist(artist_id)
try:
genre= artist['genres'][0]
except:
None
popularity = song['track']['popularity']
length = song['track']['duration_ms']
release = song['track']['album']['release_date']
f.write(genre+','+str(popularity)+','+str(msToMin(length))+','+str(release[:4])+'\n')
start += 1
if (len((items['items'])) < 50):
break
f.close()
allPlaylistGenres()
df = pd.read_csv('genres.csv', encoding="ISO-8859-1",on_bad_lines="skip")
sns.set(style="whitegrid")
genre_counts = df['Genre'].value_counts().nlargest(20)
genre_10 = df['Genre'].value_counts().nlargest(10)
plt.pie(genre_10, labels=genre_10.index, colors=sns.color_palette("Greens"), textprops={'color': 'white'})
plt.title('Top 10 Genres')
ax = plt.gca()
# Set the color of the labels to white
label_color = 'white'
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_color(label_color)
# Set the color of the ticks to white
ax.tick_params(axis='x', colors=label_color)
ax.tick_params(axis='y', colors=label_color)
ax.set_title('Top 10 Genres', color='white', fontsize=16)
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
genre_pie_buf = BytesIO()
plt.savefig(genre_pie_buf, format='png')
genre_pie_buf.seek(0)
genre_pie_base64 = base64.b64encode(genre_pie_buf.read()).decode('utf-8')
plt.clf()
ax = sns.barplot(x=genre_counts.index, y=genre_counts.values)
ax.set_xticklabels(ax.get_xticklabels(), rotation=45, ha='right')
plt.title('Top 20 Genres')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
ax.patch.set_facecolor('none')
ax.patch.set_alpha(0.0)
ax.set_title('Top 20 genres', color='white', fontsize=16)
ax.tick_params(axis='x', colors='white')
ax.tick_params(axis='y', colors='white')
ax.xaxis.label.set_color('white')
ax.yaxis.label.set_color('white')
genre_hist_buf = BytesIO()
plt.savefig(genre_hist_buf, format='png', bbox_inches = 'tight')
genre_hist_buf.seek(0)
genre_hist_base64 = base64.b64encode(genre_hist_buf.read()).decode('utf-8')
plt.clf()
genre_counts_all = df['Genre'].value_counts()
ax = wordcloud = WordCloud(width=800, height=400, background_color=None).generate_from_frequencies(genre_counts_all)
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis('off')
plt.title('Word Cloud of Genres')
fig = plt.gcf()
fig.patch.set_facecolor('none')
fig.patch.set_alpha(0.0)
# Access the Axes and set its background color to be transparent
wordcloud_buf = BytesIO()
plt.savefig(wordcloud_buf, format='png')
wordcloud_buf.seek(0)
wordcloud_base64 = base64.b64encode(wordcloud_buf.read()).decode('utf-8')
plt.clf()
top_genre = most_common_result = df['Genre'].value_counts().idxmax()
return render_template('genre.html', **locals())