/
server.py
477 lines (381 loc) · 15.7 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
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user
from flask import Flask, render_template, g, render_template, flash, request, redirect, url_for, session, jsonify
from forms import LoginForm, RegisterForm, PasswordForm, UpdateProfileForm
from flask_sqlalchemy import SQLAlchemy
import requests
import sqlite3
import hashlib
import base64
import json
import csv
import os
import random
########################
# Flask Set Up #
########################
# Flask App Creation
app = Flask(__name__)
app.config.from_object('config')
port = int(os.getenv('PORT',8000))
########################
# Database Setup #
########################
db = SQLAlchemy(app)
class Users(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True)
password = db.Column(db.String(120))
name = db.Column(db.String(120))
email = db.Column(db.String(120), unique=True)
def __init__(self, username, password):
self.username = username
self.password = password
def __repr__(self):
return '<User %r>' % self.username
class Songs(db.Model):
id = db.Column(db.Integer, primary_key=True)
band = db.Column(db.String(120))
song = db.Column(db.String(120))
album = db.Column(db.String(120))
def __init__(self, band, song, album):
self.band = band
self.song = song
self.album = album
class Ratings(db.Model):
id = db.Column(db.Integer, primary_key=True)
uid = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
sid = db.Column(db.Integer, db.ForeignKey('songs.id'), nullable=False)
rating = db.Column(db.Integer)
def __init__(self, uid, sid, rating):
self.uid = uid
self.sid = sid
self.rating = rating
class RatedSongs(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20))
band = db.Column(db.String(120))
song = db.Column(db.String(120))
album = db.Column(db.String(120))
rating = db.Column(db.Integer)
def __init__(self, username, band, song, album, rating):
self.username = username
self.band = band
self.song = song
self.album = album
self.rating = rating
########################
# User Login #
########################
# Configure Flask-Login
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
# User class for Flask-Login
class User(UserMixin):
def __init__(self, id):
self.id = id
# User callback for Flask-Login
@login_manager.user_loader
def load_user(user_id):
return User(user_id)
# User Registration
@app.route('/register', methods=('GET', 'POST'))
def register():
form = RegisterForm()
if request.method == 'POST':
if form.validate_on_submit():
# Check is a user already exists within the db
if Users.query.filter_by(username=form.username.data).first() != None:
flash('User "%s" already exists' % form.username.data)
else:
# Rudimentary password hashing, can be replaced later if desired
hash_pass = hashlib.sha256(form.password.data.encode('utf-8')).hexdigest()
# Insert new user into the db
new_user = Users(form.username.data, hash_pass)
db.session.add(new_user)
db.session.commit()
flash('Registration Successful')
return redirect(url_for('login'))
else:
if form.password.data != form.confirm.data:
flash('Password fields do not match')
else:
flash('Registration unsucessful, please check your inputs and try again')
return render_template('register.html', form=form)
# User Login
@app.route('/login', methods=('GET', 'POST'))
def login():
check_user_exists = ('SELECT password FROM users WHERE username="%s"')
retrieve_user_id = ('SELECT id FROM users WHERE username="%s"')
form = LoginForm()
if request.method == 'POST':
if form.validate_on_submit():
# Check if the user exists within the db
check_user = Users.query.filter_by(username=form.username.data).first()
if check_user != None:
# Check is password hashes match
if check_user.password == hashlib.sha256(form.password.data.encode('utf-8')).hexdigest():
# Grab the user's id
session['user_id'] = check_user.id
session['user_username'] = check_user.username
# Log in the user
user = User(session['user_id'])
login_user(user)
return redirect(url_for('index'))
else:
flash('Username or password was incorrect')
else:
flash('Username or password was incorrect')
else:
flash('Login unsuccessful, missing fields')
return render_template('login.html', form=form)
# User Log Out
@app.route('/logout')
@login_required
def logout():
logout_user()
session.clear()
return redirect(url_for('login'))
# Delete User Account
@app.route('/delete-account', methods=('GET','POST'))
@login_required
def deleteAccount():
if request.method == 'POST':
user = Users.query.filter_by(username=session['user_username']).first()
db.session.delete(user)
db.session.commit()
return redirect(url_for('logout'))
########################
# Spotify OAuth #
########################
@app.route('/spotify-request-auth', methods=('GET', 'POST'))
def spotifyRequestAuth():
# Step 1: Request Authorization
client_id = 'a5e0fc20e60c4bf18e051c669a9c7c77'
redirect_uri = 'http://localhost:8000/callback'
scope = 'user-library-read'
auth_url = (('https://accounts.spotify.com/authorize/?client_id=%s' +
'&response_type=code' +
'&redirect_uri=%s' +
'&scope=%s') % (client_id,redirect_uri, scope))
return redirect(auth_url)
@app.route('/callback', methods=('GET', 'POST'))
def spotifyCallback():
# Step 3: User is redirected back to specified URI
token_url = 'https://accounts.spotify.com/api/token'
# These should be kept secret and stored securely! In future versions, we will
# regenerate a new client secret. This is currently very bad practice.
client_id = 'a5e0fc20e60c4bf18e051c669a9c7c77'
client_secret = 'dfe55edaa2e540b89505d5d930c99f4d'
# Encode client_id and client_secret
base64encoded = base64.b64encode("%s:%s" % (client_id, client_secret))
headers = {"Authorization": "Basic %s" % (base64encoded)}
data = {
'grant_type': 'authorization_code',
'code': str(request.args['code']),
'redirect_uri': 'http://localhost:8000/callback'
}
# Step 4: Request refresh and access tokens
post_request = requests.post(token_url, data=data, headers=headers)
# Step 5: Tokens are returned to application
response_data = json.loads(post_request.text)
access_token = response_data['access_token']
refresh_token = response_data['refresh_token']
token_type = response_data['token_type']
expires_in = response_data['expires_in']
# Step 6: Use the access token to access the Spotify Web API
authorization_header = {'Authorization': 'Bearer %s' % access_token}
# END AUTHORIZTION PROCESS
# Spotify API example
# Get a list of the songs saved in the current Spotify user's "Your Music" library
get_tracks_endpoint = 'https://api.spotify.com/v1/me/tracks'
get_tracks_response = requests.get(get_tracks_endpoint, headers=authorization_header)
get_tracks_data = json.loads(get_tracks_response.text)
return jsonify(**get_tracks_data)
########################
# Routes #
########################
# localhost:8000/example_route
@app.route('/example_route')
def example():
# Render the "example_template.html" template from templates/
return render_template('example_template.html')
@app.route('/', methods=('GET','POST'))
@login_required
def index():
user_username = session['user_username']
# If no session['song-x'] exists, initialize defaults
if 'song-1' not in session:
if session['user_username'] == 'cjutmc':
session['song-1'] = {'id': 9000,
'band': 'Smino',
'song': 'song_1535',
'album': 'Blkswn'}
session['song-2'] = {'id': 9001,
'band': 'Muse',
'song': 'song_1012',
'album': 'Origin of Symmetry'}
session['song-3'] = {'id': 9002,
'band': 'Veil of Maya',
'song': 'song_114',
'album': 'False Idol'}
session['song-4'] = {'id': 9003,
'band': 'BROCKHAMPTON',
'song': 'song_872',
'album': 'Saturation'}
session['song-5'] = {'id': 9004,
'band': 'Vince Staples',
'song': 'song_1003',
'album': 'Summetime 06'}
else:
# Pull highest rated suggestion for current user
user_id = Users.query.filter_by(username=session['user_username']).first()
rating_data = Ratings.query.filter_by(uid=user_id.id).order_by(Ratings.rating)
count = 1
for rating in rating_data:
new_song_data = Songs.query.filter_by(id=rating.sid).first()
print('song-%s' % count)
session[('song-%s' % count)] = {'id': new_song_data.id,
'band': new_song_data.band,
'song': new_song_data.song,
'album': new_song_data.album}
count +=1
db.session.delete(rating)
if count > 5:
break
db.session.commit()
return render_template('index.html', user_username=user_username,
song_1=session['song-1'], song_2=session['song-2'],
song_3=session['song-3'], song_4=session['song-4'],
song_5=session['song-5'])
@app.route('/profile', methods=('GET','POST'))
@login_required
def profile():
change = PasswordForm()
update_profile = UpdateProfileForm()
# Change Password form
if request.method == 'POST' and request.form['form-check'] == 'Change Password':
if change.password.data == change.confirm.data:
temp = Users.query.filter_by(username=session['user_username']).first()
hash_pass = hashlib.sha256(change.password.data.encode('utf-8')).hexdigest()
temp.password = hash_pass
db.session.commit()
return redirect(url_for('logout'))
# Update Profile form
if request.method == 'POST' and request.form['form-check'] == 'Update Profile':
name = update_profile.name.data
email = update_profile.email.data
user = Users.query.filter_by(username=session['user_username']).first()
user.name = name
user.email = email
db.session.commit()
return redirect(url_for('profile'))
user = Users.query.filter_by(username=session['user_username']).first()
user_username = session['user_username']
name = user.name
email = user.email
return render_template('profile.html', user_username=user_username, change=change,
update_profile=update_profile, name=name, email=email)
@app.route('/rating', methods=('GET', 'POST'))
def rating():
if request.method == 'POST':
# request.data is in the form {'song':'song-1','rating':'4'}
rating_data = json.loads(request.data)
song_data = session[rating_data['song']]
# Insert into user's rated songs
rated_song = RatedSongs(session['user_username'], song_data['band'],
song_data['song'], song_data['album'],
rating_data['rating'])
db.session.add(rated_song)
db.session.commit()
return redirect(url_for('index'))
@app.route('/new_song', methods=('GET', 'POST'))
def newSong():
# Pull highest rated suggestion for current user
user_id = Users.query.filter_by(username=session['user_username']).first()
rating_data = Ratings.query.filter_by(uid=user_id.id).order_by(Ratings.rating).first()
new_song_data = Songs.query.filter_by(id=rating_data.sid).first()
# Retrieve new song to be rated from ML algorithm
# Build a string json out of the data
song_json = {}
song_json['id'] = (new_song_data.id)
song_json['band'] = (str(new_song_data.band))
song_json['song'] = (new_song_data.song)
song_json['album'] = (str(new_song_data.album))
# Store song to session
song_num = request.args['song_num']
session['song-%s' % song_num] = song_json
# Convert to json object for axios request
song_json = json.dumps(song_json)
db.session.delete(rating_data)
db.session.commit()
return song_json
@app.route('/rated-songs', methods=('GET','POST'))
@login_required
def ratedSongs():
user_username = session['user_username']
rated_songs_data = RatedSongs.query.filter_by(username=session['user_username'])
rated_songs = []
for song in rated_songs_data:
rated_songs.append(song)
return render_template('rated_songs.html', user_username=user_username,
rated_songs=rated_songs)
@app.route('/users', methods=('GET','POST'))
@login_required
def users():
user_username = session['user_username']
return render_template('users.html', user_username=user_username)
@app.route('/populate_db', methods=('GET', 'POST'))
def populateDb():
# User Creation
for i in range(1,20):
hash_pass = hashlib.sha256('password'.encode('utf-8')).hexdigest()
new_user = Users(('user_%s' % i), hash_pass)
db.session.add(new_user)
print('user_%s' % i)
db.session.commit()
# Artist Array Creation
artists = []
albums = []
with open('artist.csv') as f:
reader = csv.reader(f)
artists = list(reader)
with open('albums.csv') as j:
reader2 = csv.reader(j)
albums = list(reader2)
# Song Creation
for i in range(1,300001):
place = random.randint(0,99)
new_song = Songs(artists[place][0], ('song_%s' % i), albums[place][0])
db.session.add(new_song)
print('song_%s' % i)
db.session.commit()
# Read in algorithm output
for line in open("top_25_ratings_for_20_users.csv"):
csv_row = line.split() #returns a list ["1","50","60"]
new_suggestion = Ratings(csv_row[0], csv_row[1], csv_row[2])
db.session.add(new_suggestion)
print(csv_row)
db.session.commit()
return "dasf"
# @app.route('/rating_two')
# def rating_two():
# print("Song 2 rated.")
# return redirect(url_for('index'))
#
# @app.route('/rating_three')
# def rating_three():
# print("Song 3 rated.")
# return redirect(url_for('index'))
#
# @app.route('/rating_four')
# def rating_four():
# print("Song 4 rated.")
# return redirect(url_for('index'))
#
# @app.route('/rating_five')
# def rating_five():
# print("Song 5 rated.")
# return redirect(url_for('index'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)