-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
executable file
·380 lines (315 loc) · 12.9 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
import uuid
import pafy
from pymongo import MongoClient, errors
from flask import Flask, jsonify, request
from flask_cors import CORS
import constants
import last_fm
import top_chart
from cover_art_getter import last_fm_cover_art
from search import get_search_result
from youtube_list import youtube_search
# MONGO_URI = 'mongodb://localhost:27017'
MONGO_URI = 'mongodb://rud156:1234@ds227469.mlab.com:27469/euphonize'
pafy.set_api_key('AIzaSyCsrKjMf7_mHYrT6rIJ-oaA6KL5IYg389A')
APP = Flask(__name__)
CLIENT = MongoClient(MONGO_URI)
DB = CLIENT.euphonize
PLAYLIST = DB.playlist
CORS(APP)
@APP.route('/')
def index():
"""
Index Route
"""
return jsonify({'success': True, 'message': 'Hello World'})
@APP.route('/search', methods=['GET'])
def search():
"""
Get track search results
"""
search_query = request.args.get('search_query')
result = get_search_result(search_query)
if result is None:
return jsonify({'success': False, 'message': 'Unable to get the data'})
else:
return jsonify({'success': True, 'result': result})
@APP.route('/audio', methods=['GET'])
def get_video():
"""
Get MP3 link from track and artist names
"""
track_name = request.args.get('track')
artist_name = request.args.get('artist')
# Check validity of audio title format
if track_name is None or artist_name is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
audio_title = track_name + ' - ' + artist_name
if not constants.ARTIST_NAME.match(artist_name) or not constants.TRACK_NAME.match(track_name):
return jsonify({'success': False, 'message': 'Incorrect input string'})
request_youtube = audio_title + ' lyrics'
print('Request String Created: ' + request_youtube)
videos = youtube_search({'q': request_youtube, 'max_results': 20})
image, success = last_fm_cover_art(track_name, artist_name)
video = pafy.new(videos[0])
audio_stream = video.getbestaudio()
if success:
image_url = image
else:
image_url = video.thumb
data_set = {
'success': True,
'url': audio_stream.url,
'track_name': track_name,
'artist_name': artist_name,
'image': image_url
}
return jsonify({'success': True, 'track': data_set})
@APP.route('/get_playlist', methods=['GET'])
def get_playlist():
playlist_id = request.args.get('playlist_id')
try:
data = PLAYLIST.find_one({'playlist_id': playlist_id})
if data is None:
return jsonify({
'success': False,
'message': 'The playlist you requested does not exist'
})
return jsonify({'success': True, 'playlist': data['playlist']})
except errors.PyMongoError as exception:
print(exception)
return jsonify({
'success': False,
'message': 'An error occurred when retrieving the playlist. Please try again'
})
@APP.route('/generate_playlist_link', methods=['POST'])
def generate_playlist_link():
playlist_content = request.get_json()
unique_string = hex(int(uuid.uuid4().time_low))[2:]
try:
PLAYLIST.insert_one({
'playlist_id': unique_string,
'playlist': playlist_content
})
return jsonify({
'success': True,
'playlist_id': unique_string
})
except errors.PyMongoError as exception:
print(exception)
return jsonify({
'success': False,
'message': 'An error occurred when saving the playlist. Please try again'
})
@APP.route('/update_playlist', methods=['PUT'])
def update_playlist():
playlist_id = request.args.get('playlist_id')
playlist_content = request.get_json()
try:
PLAYLIST.update_one({'playlist_id': playlist_id}, {
'$set': {
'playlist': playlist_content
}
})
return jsonify({'success': True, 'message': 'Playlist updated successfully'})
except errors.PyMongoError as exception:
print(exception)
return jsonify({
'success': False,
'message': 'An error occurred when updating the playlist. Please try again'
})
@APP.route('/top_artists', methods=['GET'])
def get_top_artists():
"""
Get Top Artists
"""
data = request.args.get('type')
# Check for validity of incoming data
if data is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.MEDIA_DATA_TYPE.match(data):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
if data == 'user_cu':
artists = last_fm.top_artists()
if artists is None:
return jsonify({'success': False, 'message': 'Unable to fetch data. Please try again later'})
return jsonify({'success': True, 'artists': artists})
else:
artists = top_chart.top_artist()
if artists is None:
return jsonify({'success': False, 'message': 'Unable to fetch data. Please try again later'})
return jsonify({'success': True, 'artists': artists})
@APP.route('/top_tracks', methods=['GET'])
def get_top_tracks():
"""
Get Top Tracks
"""
data = request.args.get('type')
# Check for validity of incoming data
if data is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.MEDIA_DATA_TYPE.match(data):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
if data == 'user_cu':
tracks = last_fm.top_tracks()
if tracks is None:
return jsonify({'success': False, 'message': 'Unable to fetch data. Please try again later'})
return jsonify({'success': True, 'tracks': tracks})
else:
tracks = top_chart.top_chart()
if tracks is None:
return jsonify({'success': False, 'message': 'Unable to fetch data. Please try again later'})
return jsonify({'success': True, 'tracks': tracks})
@APP.route('/top_trending', methods=['GET'])
def top_trending():
"""
Get Top Trending Tracks
"""
data = top_chart.trending_chart()
if data is None:
return jsonify({'success': False, 'message': 'Unable to fetch data. Please try again later'})
return jsonify({'success': True, 'tracks': data})
@APP.route('/top_albums', methods=['GET'])
def get_top_albums():
"""
Get Top Albums
"""
albums = last_fm.top_albums()
if albums is None:
return jsonify({'success': False, 'message': 'Unable to fetch data. Please try again later'})
return jsonify({'success': True, 'albums': albums})
@APP.route('/emerging_artists', methods=['GET'])
def get_emerging_artists():
"""
Get Emerging Artists
"""
data = top_chart.emerging_artist()
if data is None:
return jsonify({'success': False, 'message': 'Unable to fetch data. Please try again later'})
return jsonify({'success': True, 'artists': data})
@APP.route('/artist_top', methods=['GET'])
def artist_top():
"""
Get artist top content
"""
artist = request.args.get('name')
data = request.args.get('type')
# Check for validity of incoming data
if data is None or artist is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.ARTIST_NAME.match(artist) or not constants.ARTIST_DATA_TYPE.match(data):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
if data == 'tracks':
return jsonify({'success': True, 'artist_tracks': last_fm.get_artist_top_tracks(artist)})
else:
return jsonify({'success': True, 'artist_albums': last_fm.get_artist_top_albums(artist)})
@APP.route('/popular_genre', methods=['GET'])
def popular_genre():
"""
Data for popular content
"""
data = request.args.get('type')
# Check for validity of incoming data
if data is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.POPULAR_DATA_TYPE.match(data):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
if data == 'tags':
pop_genre = last_fm.top_tags()
if pop_genre is None:
return jsonify({'success': False, 'message': 'Unable to fetch the data'})
return jsonify({'success': True, 'popular_genre': pop_genre})
elif data == 'albums':
tag_name = request.args.get('tag_name')
# Check validity of incoming data
if not constants.TAG_NAME.match(tag_name):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
albums = last_fm.get_albums_for_tags(tag_name, 25)
if albums is None:
return jsonify({'success': False, 'message': 'Unable to fetch the data'})
return jsonify({'success': True, 'albums': albums})
else:
tag_name = request.args.get('tag_name')
# Check validity of incoming data
if tag_name is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.TAG_NAME.match(tag_name):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
tracks = last_fm.get_tracks_for_tags(tag_name)
if tracks is None:
return jsonify({'success': False, 'message': 'Unable to fetch the data'})
return jsonify({'success': True, 'tracks': tracks})
@APP.route('/artist', methods=['GET'])
def get_artist():
"""
Get data related to artist
"""
artist_name = request.args.get('artist_name')
content = request.args.get('data_type')
# Check validity of incoming data
if artist_name is None or content is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.ARTIST_NAME.match(artist_name) or not constants.ARTIST_INFO.match(content):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
if content == 'info':
data = last_fm.get_artist_info(artist_name)
if data is None:
return jsonify({'success': False,
'message': 'I\'m sorry but \
we do not have enough information about the artist you requested'})
else:
return jsonify({'success': True, 'artist_data': data})
else:
similar_artists = last_fm.get_artist_similar_artists(artist_name)
if similar_artists is None:
return jsonify({'success': False, 'message': 'Incorrect or invalid artist name supplied'})
else:
return jsonify({'success': True, 'similar_artists': similar_artists})
@APP.route('/track', methods=['GET'])
def get_track():
"""
Get data related to track
"""
track_name = request.args.get('track_name')
artist_name = request.args.get('artist_name')
content = request.args.get('data_type')
# Check validity of incomming data
if artist_name is None or track_name is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.TRACK_NAME.match(track_name) or not constants.ARTIST_NAME.match(artist_name):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
if content == 'info':
data = last_fm.get_track_info(track_name, artist_name)
if data is None:
return jsonify({'success': False,
'message': 'I\'m sorry but we do not have enough information about the track you requested'})
else:
return jsonify({'success': True, 'track_data': data})
else:
data = last_fm.get_track_similar_tracks(track_name, artist_name)
if data is None:
return jsonify({'success': False,
'message': 'I\'m sorry but we do not have enough information about the track you requested'})
else:
return jsonify({'success': True, 'similar_tracks': data})
@APP.route('/album_info', methods=['GET'])
def get_album_info():
"""
Get data related to album
"""
album_name = request.args.get('album_name')
artist_name = request.args.get('artist_name')
# Check validity of incoming data
if artist_name is None or album_name is None:
return jsonify({'success': False, 'message': 'No parameters supplied'})
if not constants.ALBUM_NAME.match(album_name) or not constants.ARTIST_NAME.match(artist_name):
return jsonify({'success': False, 'message': 'Invalid parameters supplied'})
data = last_fm.get_album_info(album_name, artist_name)
if data is None:
return jsonify({'success': False,
'message': 'I\'m sorry but we do not have enough information about the album you requested'})
else:
return jsonify({'success': True, 'album_data': data})
if __name__ == '__main__':
APP.secret_key = 'someSecret'
APP.run(debug=True, threaded=True)