-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
583 lines (464 loc) · 19.6 KB
/
server.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
import os
import time
import psycopg2
from flask import Flask, request, jsonify, render_template, redirect, session
from flask_caching import Cache
from flask_session import Session
from pydub import AudioSegment
from google.cloud import speech
import nltk
from cryptography.fernet import Fernet
from nltk.sentiment import SentimentIntensityAnalyzer
import spotipy
from spotipy import SpotifyClientCredentials, SpotifyOAuth, CacheFileHandler
import dotenv
dotenv.load_dotenv()
conn = psycopg2.connect(os.getenv("DATABASE_URL"), sslmode='require')
app = Flask(__name__)
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
app.config['SESSION_TYPE'] = 'filesystem'
app.config["CACHE_TYPE"] = "FileSystemCache" # better not use this type w. gunicorn
app.config["CACHE_DIR"] = "cache"
cache = Cache(app)
Session(app)
# @app.route('/')
# def index():
# response = startup.getUser()
# return redirect(response)
love = [
0.40194,
0.5365000000000001,
0.6223400000000001,
115.34268000000002,
0.88,
0.4081372,
0.011372955800000003,
-7.34488,
]
happy = [
0.59787,
0.7359399999999999,
0.6777299999999998,
120.32875,
0.71,
0.12648247999999995,
0.0031038976,
-5.245430000000002,
]
sad = [
0.3026608695652174,
0.4466391304347825,
0.5615760869565216,
123.51227173913044,
0.8586956521739131,
0.5126115217391305,
0.03200099934782609,
-8.877771739130429,
]
angry = [
0.46524799999999994,
0.7859599999999999,
0.5679000000000001,
130.28026000000003,
0.44,
0.07928980000000001,
0.004783572,
-4.866699999999999,
]
excited = [
0.52866,
0.75794,
0.5932600000000001,
130.64178,
0.82,
0.07200495999999999,
0.0061866928,
-5.571400000000001,
]
vectorlist = [love, happy, sad, angry, excited]
@app.route('/callback')
def index():
print(cache.get("username"))
cache_handler = CacheFileHandler(username=cache.get("username"))
auth_manager = SpotifyOAuth(
client_id=os.getenv("SPOTIFY_CLIENT_ID"),
client_secret=os.getenv("SPOTIFY_CLIENT_SECRET"),
redirect_uri=os.getenv("SPOTIFY_REDIRECT_URI"),
scope="user-read-recently-played user-library-read playlist-modify-private playlist-modify-public user-library-read user-library-modify user-top-read user-read-currently-playing user-read-playback-state user-modify-playback-state",
cache_path=os.getenv("SPOTIFY_CACHE_PATH"),
cache_handler=CacheFileHandler(username=cache.get("username")),
show_dialog=True
)
if request.args.get("code"):
print("code")
auth_manager.get_cached_token()
# Step 2. Being redirected from Spotify auth page
auth_manager.get_access_token(request.args.get("code"))
cache_handler.save_token_to_cache(auth_manager.get_cached_token())
return redirect('/callback')
if not auth_manager.validate_token(cache_handler.get_cached_token()):
print("not logged in")
# Step 1. Display sign in link when no token
auth_url = auth_manager.get_authorize_url()
return f'<h2><a href="{auth_url}">Sign in</a></h2>'
# Step 3. Signed in, display data
spotify = spotipy.Spotify(auth_manager=auth_manager)
return f'<h2>Hi {spotify.me()["display_name"]}, ' \
f'<small><a href="/sign_out">[sign out]<a/></small></h2>' \
f'<a href="/playlists">my playlists</a> | ' \
f'<a href="/currently_playing">currently playing</a> | ' \
f'<a href="/current_user">me</a>' \
@app.route('/sign_out')
def sign_out():
session.pop("token_info", None)
return redirect('/callback')
@app.route('/test')
def test():
return "hello"
@app.route('/playlists')
def playlists():
cache_handler = CacheFileHandler(username=cache.get("username"))
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler)
if not auth_manager.validate_token(cache_handler.get_cached_token()):
return redirect('/')
client = spotipy.Spotify(auth_manager=auth_manager)
return jsonify(client.current_user_playlists())
@app.route("/create_playlist")
def create_playlist():
pass
def compare_music_taste(user1_id, user2_id):
# get music data for both users
# expects [[songs], [artists], [genres]]
user1_data = []
user2_data = []
with conn.cursor() as cur:
cur.execute(f"SELECT top5genres, top5artists, top5songs FROM preferences WHERE user_id='{user1_id}'")
res = cur.fetchall()
user1_data = [res[0][2], res[0][1], res[0][0]]
cur.execute(f"SELECT top5genres, top5artists, top5songs FROM preferences WHERE user_id='{user2_id}'")
res = cur.fetchall()
user2_data = [res[0][2], res[0][1], res[0][0]]
# compare the top 5 songs
song_match = len(set(user1_data[0]).intersection(user2_data[0])) / 3
# compare the top 5 artists
artist_match = len(set(user1_data[1]).intersection(user2_data[1])) / 3
# compare the top 5 genres (weighted more heavily)
genre_match = len(set(user1_data[2]).intersection(user2_data[2])) / 3
# calculate the overall compatibility as a percentage
compatibility = (song_match + (artist_match * 2) + (genre_match * 3)) / 6 * 100
with conn.cursor() as cur:
cur.execute(f"INSERT INTO compatibility (user_id_1, user_id_2, comp) VALUES ('{user1_id}', '{user2_id}', {compatibility})")
conn.commit()
@app.route("/sign_up", methods=["POST"])
def sign_up():
username = request.json['username']
password = request.json['password']
key = ""
with open("secret.txt") as f:
key = f.readline()
fern = Fernet(key)
password = fern.encrypt(password.encode()).decode("utf-8").strip("'")
with conn.cursor() as cur:
cur.execute("SELECT username, password, id FROM users")
res = cur.fetchall()
g = 1
for user in res:
if user[0] == username:
g = 0
if user[1] == fern.decrypt(password).decode("utf-8"):
cache.set("username", username)
return jsonify("user_login")
else:
return jsonify("incorrect_pass")
if g == 1:
cur.execute(f"INSERT INTO users (username, password) VALUES ('{username}', '{password}')")
cur.execute(f"SELECT id FROM users WHERE username='{username}'")
user_id = cur.fetchall()[0][0]
# for user in res:
# compare_music_taste(user[2], user_id)
conn.commit()
cache.set("username", username)
return jsonify("user_created")
@app.route("/get_feed")
def get_feed():
curruser = cache.get("username")
# get request to this method
# should return a list of json objects in the following format:
# {"username": "exampleuser", "mood": "examplemood", "spotify_link": "examplelink", "compatibility": "examplecomp"}
with conn.cursor() as cur:
cur.execute(f"SELECT id FROM users WHERE username='{curruser}'")
user_id = cur.fetchall()[0][0]
cur.execute("SELECT user_id, mood, playlist_link FROM feedinfo")
feeds = cur.fetchall()
comps = []
# gets all of the comps where at least one of the things are the current user
try:
cur.execute(f"SELECT user_id_1, user_id_2, comp FROM compatability WHERE user_id_1='{user_id}'")
comps = cur.fetchall()
except Exception:
cur.execute("ROLLBACK;")
try:
cur.execute(f"SELECT user_id_1, user_id_2, comp FROM compatability WHERE user_id_2='{user_id}'")
comps.extend(cur.fetchall())
except Exception:
cur.execute("ROLLBACK;")
print(comps)
output = []
for f in feeds:
if f[0] == user_id:
continue
obj = {}
print(f[0])
foo = f[0]
cur.execute(f"SELECT username FROM users WHERE id='{foo}'")
username = cur.fetchall()[0][0]
obj["username"] = username
obj["spotify"] = f[2]
obj["mood"] = f[1]
for comp in comps:
if comp[0] == f[0] or comp[1] == f[0]:
obj["compatibility"] = comp[2]
break
output.append(obj)
return output
def get_polarity(text):
nltk.download('vader_lexicon')
sid = SentimentIntensityAnalyzer()
sentiment = sid.polarity_scores(text)
angry_words = ["angry", "mad", "irritated", "frustrated", "annoyed"]
for word in angry_words:
if word in text:
return "angry"
emotions = {"happy": sentiment["pos"], "excited": sentiment["pos"], "sad": sentiment["neg"], "love": sentiment["pos"]}
top_emotion = max(emotions, key=lambda key: emotions[key])
if "love" in text:
top_emotion = "love"
return top_emotion
@app.route("/analyze_audio", methods = ["POST"])
def analyze_audio():
if 'recording' not in request.files:
return jsonify('no image uploaded')
request.files['recording'].save("./recording.wav")
src = "recording.wav"
dst = "recording.mp3"
sound = AudioSegment.from_mp3(src)
sound.export(dst, format="mp3")
client = speech.SpeechClient.from_service_account_file('key.json')
file_name = dst
with open(file_name, 'rb') as f:
mp3_data = f.read()
audio_file = speech.RecognitionAudio(content = mp3_data)
config = speech.RecognitionConfig(
sample_rate_hertz=44100,
enable_automatic_punctuation = True,
language_code = 'en-US'
)
response = client.recognize(
config = config,
audio = audio_file
)
transcript = ""
for result in response.results:
transcript = "{}".format(result.alternatives[0].transcript)
emotion = get_polarity(transcript)
return {"emotion": emotion, "text": transcript}
@app.route("/gettopemotion/<emotion>")
def gettopemotion(emotion):
cache_handler = CacheFileHandler(username=cache.get("username"))
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler)
if not auth_manager.validate_token(cache_handler.get_cached_token()):
return redirect('/')
client = spotipy.Spotify(auth_manager=auth_manager)
with conn.cursor() as cur:
curruser = cache.get("username")
print(curruser)
# get user_id using username
cur.execute(f"SELECT id FROM users WHERE username='{curruser}'")
user_id = cur.fetchall()[0][0]
# select top 10 items from happydist column from songs table where preferences_id equals user_id
cur.execute(f"SELECT song_id FROM songs WHERE preferences_id='{user_id}' ORDER BY {emotion} ASC LIMIT 20")
res = cur.fetchall()
names = []
links = []
for r in res:
names.append(client.track(r[0])["name"])
links.append("spotify:track:" + str(client.track(r[0])["id"]))
if emotion == "happydist":
emotion = "happy"
elif emotion == "sadist":
emotion = "sad"
elif emotion == "excitedist":
emotion = "excited"
elif emotion == "lovedist":
emotion = "love"
elif emotion == "angrydist":
emotion = "angry"
result = client.user_playlist_create(client.current_user()["id"], f"{emotion} playlist", public=False)
time.sleep(1)
# print(client.current_user_playlists(limit=1)["items"][0]["id"])
# results = client.current_user_playlists(limit=50)
# temp = results["items"]
# while results['next']:
# results = client.next(results)
# temp.extend(results['items'])
client.playlist_add_items(result["id"], links, 0)
# http://open.spotify.com/track/6rqhFgbbKwnb9MLmUQDhG6
url = "http://open.spotify.com/track/" + client.current_user_playlists()["items"][0]["external_urls"]["spotify"].split(":")[-1]
item = {
"url": url,
"names": names,
}
return jsonify(item)
@app.route("/loadtracksintodb")
def loadtracksintodb():
print("loading tracks into db")
# time.sleep(5)
# print("loading tracks into db")
cache_handler = CacheFileHandler(username=cache.get("username"))
auth_manager = spotipy.oauth2.SpotifyOAuth(cache_handler=cache_handler)
if not auth_manager.validate_token(cache_handler.get_cached_token()):
return redirect('/')
client = spotipy.Spotify(auth_manager=auth_manager)
user_id = client.current_user()["id"]
print(user_id)
with conn.cursor() as cur:
curruser = cache.get("username")
print(curruser)
# get user_id using username
cur.execute(f"SELECT id FROM users WHERE username='{curruser}'")
user_id = cur.fetchall()[0][0]
print(user_id)
# results = self.get_current_user_saved_tracks()
results = client.current_user_saved_tracks(limit=50, offset=0)
temp = results["items"]
while results['next']:
results = client.next(results)
temp.extend(results['items'])
tracks = []
for item in temp:
tracks.append(item["track"]["id"])
print(len(tracks))
# get features in batches of 50
features = []
for i in range(0, len(tracks), 50):
features.extend(client.audio_features(tracks[i : i + 50]))
trackdict = {tracks[i]: features[i] for i in range(len(tracks))}
recommended_tracks = {}
counter = 0
for element in trackdict.keys():
counter += 1
if counter == 100:
break
trackfeatures = trackdict[element]
# get acousticness, danceability, energy, instrumentalness, loudness, mode, tempo, valence from track
try:
featurelist = [
trackfeatures["valence"],
trackfeatures["energy"],
trackfeatures["danceability"],
trackfeatures["tempo"],
trackfeatures["mode"],
trackfeatures["acousticness"],
trackfeatures["instrumentalness"],
trackfeatures["loudness"]
]
except:
continue
vdistance = []
for vector in vectorlist:
currentvector = vector
# get distance to current vector
distance = 0
for i in range(0, 8):
distance += (featurelist[i] - currentvector[i]) ** 2
distance = distance ** 0.5
vdistance.append(distance)
lovedist = vdistance[0]
happydist = vdistance[1]
sadist = vdistance[2]
angrydist = vdistance[3]
excitedist = vdistance[4]
if element == "7wBJfHzpfI3032CSD7CE2m":
print(featurelist)
print(currentvector)
print(vdistance)
# upload all vector distances to database
nullstring = "NULL"
# print(happydist)
# print(sadist)
# print(angrydist)
# print(excitedist)
# print(lovedist)
cur.execute(f"INSERT INTO songs (song_id, song_data, lovedist, happydist, sadist, angrydist, excitedist, preferences_id) VALUES ('{element}', {nullstring}, {lovedist}, {happydist}, {sadist}, {angrydist}, {excitedist}, '{user_id}');")
conn.commit()
return jsonify("done")
if __name__ == "__main__":
# creating the table
# with conn.cursor() as cur:
# cur.execute("CREATE TABLE preferences (user_id UUID, top5Genres STRING[], top5Artists STRING[], top5Songs STRING[], );")
#drop preferences table
# cur.execute("DROP TABLE IF EXISTS songs;")
# # make user_id in preferences a unique key and primary
# cur.execute("CREATE TABLE preferences (user_id UUID PRIMARY KEY, top5Genres STRING[], top5Artists STRING[], top5Songs STRING[]);")
# # time.sleep(20)
# cur.execute("CREATE TABLE songs (song_id STRING, song_data JSONB, lovedist FLOAT, happydist FLOAT, sadist FLOAT, angrydist FLOAT, excitedist FLOAT, preferences_id UUID, FOREIGN KEY (preferences_id) REFERENCES preferences(user_id));")
# cur.execute("CREATE INDEX lovedist_index ON songs (lovedist);")
# cur.execute("CREATE INDEX happydist_index ON songs (happydist);")
# cur.execute("CREATE INDEX sadist_index ON songs (sadist);")
# cur.execute("CREATE INDEX angrydist_index ON songs (angrydist);")
# cur.execute("CREATE INDEX excitedist_index ON songs (excitedist);")
# conn.commit()
# cur.execute("DROP INDEX IF EXISTS acousticness_index;")
# cur.execute("DROP INDEX IF EXISTS instrumentalness_index;")
# cur.execute("DROP INDEX IF EXISTS loudness_index;")
# cur.execute("DROP INDEX IF EXISTS mode_index;")
# cur.execute("DROP INDEX IF EXISTS tempo_index;")
# cur.execute("DROP INDEX IF EXISTS danceability_index;")
# cur.execute("DROP INDEX IF EXISTS energy_index;")
# cur.execute("DROP INDEX IF EXISTS valence_index;")
# # drop the columns as well
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS valence;")
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS energy;")
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS danceability;")
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS tempo;")
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS mode;")
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS acousticness;")
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS instrumentalness;")
# cur.execute("ALTER TABLE songs DROP COLUMN IF EXISTS loudness;")
# time.sleep(2)
# # make valence, energy, danceability, tempo, mode, acousticness, instrumentalness, loudness indexes
# cur.execute("CREATE INDEX valence_index ON songs (valence);")
# time.sleep(2)
# cur.execute("CREATE INDEX energy_index ON songs (energy);")
# time.sleep(2)
# cur.execute("CREATE INDEX danceability_index ON songs (danceability);")
# time.sleep(2)
# cur.execute("CREATE INDEX tempo_index ON songs (tempo);")
# time.sleep(2)
# cur.execute("CREATE INDEX mode_index ON songs (mode);")
# time.sleep(2)
# cur.execute("CREATE INDEX acousticness_index ON songs (acousticness);")
# time.sleep(2)
# cur.execute("CREATE INDEX instrumentalness_index ON songs (instrumentalness);")
# time.sleep(2)
# cur.execute("CREATE INDEX loudness_index ON songs (loudness);")
# time.sleep(2)
# conn.commit()
# cur.execute("SHOW TABLES;")
# res = cur.fetchall()
# print(res)
# fern = Fernet(key)
# message = "This is the password"
# encMessage = fernet.encrypt(message.encode())
# print("original string: ", message)
# print("encrypted string: ", encMessage)
# # decrypt the encrypted string with the
# # Fernet instance of the key,
# # that was used for encrypting the string
# # encoded byte string is returned by decrypt method,
# # so decode it to string with decode methods
# decMessage = fernet.decrypt(encMessage).decode()
# print("decrypted string: ", decMessage)
app.secret_key = os.urandom(24)
app.config['SESSION_TYPE'] = 'filesystem'
app.run(host="0.0.0.0", debug=True, threaded=True)