/
application.py
738 lines (563 loc) · 22 KB
/
application.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
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
from flask import Flask
from flask import request
from flask import jsonify, send_from_directory
from werkzeug import secure_filename
from constants import error_codes, params
from helpers import send_error, send_state_track_message, check_path
from helpers import send_state_playlist_message, track_endevent, is_valid_file, is_valid_num
from helpers import check_boolean, check_string, check_integer, check_float, check_string_array, isNull
from helpers import check_int_array, translate_sorting_method, send_no_dir_error, allowed_file
from helpers import send_playlist_play_error, get_defaults, file_exsists, send_no_file_error, flush_stream
from threader import TrackThreader, PlaylistThreader
from settings import volumizer
from explore import Explorer
import os
import shutil
import threading
app = Flask(__name__)
trackThreader = TrackThreader()
playlistThreader = PlaylistThreader()
explorer = Explorer()
@app.route('/track/play', methods=['GET', 'POST'])
def play_track():
currentTrack = trackThreader.currentTrack()
trackPath = check_string(request, params.PATH)
terminate = check_boolean(request, params.TERMINATE)
if not file_exsists(trackPath) or not check_path(trackPath):
return send_no_file_error(trackPath)
if currentTrack == None:
return startTrack(trackPath)
else:
if terminate:
currentTrack.stop()
trackThreader.setTrack(None)
return startTrack(trackPath)
return send_error(error_codes.TRACK_ALREADY_PLAYING, "Track already exsists")
@app.route('/track/rewind', methods=['GET', 'POST'])
def rewind_track():
currentTrack = trackThreader.currentTrack()
destPos = check_integer(request, params.POSITION)
unpause = True
if destPos == None:
return send_error(error_codes.WRONG_PLAYBACK_POSITION, "Wrong playback position")
unpause = check_boolean(request, params.UNPAUSE)
if currentTrack == None:
return send_error(error_codes.NO_TRACK, "No track playing")
elif destPos == -1:
return send_error(error_codes.WRONG_PLAYBACK_POSITION, "No position specified")
elif destPos < 0 or destPos > round(currentTrack.getLength() / 1000):
return send_error(error_codes.WRONG_PLAYBACK_POSITION, "Wrong playback position. It should be specified with seconds")
else:
oldPos = currentTrack.playbackInfo().get('playback').get('position').get('millis')
total = currentTrack.playbackInfo().get('playback').get('total').get('millis')
currentTrack.setPlaybackPosition(destPos)
if unpause and currentTrack.isPaused():
currentTrack.unpause()
return send_state_track_message(currentTrack, "Playback position succesfully changed", {
'newPosition' : destPos * 1000,
'oldPosition' : oldPos,
'total' : total
})
@app.route('/track/pause')
def pause_track():
currentTrack = trackThreader.currentTrack()
if(currentTrack == None):
return send_error(error_codes.NO_TRACK, "No track playing")
elif(currentTrack.isPaused()):
return send_error(error_codes.TRACK_ALREADY_PAUSED, "Track is already paused")
else:
currentTrack.pause()
return send_state_track_message(currentTrack, "Track paused")
@app.route('/track/unpause')
def unpause_track():
currentTrack = trackThreader.currentTrack()
if currentTrack == None:
return send_error(error_codes.NO_TRACK, "No track playing")
elif not currentTrack.isPaused():
return send_error(error_codes.TRACK_ALREADY_PLAYING, "Track is already playing")
else:
currentTrack.unpause()
return send_state_track_message(currentTrack, "Track unpaused")
@app.route('/track/smartpause')
def smartpause_track():
currentTrack = trackThreader.currentTrack()
if currentTrack == None:
return send_error(error_codes.NO_TRACK, "No track playing")
if currentTrack.isPaused():
currentTrack.unpause()
return send_state_track_message(currentTrack, "Track unpaused")
else:
currentTrack.pause()
return send_state_track_message(currentTrack, "Track paused")
@app.route('/track/metadata', methods=['GET'])
def metadata_track():
currentTrack = trackThreader.currentTrack()
if currentTrack != None:
return jsonify(currentTrack.getMetadata())
return send_error(error_codes.NO_TRACK, "No track playing")
@app.route('/track/playback', methods=['GET'])
def playback_track():
currentTrack = trackThreader.currentTrack()
if(currentTrack != None):
response = currentTrack.playbackInfo()
response['code'] = error_codes.SUCCESFULL_QUERY
return jsonify(response)
return send_error(error_codes.NO_TRACK, "No track playing")
@app.route('/track/online', methods=['GET'])
def track_online():
currentTrack = trackThreader.currentTrack()
if(currentTrack != None):
response = currentTrack.playbackInfoExtended()
response['code'] = error_codes.SUCCESFULL_QUERY
return jsonify(response)
return send_error(error_codes.NO_TRACK, "No track playing")
@app.route('/track/alive')
def alive_track():
currentTrack = trackThreader.currentTrack()
alive = currentTrack != None
return jsonify({
"code" : error_codes.SUCCESFULL_QUERY,
"state" : alive
})
@app.route('/track/stop')
def stop_track():
currentTrack = trackThreader.currentTrack()
if currentTrack != None:
currentTrack.stop()
trackThreader.setTrack(None)
return jsonify({'message' : "Track stopped"})
else:
return send_error(error_codes.NO_TRACK, "No track playing")
# PLAYLIST SECTION
@app.route('/playlist/play', methods=['GET', 'POST'])
def play_playlist():
currentPlaylist = playlistThreader.currentPlaylist()
defaultPosition = check_integer(request, params.INDEX)
terminate = check_boolean(request, params.TERMINATE)
tracks = check_string_array(request, params.TRACK)
#http://localhost:5000/playlist/play?track=tracks/track5.mp3&track=tracks/track2.mp3&track=tracks/track5.mp3&i=0&t=True
if currentPlaylist == None:
return startPlaylist(tracks, defaultPosition)
else:
if terminate:
if currentPlaylist != None and currentPlaylist.currentTrack != None:
currentPlaylist.stop()
playlistThreader.setPlaylist(None)
playlistThreader.reInit()
return startPlaylist(tracks, defaultPosition)
return send_error(error_codes.PLAYLIST_EXSIST, "Playlist already exsist")
@app.route('/playlist/next')
def playlist_next():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
if currentPlaylist.nextTrackAvilable():
currentPlaylist.nextTrack(onPlaylistLoadError)
return send_state_playlist_message(currentPlaylist, "Track succesfully changed")
else:
return send_error(error_codes.NO_NEXT_TRACK, "No next track avilable")
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/prev')
def playlist_prev():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
if currentPlaylist.prevTrackAvilable():
currentPlaylist.previousTrack(onPlaylistLoadError)
return send_state_playlist_message(currentPlaylist, "Track succesfully changed")
else:
return send_error(error_codes.NO_PREV_TRACK, "No previous track avilable")
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/rewind', methods=['GET', 'POST'])
def playlist_rewind():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None:
currentTrack = currentPlaylist.currentTrack
destPos = check_integer(request, params.POSITION)
unpause = True
if destPos == None:
return send_error(error_codes.WRONG_PLAYBACK_POSITION, "Wrong playback position")
unpause = check_boolean(request, params.UNPAUSE)
if currentTrack == None:
return send_error(error_codes.NO_TRACK, "No track playing")
elif destPos == -1:
return send_error(error_codes.WRONG_PLAYBACK_POSITION, "No position specified")
elif destPos < 0 or destPos > round(currentTrack.getLength() / 1000):
return send_error(error_codes.WRONG_PLAYBACK_POSITION, "Wrong playback position. It should be specified with seconds")
else:
oldPos = currentTrack.playbackInfo().get('playback').get('position').get('secs')
total = currentTrack.playbackInfo().get('playback').get('total').get('secs')
currentTrack.setPlaybackPosition(destPos)
if unpause and currentTrack.isPaused():
currentTrack.unpause()
return send_state_playlist_message(currentPlaylist, "Playback position succesfully changed", {
'newPosition' : destPos,
'oldPosition' : oldPos,
'total' : total
})
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/pos', methods=['GET', 'POST'])
def playlist_pos():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
index = check_integer(request, params.INDEX)
if index == None or not is_valid_num(0, len(currentPlaylist.tracks) - 1, index):
return send_error(error_codes.INVALID_TRACK_INDEX, "Invalid track index")
else:
currentPlaylist.play(index)
return send_state_playlist_message(currentPlaylist, "Track succesfully changed")
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/playback')
def playlist_playback():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
return jsonify(currentPlaylist.playbackInfo(error_codes.SUCCESFULL_QUERY))
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/pause')
def playlist_pause():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
currentTrack = currentPlaylist.currentTrack
if(currentTrack == None):
return send_error(error_codes.NO_TRACK, "No track playing")
elif(currentTrack.isPaused()):
return send_error(error_codes.TRACK_ALREADY_PAUSED, "Track is already paused")
else:
currentTrack.pause()
return send_state_playlist_message(currentPlaylist, "Track paused")
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/unpause')
def playlist_unpause():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
currentTrack = currentPlaylist.currentTrack
if(currentTrack == None):
return send_error(error_codes.NO_TRACK, "No track playing")
elif not currentTrack.isPaused():
return send_error(error_codes.TRACK_ALREADY_PLAYING, "Track is already playing")
else:
currentTrack.unpause()
return send_state_playlist_message(currentPlaylist, "Track unpaused")
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/smartpause')
def playlist_smartpause():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
currentTrack = currentPlaylist.currentTrack
if(currentTrack == None):
return send_error(error_codes.NO_TRACK, "No track playing")
if currentTrack.isPaused():
currentTrack.unpause()
return send_state_playlist_message(currentPlaylist, "Track unpaused")
else:
currentTrack.pause()
return send_state_playlist_message(currentPlaylist, "Track paused")
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
@app.route('/playlist/alive')
def playlist_alive():
currentPlaylist = playlistThreader.currentPlaylist()
alive = currentPlaylist != None
return jsonify({
"code" : error_codes.SUCCESFULL_QUERY,
"state" : alive
})
@app.route('/playlist/stop')
def stop_playlist():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
currentPlaylist.stop()
playlistThreader.setPlaylist(None)
return jsonify({'message' : "Playlist stopped" })
else:
return send_error(error_codes.NO_PLAYLIST, "Playlist doesn't exsist")
#Config API actions
@app.route('/volume', methods = ['GET'])
def get_volume():
value = check_float(request, params.VALUE)
if value != None and value != "":
vol_response = volumizer.setVolume(value)
if vol_response != None:
vol = vol_response['config']['milli']
playlistThreader.setVolume(vol)
trackThreader.setVolume(vol)
respone = volumizer.getJSON()
respone.update({"code" : error_codes.SUCCESFULL_QUERY})
return jsonify(respone)
@app.route('/volume', methods = ['POST'])
def set_volume():
respone = volumizer.setVolume(check_float(request, params.VALUE))
if respone == None:
return send_error(error_codes.INVALID_VALUE, "Invalid volume value. Only 0 - 100")
respone.update({"code" : error_codes.SUCCESFULL_QUERY})
vol = respone['config']['milli']
playlistThreader.setVolume(vol)
trackThreader.setVolume(vol)
return jsonify(respone)
#Explore filesystem action
@app.route('/data', methods = ['GET', 'POST'])
def getDirectory():
path = check_string(request, params.PATH)
if path == None:
path = get_defaults()['defaults']['default_path']
metadata = check_boolean(request, params.METADATA)
sortingMethod = check_integer(request, params.SORT)
if sortingMethod == None:
sortingMethod = check_string(request, params.SORT)
sortingMethod = translate_sorting_method(sortingMethod, 0)
if check_path(path):
respone = explorer.getPathContent(path, metadata, sorting = sortingMethod)
if respone == None:
return send_error(error_codes.INVALID_PATH, "Invalid path")
else:
return send_error(error_codes.INVALID_PATH, "Invalid path")
respone['code'] = error_codes.SUCCESFULL_QUERY
return jsonify(respone)
#Gets all tracks on file system(may be unefficient)
@app.route('/all_tracks', methods = ['GET', 'POST'])
def getAllTracks():
simple = check_boolean(request, params.SIMPLE)
local = check_boolean(request, params.LOCAL)
initialPath = check_string(request, params.PATH)
sortingMethod = check_integer(request, params.SORT)
if sortingMethod == None:
sortingMethod = check_string(request, params.SORT)
sortingMethod = translate_sorting_method(sortingMethod, 1)
if not check_path(initialPath):
initialPath = None
if initialPath == None:
initialPath = get_defaults()["defaults"]["default_path"]
if not os.path.isdir(initialPath):
return send_error(error_codes.INVALID_PATH, "Invalid path")
respone = explorer.getAllTracks(initialPath, sortingMethod, simple, local)
if not 'error' in respone:
respone['code'] = error_codes.SUCCESFULL_QUERY
return jsonify(respone)
@app.route('/all_playlists', methods = ['GET', 'POST'])
def getAllPlaylists():
local = check_boolean(request, params.LOCAL)
sortingMethod = check_integer(request, params.SORT)
if sortingMethod == None:
sortingMethod = check_string(request, params.SORT)
sortingMethod = translate_sorting_method(sortingMethod)
trackSortingMethod = check_integer(request, params.TRACK_SORT)
if trackSortingMethod == None:
trackSortingMethod = check_string(request, params.TRACK_SORT)
trackSortingMethod = translate_sorting_method(trackSortingMethod, 1)
filters = check_int_array(request, params.FILTER)
if len(filters) == 0:
filters.append(0) #0 means no filtering
initialPath = check_string(request, params.PATH)
if not check_path(initialPath):
initialPath = None
if initialPath == None:
initialPath = get_defaults()["defaults"]["default_path"]
if not os.path.isdir(initialPath):
return send_error(error_codes.INVALID_PATH, "Invalid path")
respone = explorer.getAllPlaylists(initialPath, sortingMethod, trackSortingMethod, filters, local)
if not 'error' in respone:
respone['code'] = error_codes.SUCCESFULL_QUERY
return jsonify(respone)
@app.route('/metadata', methods = ['GET'])
def get_metadata():
path = check_string(request, params.PATH)
if path == None:
return send_error(error_codes.INVALID_PATH, "You need to specify path parameter")
if not file_exsists(path):
return send_no_file_error(path)
respone = explorer.getMetadata(path)
respone['code'] = error_codes.SUCCESFULL_QUERY
return jsonify(respone)
@app.route('/defaults', methods = ['GET'])
def defaults():
return jsonify(get_defaults())
#Flush every aduio stream
@app.route('/flush', methods = ['GET', 'POST'])
def flush():
flushTrack()
flushPlaylist()
return jsonify({
"code" : error_codes.SUCCESFULL_QUERY,
"message" : "flushed"
})
@app.route('/test', methods = ['GET'])
def test():
return jsonify(
{
"code" : error_codes.SUCCESFULL_QUERY,
"message" : "Seems to be working fine"
}
)
#Path for serving local files
@app.route('/file', methods = ['GET'])
def send_audio():
path = check_string(request, params.PATH)
if not check_path(path):
path = None
if path == None:
return send_error(error_codes.INVALID_PATH, "You need to specify path parameter")
if not file_exsists(path):
return send_no_file_error(path)
return send_from_directory(directory = os.path.dirname(path), filename = os.path.basename(path))
@app.route('/file/upload', methods = ['POST'])
def file_upload():
path = check_string(request, params.PATH)
if not check_path(path):
path = None
if path == None:
return send_error(error_codes.INVALID_PATH, "You need to specify path parameter")
if not os.path.isdir(path):
return send_no_dir_error(path)
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(path, filename)
file.save(file_path)
return jsonify({
"code" : error_codes.SUCCESFULL_QUERY,
"path" : file_path,
"message" : "File uploaded"
})
return jsonify({
"code" : error_codes.UNALLOWED_EXTENSION,
"message" : "Unallowed extension"
})
#Requres FFMPEG to be instaled on server
@app.route('/file/youtube', methods = ['GET'])
def youtube_upload():
path = check_string(request, params.PATH)
if not check_path(path):
path = None
if path == None:
return send_error(error_codes.INVALID_PATH, "You need to specify path parameter")
if not os.path.isdir(path):
return send_no_dir_error(path)
url = check_string(request, params.URL)
if url == None:
return send_error(error_codes.INVALID_URL, "You need to specify URL parameter")
threaded = check_boolean(request, params.THREADED)
if threaded:
thread = threading.Thread(target = explorer.downloadYouTube, args = (path, url))
thread.daemon = True
thread.start()
else:
explorer.downloadYouTube(path, url)
return jsonify({
"code" : error_codes.SUCCESFULL_QUERY,
"message" : "Query has been dispatched"
})
@app.route('/file/delete', methods = ['GET', 'POST'])
def file_delete():
path = check_string(request, params.PATH)
if not check_path(path):
path = None
if path == None:
return send_error(error_codes.INVALID_PATH, "You need to specify path parameter")
if not os.path.exists(path):
return send_no_file_error(path)
try:
if(os.path.isdir(path)):
shutil.rmtree(path)
else:
os.remove(path)
return jsonify({
"code" : error_codes.SUCCESFULL_QUERY,
"message" : "File/directory deleted"
})
except:
return jsonify({
"code" : error_codes.DATA_MANAGEMENT_ERROR,
"message" : "An error while deleting file has occured"
})
@app.route('/file/new_catalog', methods = ['GET', 'POST'])
def create_catalog():
path = check_string(request, params.PATH)
name = check_string(request, params.NAME)
if not check_path(path):
path = None
if path == None:
return send_error(error_codes.INVALID_PATH, "You need to specify path parameter")
if not os.path.isdir(path):
return send_no_dir_error(path)
if name == "":
return jsonify({
"code" : error_codes.INVALID_CATALOG_NAME,
"message" : "Invalid name"
})
final_path = os.path.join(path, name)
if os.path.isdir(final_path):
return jsonify({
"code" : error_codes.DATA_MANAGEMENT_ERROR,
"message" : "Folder already exsists."
})
try:
os.mkdir(final_path)
return jsonify({
"code" : error_codes.SUCCESFULL_QUERY,
"message" : "Folder succesfully created"
})
except:
return jsonify({
"code" : error_codes.DATA_MANAGEMENT_ERROR,
"message" : "An error has occured."
})
#Utility methods
def flushTrack():
currentTrack = trackThreader.currentTrack()
if currentTrack != None:
currentTrack.stop()
trackThreader.setTrack(None)
flush_stream()
def flushPlaylist():
currentPlaylist = playlistThreader.currentPlaylist()
if currentPlaylist != None and currentPlaylist.currentTrack != None:
currentPlaylist.stop()
playlistThreader.setPlaylist(None)
flush_stream()
def startTrack(trackPath):
#trackThreader.registerOnEndCustomCallback(track_endevent)
if(trackPath == "" or trackPath == None):
return send_error(error_codes.NO_PATH_DEFINED, "No path parameter passed.")
if not os.path.exists(trackPath) or not check_path(trackPath):
return send_error(error_codes.WRONG_PATH_SPECIFIED, "File doesn't exsist.")
if not is_valid_file(trackPath):
return send_error(error_codes.INVALID_TYPE, "Invalid filetype.")
flushPlaylist()
data = trackThreader.getThread(trackPath)
currentThread = data.get('thread')
currentTrack = data.get('track')
currentThread.start()
return send_state_track_message(currentTrack, "Track started")
def startPlaylist(tracks, defaultPosition = 0):
if defaultPosition != None and is_valid_num(0, len(tracks) - 1, defaultPosition):
data = playlistThreader.getThread(tracks, defaultPosition)
else:
data = playlistThreader.getThread(tracks)
if len(tracks) == 0:
return send_error(error_codes.NO_PATH_DEFINED, "No track parameter passed.")
errorPos = []
for index, trackPath in enumerate(tracks):
if not os.path.exists(trackPath) or not check_path(trackPath):
errorPos.append({"index" : index})
errorPos[-1].update(send_error(error_codes.WRONG_PATH_SPECIFIED, "File doesn't exsist.", False))
continue
if not is_valid_file(trackPath):
errorPos.append({"index" : index})
errorPos[-1].update(send_error(error_codes.INVALID_TYPE, "Invalid filetype.", False))
continue
if len(errorPos) > 0:
return send_playlist_play_error(errorPos, "Invalid track array")
flushTrack()
currentPlaylist = data.get('playlist')
playlistThreader.startThread()
return send_state_playlist_message(currentPlaylist, "Playlist succesfully started", None, 1, False)
def onPlaylistLoadError():
playlistThreader.setPlaylist(None)
return send_error(error_codes.INVALID_PATH, "Path error occured. Removing playlist...")
if __name__ == '__main__':
app.run(host = '0.0.0.0', debug=True, threaded = True)