-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
469 lines (405 loc) · 24.2 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
from swaggerConfig import app
from flask import request, jsonify, send_from_directory, Response
from config import Config as SETTING
from flask_pymongo import pymongo
import os
import json
from passlib.hash import sha256_crypt
import secrets
import requests
import routes.route_auth
import routes.route_playlist
import routes.route_spotify
# app = Flask(__name__)
# app.secret_key = SETTING.FLASK_KEY
# Get Data from Database
myclient = pymongo.MongoClient(SETTING.MONGO_LINK)
GMusicDatabase = myclient[SETTING.DATABASE_NAME]
GMusicDatabaseCollection = GMusicDatabase[SETTING.GMUSIC_DATABASE_COLLECTION]
GMusicUsers = GMusicDatabase[SETTING.USERS_DATABASE_COLLECTION]
GMusicUsersData = GMusicDatabase[SETTING.USERS_DATA_COLLECTION]
# Public Routes
@app.route(f'/{SETTING.VERSION}/popular', methods=['GET'])
def index():
try:
if "page" in request.args:
page = int(request.args.get('page'))
if page > 0:
GMusicList = GMusicDatabaseCollection.find({}, {'_id': False}).skip(0 if page == 1 else page*SETTING.PAGING - SETTING.PAGING).limit(SETTING.PAGING)
else:
raise ValueError
else:
GMusicList = GMusicDatabaseCollection.find({}, {'_id': False}).skip(0).limit(SETTING.PAGING)
# Convert Data into List
GMusicFetchData = []
for GMusic in GMusicList:
GMusicFetchData.append(GMusic)
response = Response(json.dumps(GMusicFetchData), status=200, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except ValueError:
response = Response(json.dumps({"status": False, "message": "Bad Request"}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
print(e)
response = Response(json.dumps({"status" : False, "message" : "Error"}), status=404, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route(f'/{SETTING.VERSION}/spotify/<string:spotify_id>', methods=['GET'])
def spotify_id_func(spotify_id):
try:
GMusicList = GMusicDatabaseCollection.find({'spotify_id' : spotify_id}, {'_id': False})
# Send Response
response = Response(json.dumps(GMusicList[0]), status=200, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except IndexError:
response = Response(json.dumps({"status": False, "message": "Song not available"}), status=404, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except ValueError:
response = Response(json.dumps({"status": False, "message": "Bad Request"}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
# print(e)
response = Response(json.dumps({"status" : False, "message" : "Internal Server Error"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
# Authenticate Routes
@app.route(f'/{SETTING.VERSION}/playlist/<string:playlist_name>', methods=['GET', 'POST', 'PUT', 'DELETE'])
def add_playlist_func(playlist_name):
if request.method == "GET":
try:
if "api" in request.args:
api_key = request.args.get("api")
api_check = GMusicUsers.count_documents({"api" : api_key})
if api_check != 0:
# check that playlist is exists or not
user_id = GMusicUsers.find({"api" : api_key}, {"_id" : True})[0]['_id']
playlist_check = GMusicUsersData.count_documents({"user_id" : user_id, f"playlists.{playlist_name}" : {"$exists" : True}})
if playlist_check == 1:
# fetch playlist
GMusicList = GMusicUsersData.find({"user_id" : user_id}, {"_id" : False, f"playlists.{playlist_name}" : True})
GMusicList = GMusicList[0]["playlists"]
response = Response(json.dumps(GMusicList), status=200, mimetype='application/json')
else:
# playlist not exists
response = Response(json.dumps({"status": False, "message": "Playlist not exists"}), status=400, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Invalid API"}), status=401, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Bad Request"}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
print(e)
response = Response(json.dumps({"status" : False, "message" : "Internal Server Error"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
elif request.method == "POST":
try:
if "api" in request.form:
api_key = request.form["api"]
api_check = GMusicUsers.count_documents({"api" : api_key})
if api_check != 0:
# check that playlist is not already exist
user_id = GMusicUsers.find({"api" : api_key}, {"_id" : True})[0]['_id']
playlist_check = GMusicUsersData.count_documents({"user_id" : user_id, f"playlists.{playlist_name}" : {"$exists" : True}})
if playlist_check == 0:
# create new playlist
GMusicUsersData.update_one({"user_id" : user_id}, {"$set" : {f"playlists.{playlist_name}" : []}})
response = Response(json.dumps({"status": False, "message": "Playlist created"}), status=200, mimetype='application/json')
else:
# playlist already exists
response = Response(json.dumps({"status": False, "message": "Playlist already exists"}), status=400, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Invalid API"}), status=401, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Bad Request"}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
print(e)
response = Response(json.dumps({"status" : False, "message" : "Internal Server Error"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
elif request.method == "PUT":
try:
if "api" in request.form and "spotify_id" in request.form:
api_key = request.form["api"]
spotify_id = request.form["spotify_id"]
api_check = GMusicUsers.count_documents({"api" : api_key})
if api_check != 0:
# check that playlist is not already exist
user_id = GMusicUsers.find({"api" : api_key}, {"_id" : True})[0]['_id']
playlist_check = GMusicUsersData.count_documents({"user_id" : user_id, f"playlists.{playlist_name}" : {"$exists" : True}})
# edit playlist
if playlist_check == 1:
# check that spotify is available
spotify_check = GMusicDatabaseCollection.count_documents({"spotify_id" : spotify_id})
# spotify id is available
if spotify_check == 1:
GMusicUsersData.update_one({"user_id" : user_id}, {"$push" : {f"playlists.{playlist_name}" : spotify_id}})
response = Response(json.dumps({"status": False, "message": "Playlist edited"}), status=200, mimetype='application/json')
# spotify id is not valid
else:
response = Response(json.dumps({"status": False, "message": "Invalid spotify id"}), status=400, mimetype='application/json')
# playlist not exists
else:
response = Response(json.dumps({"status": False, "message": "Playlist already exists"}), status=400, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Invalid API"}), status=401, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Bad Request"}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
print(e)
response = Response(json.dumps({"status" : False, "message" : "Internal Server Error"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
elif request.method == "DELETE":
try:
if "api" in request.form:
api_key = request.form["api"]
# spotify_id = request.form["spotify_id"]
api_check = GMusicUsers.count_documents({"api" : api_key})
if api_check != 0:
# check that playlist is not already exist
user_id = GMusicUsers.find({"api" : api_key}, {"_id" : True})[0]['_id']
playlist_check = GMusicUsersData.count_documents({"user_id" : user_id, f"playlists.{playlist_name}" : {"$exists" : True}})
print(playlist_check)
# delete playlist
if playlist_check == 1:
# GMusicUsersData.update_one({"user_id" : user_id}, {"$pull" : {"playlists" : playlist_name}})
GMusicUsersData.find_one_and_delete({"user_id" : user_id}, {"playlists" : playlist_name})
# # check that spotify id is available
# spotify_check = GMusicUsersData.count_documents({"user_id" : user_id, "playlists" : {"$elemMatch" : {playlist_name : spotify_id}}})
# print(spotify_check)
# # spotify id is available
# if spotify_check == 1:
# GMusicUsersData.update_one({"user_id" : user_id}, {"$pull" : {f"playlists.{playlist_name}" : spotify_id}})
# response = Response(json.dumps({"status": False, "message": "Deleted Successfully"}), status=200, mimetype='application/json')
# # spotify id is not valid
# else:
# response = Response(json.dumps({"status": False, "message": "Invalid spotify id"}), status=400, mimetype='application/json')
response = Response(json.dumps({"status": False, "message": "Playlist deleted"}), status=200, mimetype='application/json')
# playlist not exists
else:
response = Response(json.dumps({"status": False, "message": "Playlist already exists"}), status=400, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Invalid API"}), status=401, mimetype='application/json')
else:
response = Response(json.dumps({"status": False, "message": "Bad Request"}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
print(e)
response = Response(json.dumps({"status" : False, "message" : "Internal Server Error"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
try:
# if "api" in request.args:
# pass
# else:
# response = Response(json.dumps({"status": False, "message": "Invalid API"}), status=401, mimetype='application/json')
# GMusicList = GMusicDatabaseCollection.find({'spotify_id' : playlist_name}, {'_id': False})
# Send Response
# response = Response(json.dumps("hehe"), status=200, mimetype='application/json')
# response = Response(json.dumps(GMusicList[0]), status=200, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except IndexError:
response = Response(json.dumps({"status": False, "message": "Song not available"}), status=404, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except ValueError:
response = Response(json.dumps({"status": False, "message": "Bad Request"}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
# print(e)
response = Response(json.dumps({"status" : False, "message" : "Internal Server Error"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
# @app.route('/addMusic', methods=['POST'])
# def addMusicFunc():
# if request.method == 'POST' and request.form.get('auth') == SETTING.AUTH:
# try:
# if request.form.get('spotify_id') != None:
# GMusicCheck = GMusicDatabaseCollection.count_documents({"spotify_id" : request.form.get('spotify_id')})
# if GMusicCheck == 0:
# newData = {
# "spotify_id": request.form.get('spotify_id'),
# "album": request.form.get('album'),
# "artist": request.form.get('artist').split(','),
# "name": request.form.get('name'),
# "music": request.form.get('music'),
# "poster": request.form.get('poster'),
# }
# MusicID = GMusicDatabaseCollection.insert_one(newData).inserted_id
# response = Response(json.dumps({"status": True, "id": str(MusicID), "message": "New Song has been added"}), status=200, mimetype='application/json')
# else:
# response = Response(json.dumps({"status": False, "message": "Music already available"}), status=404, mimetype='application/json')
# else:
# response = Response(json.dumps({"status": False, "message": "Invalid Music ID"}), status=500, mimetype='application/json')
# response.headers['Access-Control-Allow-Origin'] = '*'
# return response
# newData = {
# "spotify_id": request.form.get('spotify_id'),
# "album": request.form.get('album'),
# "artist": request.form.get('artist').split(','),
# # "duration": int(request.form.get('duration')),
# # "lyrics": request.form.get('lyrics'),
# "name": request.form.get('name'),
# "music": request.form.get('music'),
# "poster": request.form.get('poster'),
# }
# # GMusicDatabaseCollection.update_one({'_id': MusicID}, {'$set': {'music': str(MusicID), 'poster': str(MusicID), 'lyrics':str(MusicID)}})
# response.headers['Access-Control-Allow-Origin'] = '*'
# return response
# except:
# response = Response(json.dumps({"status": False, "message": "Error while adding data"}), status=500, mimetype='application/json')
# response.headers['Access-Control-Allow-Origin'] = '*'
# return response
# else:
# response = Response(json.dumps({"status": False, "message": "You are not authenticated"}), status=401, mimetype='application/json')
# response.headers['Access-Control-Allow-Origin'] = '*'
# return response
# @app.route('/deleteMusic', methods=['POST'])
# def deleteMusic():
# if request.method == 'POST' and request.form.get('auth') == SETTING.AUTH:
# try:
# print(request.form.get('doc_id'))
# GMusicDatabaseCollection.delete_one({"music" : request.form.get('doc_id')})
# response = Response(json.dumps({"status": True, "message": "Music Successfully Deleted"}), status=200, mimetype='application/json')
# response.headers['Access-Control-Allow-Origin'] = '*'
# return response
# except:
# response = Response(json.dumps({"status": False, "message": "Not able to delete"}), status=500, mimetype='application/json')
# response.headers['Access-Control-Allow-Origin'] = '*'
# return response
@app.route('/check', methods=['POST'])
def checkMusic():
if request.method == 'POST' and request.form.get('auth') == SETTING.AUTH:
try:
print(request.form.get('spotify_id'))
if request.form.get('spotify_id') != None:
GMusicCheck = GMusicDatabaseCollection.count_documents({"spotify_id" : request.form.get('spotify_id')})
if GMusicCheck == 0:
response = Response(json.dumps({"status": True, "message": "Music Not Available"}), status=404, mimetype='application/json')
else:
response = Response(json.dumps({"status": True, "message": "Music Available"}), status=200, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
else:
response = Response(json.dumps({"status": False, "message": "Invalid Music ID"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except:
response = Response(json.dumps({"status": False, "message": "Not able to delete"}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
# API Authentication
def api_unique(api_key):
api_check = GMusicUsers.count_documents({"api" : api_key})
if api_check != 0:
return api_unique(secrets.token_urlsafe(32))
else:
return api_key
@app.route(f'/{SETTING.VERSION}/api/login', methods=['POST'])
def login_func():
try:
if "email" in request.form and "password" in request.form:
email_check = GMusicUsers.count_documents({"email" : request.form['email'].lower()})
if email_check != 0:
user_api = GMusicUsers.find({"email" : request.form['email'].lower()}, {'_id' : False})[0]
if sha256_crypt.verify(request.form["password"], user_api["password"]):
response = Response(json.dumps({'success' : True, 'data' : {'email' : user_api['email'], 'api' : user_api['api'], "firstName" : user_api["firstName"], "lastName" : user_api["lastName"]}}), status=200, mimetype='application/json')
else:
response = Response(json.dumps({'success' : False, 'message' : 'Wrong password'}), status=401, mimetype='application/json')
else:
response = Response(json.dumps({'success' : False, 'message' : 'User not found'}), status=404, mimetype='application/json')
else:
response = Response(json.dumps({'success' : False, 'message' : 'Bad Request'}), status=400, mimetype='application/json')
# Send response
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
response = Response(json.dumps({'success' : False, 'message' : 'Internal Server Error'}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route(f'/{SETTING.VERSION}/api/signup', methods=['POST'])
def signup_func():
try:
if "email" in request.form and "password" in request.form and "confPassword" in request.form and "fname" in request.form and "lname" in request.form:
if request.form['password'] == request.form['confPassword']:
generated_api = api_unique(secrets.token_urlsafe(32))
email_check = GMusicUsers.count_documents({"email" : request.form['email'].lower()})
if email_check == 0:
new_user = {
"email" : request.form['email'].lower(),
"password" : sha256_crypt.hash(request.form['password']),
"firstName" : request.form['fname'],
"lastName" : request.form['lname'],
"ip" : request.remote_addr,
"country" : None,
"region" : None,
"city" : None,
"api" : generated_api,
"worker" : 0,
"admin" : 0,
"superuser" : 0
}
if SETTING.DEBUG == False:
ip_response = requests.get(f"{SETTING.IP_LOOKUP_WEBSITE}{request.environ['HTTP_X_FORWARDED_FOR']}").json()
if ip_response['status'] == "success":
new_user["ip"] = request.environ['HTTP_X_FORWARDED_FOR']
new_user["country"] = ip_response['country']
new_user["region"] = ip_response['regionName']
new_user["city"] = ip_response['city']
if "superuser" in request.form:
if request.form['superuser'] == SETTING.SUPER_USER:
new_user['admin'] = 1
new_user['superuser'] = 1
new_user['worker'] = 1
elif "admin" in request.form:
if request.form['admin'] == SETTING.ADMIN_USER:
new_user['admin'] = 1
new_user['worker'] = 1
elif "worker" in request.form:
if request.form['worker'] == SETTING.WORKER_USER:
new_user['worker'] = 1
user_id = GMusicUsers.insert_one(new_user).inserted_id
user_data = {
"user_id" : user_id,
"playlists" : {},
"private_music" : []
}
GMusicUsersData.insert_one(user_data)
response = Response(json.dumps({'success' : True, 'api' : generated_api}), status=201, mimetype='application/json')
else:
response = Response(json.dumps({'success' : False, 'message' : 'User exists'}), status=409, mimetype='application/json')
else:
response = Response(json.dumps({'success' : False, 'message' : 'Password not same'}), status=401, mimetype='application/json')
# Send response
response.headers['Access-Control-Allow-Origin'] = '*'
return response
else:
raise ValueError
except ValueError:
response = Response(json.dumps({'success' : False, 'message' : 'Bad Request'}), status=400, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
except Exception as e:
# print(e)
response = Response(json.dumps({'success' : False, 'message' : 'Internal Server Error'}), status=500, mimetype='application/json')
response.headers['Access-Control-Allow-Origin'] = '*'
return response
@app.route('/favicon.ico')
def favicon():
return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico',mimetype='image/x-icon')
if __name__ == "__main__":
app.run(debug=SETTING.DEBUG)