-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
704 lines (491 loc) · 20.3 KB
/
main.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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
import pymongo
import pprint
import pymysql
import json
from flask import Flask, flash, render_template, request, redirect, g, session
from pymongo import MongoClient
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine
from sqlalchemy.sql import text
from spotify_requests import spotify
# credit for spotify api: https://github.com/mari-linhares/spotify-flask
try:
import configparser
except:
from six.moves import configparser
app = Flask(__name__)
app.secret_key = "super secret key"
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:cs411project@localhost/smartify'
SQL_db = SQLAlchemy(app)
class Artist(SQL_db.Model):
__tablename__ = 'top_artists'
artist_id = SQL_db.Column(SQL_db.String(225), nullable=False, primary_key=True)
artist_name = SQL_db.Column(SQL_db.String(225), nullable=False)
popularity = SQL_db.Column(SQL_db.Integer, nullable=False)
rating = SQL_db.Column(SQL_db.Integer, nullable=False)
class Song(SQL_db.Model):
__tablename__ = 'top_songs'
song_id = SQL_db.Column(SQL_db.String(225), nullable=False, primary_key=True)
song_name = SQL_db.Column(SQL_db.String(225), nullable=False)
artist_name = SQL_db.Column(SQL_db.String(225), nullable=False)
popularity = SQL_db.Column(SQL_db.Integer, nullable=False)
rating = SQL_db.Column(SQL_db.Integer, nullable=False)
# SQL connection
engine = create_engine('mysql+pymysql://root:cs411project@localhost/smartify')
# mongodb connection
connectionString = "mongodb+srv://bin:Robin1999@cluster0-qgaoz.mongodb.net/test?retryWrites=true&w=majority"
client = MongoClient(connectionString)
db = client.smartify
# ----------------------- AUTH API PROCEDURE -------------------------
@app.route("/auth")
def auth():
return redirect(spotify.AUTH_URL)
@app.route("/callback/")
def callback():
auth_token = request.args['code']
auth_header = spotify.authorize(auth_token)
session['auth_header'] = auth_header
return home()
def valid_token(resp):
return resp is not None and not 'error' in resp
# HOME PAGE
@app.route("/")
def home():
if 'auth_header' in session:
auth_header = session['auth_header']
profile = spotify.get_users_profile(auth_header)
if not valid_token(profile):
return render_template("first.html")
# LOAD RECENTLY PLAYED SONGS
recently_played = spotify.get_users_recently_played(auth_header)
with open('recently_played.json', 'w') as outfile:
json.dump(recently_played, outfile)
with open('recently_played.json') as json_file:
recently_played_data = json.load(json_file)
#print(recently_played_data)
song_uris = []
song_names = []
played_at = []
for temp in recently_played_data['items']:
for key, value in temp.items():
if (key == 'played_at'):
played_at.append(value)
#print(value)
elif (key == 'track'):
for k, v in temp['track'].items():
if (k == 'uri'):
song_uris.append(v)
#print(v)
elif (k == 'name'):
song_names.append(v)
#print(v)
print("trying mongodb")
# clear database first
db.recently_played.drop()
for i in range(len(song_uris)):
new_song = ({"song_uri": song_uris[i], "song_name": song_names[i], "played_at": played_at[i] })
db.recently_played.insert_one(new_song)
# LOAD DATA ON TOP SONGS AND ARTISTS INTO MYSQL DATABASE
# ARTISTS
top_artists = spotify.get_users_top(auth_header, 'artists')
with open('top_artists.json', 'w') as outfile:
json.dump(top_artists, outfile)
with open('top_artists.json') as json_file:
artist_data = json.load(json_file)
#print(artist_data)
artist_ids = []
artist_names = []
artist_popularity = []
artist_ratings = []
count = 0
for temp in artist_data['items']:
for key, value in temp.items():
if (key == 'id'):
#print(value)
artist_ids.append(value)
if (count < 10):
artist_ratings.append(5)
elif (count < 20):
artist_ratings.append(4)
elif (count < 30):
artist_ratings.append(3)
elif (count < 40):
artist_ratings.append(2)
else:
artist_ratings.append(1)
count += 1
elif (key == 'name'):
#print(value)
artist_names.append(value)
elif (key == 'popularity'):
#print(value)
artist_popularity.append(value)
with engine.connect() as con:
con.execute("TRUNCATE TABLE top_artists")
for i in range(len(artist_ids)):
new_artist = Artist(artist_id = artist_ids[i], artist_name = artist_names[i], popularity = artist_popularity[i], rating = artist_ratings[i])
SQL_db.session.add(new_artist)
SQL_db.session.commit()
# SONGS
top_songs = spotify.get_users_top(auth_header, 'tracks')
with open('top_songs.json', 'w') as outfile:
json.dump(top_songs, outfile)
with open('top_songs.json') as json_file:
song_data = json.load(json_file)
#print(song_data)
song_ids = []
song_names = []
artist_names = []
song_popularity = []
song_ratings = []
count = 0
for temp in song_data['items']:
for key, value in temp.items():
#print(key)
if (key == 'id'):
#print(value)
song_ids.append(value)
if (count < 10):
song_ratings.append(5)
elif (count < 20):
song_ratings.append(4)
elif (count < 30):
song_ratings.append(3)
elif (count < 40):
song_ratings.append(2)
else:
song_ratings.append(1)
count += 1
elif (key == 'name'):
#print(value)
song_names.append(value)
elif (key == 'popularity'):
#print(value)
song_popularity.append(value)
elif (key == 'artists'):
for k, v in temp['artists'][0].items():
if (k == 'name'):
#print(v)
artist_names.append(v)
break
with engine.connect() as con:
con.execute("TRUNCATE TABLE top_songs")
for i in range(len(song_ids)):
new_song = Song(song_id = song_ids[i], song_name = song_names[i], artist_name = artist_names[i], popularity = song_popularity[i], rating = song_ratings[i])
SQL_db.session.add(new_song)
SQL_db.session.commit()
if valid_token(profile):
return render_template("first.html",
user=profile)
return render_template("first.html")
@app.route("/recent")
def recent():
if 'auth_header' in session:
auth_header = session['auth_header']
profile = spotify.get_users_profile(auth_header)
text = ""
for x in db.recently_played.find({}, {'_id': 0,'song_name': 1, 'played_at': 1}):
text += pprint.pformat(x)
text += '\n\n'
if valid_token(profile):
return render_template("recent.html",
user=profile, text = text)
return render_template("recent.html")
# LIST TOP SONGS AND ARTISTS
@app.route("/list")
def list():
if 'auth_header' in session:
auth_header = session['auth_header']
profile = spotify.get_users_profile(auth_header)
# DISPLAY CURRENT LIST OF TOP SONGS AND ARTISTS
with engine.connect() as con:
rows = con.execute('SELECT * FROM top_artists')
artist_text = ""
while True:
row = rows.fetchone()
if row == None:
break
else:
artist_text += str(row)
artist_text += "\n"
artist_text += "\n\n"
rows = con.execute('SELECT * FROM top_songs')
song_text = ""
while True:
row = rows.fetchone()
if row == None:
break
else:
song_text += str(row)
song_text += "\n"
if valid_token(profile):
return render_template("list.html",
user=profile, artist_text = artist_text, song_text = song_text)
return render_template("list.html")
# INSERT
@app.route("/insert")
def insert():
# MONGODB: print everything in collection
#database_text = ""
#for x in db.friends.find({}):
# database_text += pprint.pformat(x)
# database_text += '\n\n'
#count = db.friends.count_documents({})
#print("TESTTT")
#print(count)
#count = db.friends.count_documents({})
#print("TESTTT")
#print(count)
return render_template("insert.html")
@app.route('/insert', methods=['POST'])
def process_insert():
name = request.form['name']
rating = request.form['rating']
search_type = request.form['search_type']
if 'auth_header' in session:
auth_header = session['auth_header']
if (search_type == 'artist'):
data = spotify.search('artist', name, auth_header)
result = data['artists']['items']
print(result[0])
artist_id = result[0]['id']
artist_name = result[0]['name']
popularity = result[0]['popularity']
new_artist = Artist(artist_id = artist_id, artist_name = artist_name, popularity = popularity, rating = rating)
SQL_db.session.add(new_artist)
else:
data = spotify.search('track', name, auth_header)
result = data['tracks']['items']
print(result[0])
song_id = result[0]['id']
song_name = result[0]['name']
popularity = result[0]['popularity']
artist_name = ""
for k, v in result[0]['artists'][0].items():
if (k == 'name'):
artist_name = v
break
new_song = Song(song_id = song_id, song_name = song_name, artist_name = artist_name, popularity = popularity, rating = rating)
SQL_db.session.add(new_song)
SQL_db.session.commit()
return insert()
# SEARCH
@app.route("/search")
def search():
return render_template("search.html", text = "")
@app.route('/search', methods=['POST'])
def process_search():
rating = request.form['rating']
search_type = request.form['search_type']
# friend = ""
# for x in db.friends.find({"username": username}):
# friend += pprint.pformat(x)
# friend += '\n\n'
with engine.connect() as con:
command = "SELECT * FROM "
if (search_type == 'artist'):
command += "top_artists WHERE rating = '"
else:
command += "top_songs WHERE rating = '"
command += rating
command += "'"
rows = con.execute(command)
#songs = rows.fetchall()
result = ""
while True:
row = rows.fetchone()
if row == None:
break
else:
result += str(row)
result += "\n"
return render_template("search.html", text = result)
# UPDATE
@app.route("/update")
def update():
# MONGODB
# database_text = ""
# for x in db.friends.find({}):
# database_text += pprint.pformat(x)
# database_text += '\n\n'
# count = db.friends.count_documents({})
return render_template("update.html")
@app.route('/update', methods=['POST'])
def process_update():
name = request.form['name']
rating = request.form['rating']
search_type = request.form['search_type']
# MONGODB
# db.friends.update_one({"username": username},{"$set": {"rating": rating}})
if (search_type == 'artist'):
artist = Artist.query.filter_by(artist_name = name).first()
if artist:
artist.rating = rating
else:
song = Song.query.filter_by(song_name = name).first()
if song:
song.rating = rating
SQL_db.session.commit()
return update()
# REMOVE
@app.route("/remove")
def remove():
# MONGODB
# database_text = ""
# for x in db.friends.find({}):
# database_text += pprint.pformat(x)
# database_text += '\n\n'
# count = db.friends.count_documents({})
return render_template("remove.html")
@app.route('/remove', methods=['POST'])
def process_remove():
name = request.form['name']
search_type = request.form['search_type']
if (search_type == 'artist'):
Artist.query.filter_by(artist_name = name).delete()
else:
Song.query.filter_by(song_name = name).delete()
SQL_db.session.commit()
# MONGODB
# db.friends.remove( { "username": username } )
return remove()
@app.route("/query")
def query():
if 'auth_header' in session:
auth_header = session['auth_header']
profile = spotify.get_users_profile(auth_header)
if not valid_token(profile):
return render_template("query.html")
with engine.connect() as con:
rows = con.execute('SELECT top_artists.artist_name, count(*) as song_count FROM top_songs JOIN top_artists on top_songs.artist_name = top_artists.artist_name GROUP BY artist_name ORDER BY song_count desc;')
#data = rows.fetchall()
result = ""
while True:
row = rows.fetchone()
if row == None:
break
else:
result += str(row)
result += "\n"
rows = con.execute('SELECT song_name, popularity FROM top_songs ORDER BY popularity desc;')
popularity_result = ""
while True:
row = rows.fetchone()
if row == None:
break
else:
popularity_result += str(row)
popularity_result += "\n"
rows = con.execute('SELECT avg(popularity) as avg_popularity FROM top_artists;')
artist_rating = rows.fetchall()
artist_rating = str(artist_rating[0])
artist_rating = artist_rating[10:15]
print(artist_rating)
rows = con.execute('SELECT avg(popularity) as avg_popularity FROM top_songs;')
song_rating = rows.fetchall()
song_rating = str(song_rating[0])
song_rating = song_rating[10:15]
print(song_rating)
p_rating = (float(artist_rating) + float(song_rating)) / 2.0
return render_template("query.html", user = profile, current_data = result, popularity_data = popularity_result, artist_rating = artist_rating, song_rating = song_rating, popular_rating = p_rating)
@app.route("/playlist")
def playlist():
if 'auth_header' in session:
auth_header = session['auth_header']
profile = spotify.get_users_profile(auth_header)
if not valid_token(profile):
return render_template("playlist.html")
playlists = spotify.get_users_playlists(auth_header)
with open('playlists.json', 'w') as outfile:
json.dump(playlists, outfile)
with open('playlists.json') as json_file:
playlists = json.load(json_file)
playlist_names_str = ""
for temp in playlists['items']:
for key, value in temp.items():
if (key == 'name'):
playlist_names_str += value
playlist_names_str += "\n"
return render_template("playlist.html", user = profile, text = playlist_names_str)
return render_template("playlist.html")
@app.route('/playlist', methods=['POST'])
def process_playlist():
# MONGODB
# db.friends.remove( { "username": username } )
name = request.form['name']
if 'auth_header' in session:
auth_header = session['auth_header']
profile = spotify.get_users_profile(auth_header)
if not valid_token(profile):
return render_template("playlist.html")
# PARSE ALL OF USER'S PUBLIC PLAYLISTS
playlists = spotify.get_users_playlists(auth_header)
with open('playlists.json', 'w') as outfile:
json.dump(playlists, outfile)
with open('playlists.json') as json_file:
playlists = json.load(json_file)
playlist_names = []
playlist_ids = []
playlist_names_str = ""
for temp in playlists['items']:
for key, value in temp.items():
#print(key)
if (key == 'name'):
playlist_names.append(value)
playlist_names_str += value
playlist_names_str += "\n"
elif (key == 'id'):
playlist_ids.append(value)
result = "playlist not found!"
playlist_id = ""
for index in range(len(playlist_names)):
if (playlist_names[index] == name):
result = "done!"
playlist_id = playlist_ids[index]
break
if result == "playlist not found!":
return render_template("playlist.html", user = profile, text = playlist_names_str, result = result)
# PARSE PLAYLIST TRACKS
# get track ids of all tracks in playlist
playlist_tracks = spotify.get_playlist_tracks(auth_header, playlist_id)
with open('playlist_tracks.json', 'w') as outfile:
json.dump(playlist_tracks, outfile)
with open('playlist_tracks.json') as json_file:
playlist_tracks = json.load(json_file)
track_uris = []
for temp in playlist_tracks['items']:
for key, value in temp.items():
if (key == 'track'):
for k, v in temp[key].items():
if (k == 'uri'):
track_uris.append(v)
break
# loop through all track ids
for current_uri in track_uris:
# search mongodb database for track id
found = "" # CHANGEeEeeeeeeeeeee
for x in db.recently_played.find({"song_uri": current_uri}):
found += pprint.pformat(x)
found += '\n\n'
if (found == ""):
# if not found, remove track id from playlist
spotify.remove_track_from_playlist(auth_header, playlist_id, current_uri)
print("NOT FOUND")
with engine.connect() as con:
# get random track from current top 50 songs
rows = con.execute('SELECT song_id from top_songs order by rand() limit 1;')
song_id = rows.fetchall()
song_id = str(song_id)[3:25]
# use song as seed for spotify recommendation
recommendations = spotify.get_track_recommendation(auth_header, song_id)
# use recommendation to replace the old song
for temp in recommendations['tracks']:
for key, value in temp.items():
if (key == 'uri'):
song_rec_uri = value
spotify.add_track_to_playlist(auth_header, playlist_id, song_rec_uri)
break
return render_template("playlist.html", user = profile, text = playlist_names_str, result = result)
return playlist()
if __name__ == "__main__":
app.run(debug=True)