/
explore.py
526 lines (417 loc) · 13.5 KB
/
explore.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
import os
import glob
import time
from urllib.parse import quote
from constants import whitelisted_extensions, defaults, playlist_filters, limits, error_codes
from mutagen import File
from mutagen.easyid3 import EasyID3
from mutagen.flac import FLAC
from mutagen.mp3 import MP3
from mutagen.oggvorbis import OggVorbis
from helpers import send_error
import youtube_dl
import random
import getpass
import shutil
class Explorer():
def __init__(self):
self.__default_path = defaults.path
self.__PLAYLIST_TYPE_ARTIST = "artist"
self.__PLAYLIST_TYPE_ALBUM = "album"
self.__PLAYLIST_TYPE_GENRE = "genre"
self.__PLAYLIST_TYPE_ALL = "all"
self.__UNDEFINED_PLAYLIST_TYPE = "Unknown"
f = playlist_filters
self.__ARTISTS_ALLOWED_FILTERS = [f.NO_FILTERING, f.ARTISTS_ONLY]
self.__ALBUMS_ALLOWED_FILTERS = [f.NO_FILTERING, f.ALBUMS_ONLY]
self.__GENRES_ALLOWED_FILTERS = [f.NO_FILTERING, f.GENRES_ONLY]
self.__UNDEFINED_ALLOWED_FILTERS = [f.NO_FILTERING, f.UNKNOWN_ONLY]
self.__ALL_ALLOWED_FILTERS = [f.INCLUDE_ALL]
self.__filters = [f.NO_FILTERING, f.ARTISTS_ONLY, f.ALBUMS_ONLY, f.GENRES_ONLY,
f.UNKNOWN_ONLY, f.INCLUDE_ALL]
self.ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}]
}
def getPathContent(self, path, metadata = False, sorting = 0):
if path == None:
return None
if not os.path.isdir(path):
return None
path = self.__path_leaf(path)
if not path.startswith(self.__default_path):
return None
directories_list = []
for directory in self.__get_immediate_subdirectories(path):
if directory[0] != ".":
directories_list.append({"relative" : directory,
"full" : os.path.join(path, directory)})
files_list = []
files = []
for fil in os.listdir(path):
if self.__isWhitelisted(fil):
files.append(os.path.join(path, fil))
for f in files:
basename = os.path.basename(f)
if not metadata:
files_list.append({
"relative" : basename,
"full" : f,
"simple" : os.path.splitext(basename)[0]
})
else:
files_list.append({
"relative" : basename,
"full" : f,
"simple" : os.path.splitext(basename)[0],
"metadata" : self.getMetadata(f)
})
upDir = os.path.dirname(path)
if path == self.__default_path:
upDir = None
if sorting == 1:
files_list = sorted(files_list, key = lambda k: k['relative'])
directories_list = sorted(directories_list, key = lambda k: k['relative'])
elif sorting == 3:
files_list = sorted(files_list, key = lambda k: k['relative'], reverse = True)
directories_list = sorted(directories_list, key = lambda k: k['relative'], reverse = True)
return {
"directory" : {
"path" : path,
"upDir" : upDir,
"directories" : directories_list,
"tracks" : files_list
}
}
def getAllTracks(self, initialPath, sort = 1, simple = False, local = False):
files_list = []
startTime = int(round(time.time() * 1000))
if not local:
for dirpath, dirnames, filenames in os.walk(initialPath):
for filename in [f for f in filenames if self.__isWhitelisted(f)]:
fullPath = os.path.join(dirpath, filename)
basename = os.path.basename(fullPath)
f = self.__getFile(fullPath)
if not simple:
artist = f['artist'][0] if 'artist' in f else None
album = f['album'][0] if 'album' in f else None
genre = f['genre'][0] if 'genre' in f else None
filesize = round(os.stat(fullPath).st_size/1000000, 2)
cover = self.__getCover(fullPath)
files_list.append({
"basename" : basename,
"full" : fullPath,
"simple" : os.path.splitext(basename)[0],
"artist" : artist,
"album" : album,
"genre" : genre,
"cover" : cover,
"filesize" : filesize,
"length" : round(f.info.length)
})
else:
files_list.append({
"basename" : basename,
"full" : fullPath,
"simple" : os.path.splitext(basename)[0],
"length" : round(f.info.length)
})
endTime = int(round(time.time() * 1000))
if endTime - startTime > limits.MAX_REQUEST_TIME * 1000:
return send_error(error_codes.REQUEST_TIMEOUT, "Request took too much time", False)
else:
for fil in os.listdir(initialPath):
if self.__isWhitelisted(fil):
fullPath = os.path.join(initialPath, fil)
f = self.__getFile(fullPath)
if not simple:
artist = f['artist'][0] if 'artist' in f else None
album = f['album'][0] if 'album' in f else None
genre = f['genre'][0] if 'genre' in f else None
cover = self.__getCover(fullPath)
filesize = round(os.stat(fullPath).st_size/1000000, 2)
files_list.append({
"basename" : fil,
"full" : fullPath,
"simple" : os.path.splitext(fil)[0],
"artist" : artist,
"album" : album,
"genre" : genre,
"cover" : cover,
"filesize" : filesize,
"length" : round(f.info.length)
})
else:
files_list.append({
"basename" : fil,
"full" : fullPath,
"simple" : os.path.splitext(fil)[0],
"length" : round(f.info.length)
})
return {
"tracks" : self.__sortTracks(files_list, sort)
}
#Getting playlists via id3 tags
def getAllPlaylists(self, initialPath, sort = 0, trackSort = 1, filt = [playlist_filters.NO_FILTERING], local = False):
#Apply filters like:
'''
- albums only
- artists only
- unknown only
- no unknown
'''
if len(filt) <= 0:
filt.append(playlist_filters.NO_FILTERING)
for i in range(len(filt) - 1):
if not filt[i] in self.__filters:
filt[i] = playlist_filters.NO_FILTERING
playlists = []
albums = []
artists = []
genres = []
response = self.getAllTracks(initialPath, trackSort, False, local = local)
if not 'code' in response:
tracks = response['tracks']
else:
return response
for track in tracks:
path = track['full']
f = self.__getFile(path)
artist = f['artist'][0] if 'artist' in f else None
album = f['album'][0] if 'album' in f else None
genre = f['genre'][0] if 'genre' in f else None
filesize = round(os.stat(path).st_size/1000000, 2)
if not artist in artists and artist != None:
artists.append(artist)
if not album in albums and album != None:
albums.append(album)
if not genre in genres and genre != None:
genres.append(genre)
trackInfo = {
"path" : track,
"artist" : artist,
"album" : album,
"genre" : genre,
"cover" : self.__getCover(path),
"filesize" : filesize
}
#By artist
if artist != None and self.__isAllowed(self.__PLAYLIST_TYPE_ARTIST, filt):
self.__addTrack(playlists, artist, trackInfo, self.__PLAYLIST_TYPE_ARTIST)
#By album
if album != None and self.__isAllowed(self.__PLAYLIST_TYPE_ALBUM, filt):
self.__addTrack(playlists, album, trackInfo, self.__PLAYLIST_TYPE_ALBUM)
#By genre
if genre != None and self.__isAllowed(self.__PLAYLIST_TYPE_GENRE, filt):
self.__addTrack(playlists, genre, trackInfo, self.__PLAYLIST_TYPE_GENRE)
#Unknown
if album == None and artist == None and genre == None and self.__isAllowed(self.__UNDEFINED_PLAYLIST_TYPE, filt):
self.__addTrack(playlists, self.__UNDEFINED_PLAYLIST_TYPE, trackInfo, self.__UNDEFINED_PLAYLIST_TYPE)
#all
if self.__isAllowed(self.__PLAYLIST_TYPE_ALL, filt):
self.__addTrack(playlists, self.__PLAYLIST_TYPE_ALL, trackInfo, self.__PLAYLIST_TYPE_ALL)
return {"playlists" : self.__sortPlaylists(playlists, sort)}
def getMetadata(self, path):
f = self.__getFile(path)
response = {}
filesize = round(os.stat(path).st_size/1000000, 2)
try:
artist = f['artist'][0] if 'artist' in f else None
album = f['album'][0] if 'album' in f else None
genre = f['genre'][0] if 'genre' in f else None
response = {
"path" : path,
"artist" : artist,
"album" : album,
"genre" : genre,
"length" : round(f.info.length),
"cover" : self.__getCover(path),
"filesize" : filesize
}
except:
response = {
"path" : path,
"artist" : None,
"album" : None,
"genre" : None,
"length" : None,
"cover" : None,
"filesize" : filesize
}
return response
def __get_immediate_subdirectories(self, a_dir):
return [name for name in os.listdir(a_dir)
if os.path.isdir(os.path.join(a_dir, name))]
def __path_leaf(self, path):
if path.endswith('/'):
return path[:-1]
return path
def __isWhitelisted(self, path):
for ext in whitelisted_extensions:
if os.path.splitext(path)[1] == ext:
return True
return False
#This method will find and create cover of an album on device
#It names album arts with this pattern : [Artist] - [Album].jpg
def __getCover(self, file_path):
path = self.__path_leaf(file_path)
if not os.path.isfile(path) or not self.__isWhitelisted(path):
return None
f = self.__getFile(path)
artist = f['artist'][0] if 'artist' in f else None
album = f['album'][0] if 'album' in f else None
f = File(path)
cover = None
#audio.pictures, audio['covr'] and audio['APIC:']
cover = f['covr'] if 'covr' in f else None
if cover == None:
cover = f['APIC:'] if 'APIC:' in f else None
try:
if cover == None:
cover = f.pictures[0] if len(f.pictures) > 0 else None
except:
pass
if cover == None:
return cover
dir_path = os.path.dirname(path)
if artist != None and album != None:
cover_filename = artist + " - " + album + ".jpg"
elif artist == None and album != None:
cover_filename = album + ".jpg"
elif artist != None and album == None:
cover_filename = artist + ".jpg"
else:
cover_filename = os.path.splitext(os.path.basename(path))[0] + "_cover.jpg"
end_path = dir_path + "/" + cover_filename
if not os.path.exists(end_path): #Don't recreate it every query. Do it only when there's no jpg cover
picture_data = cover.data
try:
with open(end_path, 'wb') as img:
img.write(picture_data)
except:
return end_path
return end_path
def __isAllowed(self, type, filters):
allowedFilters = self.__getFilteringTable(type)
if allowedFilters == None:
return False
allow = False
for filter in allowedFilters:
if filter in filters:
allow = True
return allow
def __getFilteringTable(self, type):
if type == self.__PLAYLIST_TYPE_ARTIST:
return self.__ARTISTS_ALLOWED_FILTERS
elif type == self.__PLAYLIST_TYPE_ALBUM:
return self.__ALBUMS_ALLOWED_FILTERS
elif type == self.__PLAYLIST_TYPE_GENRE:
return self.__GENRES_ALLOWED_FILTERS
elif type == self.__UNDEFINED_PLAYLIST_TYPE:
return self.__UNDEFINED_ALLOWED_FILTERS
elif type == self.__PLAYLIST_TYPE_ALL:
return self.__ALL_ALLOWED_FILTERS
return None
def __addTrack(self, playlists, name, info, typ):
playlistsExsists = False
track = info['path']
genre = info['genre']
artist = info['artist']
album = info['album']
trackCover = info['cover']
for index, playlist in enumerate(playlists):
if playlist['name'] == name and playlist['type'] == typ:
if playlist['cover'] == None and trackCover != None:
playlist['cover'] = trackCover
playlist['tracks'].append(track)
if typ == self.__UNDEFINED_PLAYLIST_TYPE: #Add cover to tracks from undefined playlist
playlist['tracks'][-1]['cover'] = trackCover
playlist['cover'] = None
playlistsExsists = True
break
if len(playlists) == 0 or not playlistsExsists:
if typ == self.__PLAYLIST_TYPE_GENRE:
playlist = {
"name" : name,
"type" : typ,
"cover" : trackCover,
"tracks" : [track]
}
elif typ == self.__PLAYLIST_TYPE_ALBUM:
playlist = {
"name" : name,
"artist" : artist,
"genre" : genre,
"type" : typ,
"cover" : trackCover,
"tracks" : [track]
}
else:
playlist = {
"name" : name,
"artist" : artist,
"album" : album,
"genre" : genre,
"type" : typ,
"cover" : trackCover,
"tracks" : [track]
}
if typ == self.__UNDEFINED_PLAYLIST_TYPE: #Add cover to tracks from undefined playlist
playlist['tracks'][-1]['cover'] = trackCover
playlist['cover'] = None
playlists.append(playlist)
return playlists
def __sortTracks(self, tracks, method):
if method == 1:
return sorted(tracks, key = lambda k: k['simple'])
elif method == 2:
return sorted(tracks, key = lambda k: k['length'], reverse = True)
elif method == 3:
return sorted(tracks, key = lambda k: k['simple'], reverse = True)
elif method == 4:
return sorted(tracks, key = lambda k: k['length'])
return tracks
def __sortPlaylists(self, playlists, method):
if method == 1:
return sorted(playlists, key = lambda k: k['name'])
elif method == 3:
return sorted(playlists, key = lambda k: k['name'], reverse = True)
return playlists
def __getFile(self, path):
try:
extension = os.path.splitext(os.path.basename(path))[1]
audio = None
if(extension == ".mp3"):
audio = MP3(path, ID3=EasyID3)
elif(extension == ".flac"):
audio = FLAC(path)
elif(extension == ".ogg"):
audio = OggVorbis(path)
if audio != None:
return audio
return None
except:
return None
def downloadYouTube(self, path, url):
username = getpass.getuser()
try:
os.mkdir("/home/" + username + "/temp")
except:
pass
os.chdir("/home/" + getpass.getuser() + "/temp")
dirname = str(random.getrandbits(128))
try:
os.mkdir(dirname)
except:
pass
os.chdir(dirname)
with youtube_dl.YoutubeDL(self.ydl_opts) as ydl:
ydl.download([url])
for fil in os.listdir():
shutil.move(fil, path + "/" + fil)
os.chdir("/home/" + username + "/temp")
shutil.rmtree(dirname)