Polished some code

This commit is contained in:
RemixDev 2020-08-18 15:13:32 +02:00
parent 143d3f7dfd
commit 2a662928f9
7 changed files with 37 additions and 64 deletions

View File

@ -1,3 +1,3 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
__version__ = "1.2.5" __version__ = "1.2.6"

View File

@ -161,7 +161,7 @@ class DownloadJob:
# Log errors to file # Log errors to file
if 'error' in result: if 'error' in result:
if not 'data' in result['error']: if not 'data' in result['error']:
result['error']['data'] = {'id': 0, 'title': 'Unknown', 'artist': 'Unknown'} result['error']['data'] = {'id': "0", 'title': 'Unknown', 'artist': 'Unknown'}
errors += f"{result['error']['data']['id']} | {result['error']['data']['artist']} - {result['error']['data']['title']} | {result['error']['message']}\r\n" errors += f"{result['error']['data']['id']} | {result['error']['data']['artist']} - {result['error']['data']['title']} | {result['error']['message']}\r\n"
# Log searched to file # Log searched to file
if 'searched' in result: if 'searched' in result:
@ -228,7 +228,7 @@ class DownloadJob:
elif not track.searched and self.settings['fallbackSearch']: elif not track.searched and self.settings['fallbackSearch']:
logger.warn(f"[{track.mainArtist['name']} - {track.title}] Track not yet encoded, searching for alternative") logger.warn(f"[{track.mainArtist['name']} - {track.title}] Track not yet encoded, searching for alternative")
searchedId = self.dz.get_track_from_metadata(track.mainArtist['name'], track.title, track.album['title']) searchedId = self.dz.get_track_from_metadata(track.mainArtist['name'], track.title, track.album['title'])
if searchedId != 0: if searchedId != "0":
newTrack = self.dz.get_track_gw(searchedId) newTrack = self.dz.get_track_gw(searchedId)
track.parseEssentialData(self.dz, newTrack) track.parseEssentialData(self.dz, newTrack)
track.searched = True track.searched = True
@ -248,7 +248,7 @@ class DownloadJob:
elif not track.searched and self.settings['fallbackSearch']: elif not track.searched and self.settings['fallbackSearch']:
logger.warn(f"[{track.mainArtist['name']} - {track.title}] Track not found at desired bitrate, searching for alternative") logger.warn(f"[{track.mainArtist['name']} - {track.title}] Track not found at desired bitrate, searching for alternative")
searchedId = self.dz.get_track_from_metadata(track.mainArtist['name'], track.title, track.album['title']) searchedId = self.dz.get_track_from_metadata(track.mainArtist['name'], track.title, track.album['title'])
if searchedId != 0: if searchedId != "0":
newTrack = self.dz.get_track_gw(searchedId) newTrack = self.dz.get_track_gw(searchedId)
track.parseEssentialData(self.dz, newTrack) track.parseEssentialData(self.dz, newTrack)
track.searched = True track.searched = True
@ -437,7 +437,7 @@ class DownloadJob:
elif not track.searched and self.settings['fallbackSearch']: elif not track.searched and self.settings['fallbackSearch']:
logger.warn(f"[{track.mainArtist['name']} - {track.title}] Track not available, searching for alternative") logger.warn(f"[{track.mainArtist['name']} - {track.title}] Track not available, searching for alternative")
searchedId = self.dz.get_track_from_metadata(track.mainArtist['name'], track.title, track.album['title']) searchedId = self.dz.get_track_from_metadata(track.mainArtist['name'], track.title, track.album['title'])
if searchedId != 0: if searchedId != "0":
newTrack = self.dz.get_track_gw(searchedId) newTrack = self.dz.get_track_gw(searchedId)
track.parseEssentialData(self.dz, newTrack) track.parseEssentialData(self.dz, newTrack)
track.searched = True track.searched = True

View File

@ -14,9 +14,7 @@ class QueueItem:
self.failed = queueItemDict['failed'] self.failed = queueItemDict['failed']
self.errors = queueItemDict['errors'] self.errors = queueItemDict['errors']
self.progress = queueItemDict['progress'] self.progress = queueItemDict['progress']
self.settings = None self.settings = queueItemDict.get('settings')
if 'settings' in queueItemDict:
self.settings = queueItemDict['settings']
else: else:
self.title = title self.title = title
self.artist = artist self.artist = artist

View File

@ -139,7 +139,7 @@ class QueueManager:
totalSize = len(playlistTracksAPI) totalSize = len(playlistTracksAPI)
collection = [] collection = []
for pos, trackAPI in enumerate(playlistTracksAPI, start=1): for pos, trackAPI in enumerate(playlistTracksAPI, start=1):
if 'EXPLICIT_TRACK_CONTENT' in trackAPI and 'EXPLICIT_LYRICS_STATUS' in trackAPI['EXPLICIT_TRACK_CONTENT'] and trackAPI['EXPLICIT_TRACK_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4]: if 'EXPLICIT_TRACK_CONTENT' in trackAPI and trackAPI['EXPLICIT_TRACK_CONTENT'].get('EXPLICIT_LYRICS_STATUS') in [1,4]:
playlistAPI['explicit'] = True playlistAPI['explicit'] = True
trackAPI['_EXTRA_PLAYLIST'] = playlistAPI trackAPI['_EXTRA_PLAYLIST'] = playlistAPI
trackAPI['POSITION'] = pos trackAPI['POSITION'] = pos
@ -245,7 +245,7 @@ class QueueManager:
totalSize = len(artistTopTracksAPI_gw) totalSize = len(artistTopTracksAPI_gw)
collection = [] collection = []
for pos, trackAPI in enumerate(artistTopTracksAPI_gw, start=1): for pos, trackAPI in enumerate(artistTopTracksAPI_gw, start=1):
if 'EXPLICIT_TRACK_CONTENT' in trackAPI and 'EXPLICIT_LYRICS_STATUS' in trackAPI['EXPLICIT_TRACK_CONTENT'] and trackAPI['EXPLICIT_TRACK_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4]: if 'EXPLICIT_TRACK_CONTENT' in trackAPI and trackAPI['EXPLICIT_TRACK_CONTENT'].get('EXPLICIT_LYRICS_STATUS') in [1,4]:
playlistAPI['explicit'] = True playlistAPI['explicit'] = True
trackAPI['_EXTRA_PLAYLIST'] = playlistAPI trackAPI['_EXTRA_PLAYLIST'] = playlistAPI
trackAPI['POSITION'] = pos trackAPI['POSITION'] = pos
@ -277,7 +277,7 @@ class QueueManager:
except SpotifyException as e: except SpotifyException as e:
return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
if track_id != 0: if track_id != "0":
return self.generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate) return self.generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate)
else: else:
logger.warn("Track not found on deezer!") logger.warn("Track not found on deezer!")
@ -293,7 +293,7 @@ class QueueManager:
except SpotifyException as e: except SpotifyException as e:
return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:])
if album_id != 0: if album_id != "0":
return self.generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate) return self.generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate)
else: else:
logger.warn("Album not found on deezer!") logger.warn("Album not found on deezer!")

View File

@ -137,14 +137,14 @@ class SpotifyHelper:
spotify_track = self.sp.track(track_id) spotify_track = self.sp.track(track_id)
else: else:
spotify_track = spotifyTrack spotify_track = spotifyTrack
dz_track = 0 dz_track = "0"
if 'external_ids' in spotify_track and 'isrc' in spotify_track['external_ids']: if 'external_ids' in spotify_track and 'isrc' in spotify_track['external_ids']:
try: try:
dz_track = dz.get_track_by_ISRC(spotify_track['external_ids']['isrc']) dz_track = dz.get_track_by_ISRC(spotify_track['external_ids']['isrc'])
dz_track = dz_track['id'] if 'id' in dz_track and 'title' in dz_track else 0 dz_track = dz_track['id'] if 'id' in dz_track and 'title' in dz_track else "0"
except: except:
dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'],
spotify_track['album']['name']) if fallbackSearch else 0 spotify_track['album']['name']) if fallbackSearch else "0"
elif fallbackSearch: elif fallbackSearch:
dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'],
spotify_track['album']['name']) spotify_track['album']['name'])
@ -166,17 +166,17 @@ class SpotifyHelper:
if str(album_id) in cache['albums']: if str(album_id) in cache['albums']:
return cache['albums'][str(album_id)] return cache['albums'][str(album_id)]
spotify_album = self.sp.album(album_id) spotify_album = self.sp.album(album_id)
dz_album = 0 dz_album = "0"
if 'external_ids' in spotify_album and 'upc' in spotify_album['external_ids']: if 'external_ids' in spotify_album and 'upc' in spotify_album['external_ids']:
try: try:
dz_album = dz.get_album_by_UPC(spotify_album['external_ids']['upc']) dz_album = dz.get_album_by_UPC(spotify_album['external_ids']['upc'])
dz_album = dz_album['id'] if 'id' in dz_album else 0 dz_album = dz_album['id'] if 'id' in dz_album else "0"
except: except:
try: try:
dz_album = dz.get_album_by_UPC(int(spotify_album['external_ids']['upc'])) dz_album = dz.get_album_by_UPC(int(spotify_album['external_ids']['upc']))
dz_album = dz_album['id'] if 'id' in dz_album else 0 dz_album = dz_album['id'] if 'id' in dz_album else "0"
except: except:
dz_album = 0 dz_album = "0"
cache['albums'][str(album_id)] = dz_album cache['albums'][str(album_id)] = dz_album
with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache: with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache:
json.dump(cache, spotifyCache) json.dump(cache, spotifyCache)
@ -240,11 +240,11 @@ class SpotifyHelper:
if str(track['id']) in cache['tracks']: if str(track['id']) in cache['tracks']:
trackID = cache['tracks'][str(track['id'])] trackID = cache['tracks'][str(track['id'])]
else: else:
trackID = self.get_trackid_spotify(dz, 0, queueItem.settings['fallbackSearch'], track) trackID = self.get_trackid_spotify(dz, "0", queueItem.settings['fallbackSearch'], track)
cache['tracks'][str(track['id'])] = trackID cache['tracks'][str(track['id'])] = trackID
if trackID == 0: if trackID == "0":
deezerTrack = { deezerTrack = {
'SNG_ID': 0, 'SNG_ID': "0",
'SNG_TITLE': track['name'], 'SNG_TITLE': track['name'],
'DURATION': 0, 'DURATION': 0,
'MD5_ORIGIN': 0, 'MD5_ORIGIN': 0,

View File

@ -12,12 +12,10 @@ class Track:
self.parseEssentialData(dz, trackAPI_gw) self.parseEssentialData(dz, trackAPI_gw)
self.title = trackAPI_gw['SNG_TITLE'].strip() self.title = trackAPI_gw['SNG_TITLE'].strip()
if 'VERSION' in trackAPI_gw and trackAPI_gw['VERSION'] and not trackAPI_gw['VERSION'] in trackAPI_gw['SNG_TITLE']: if trackAPI_gw.get('VERSION') and not trackAPI_gw['VERSION'] in trackAPI_gw['SNG_TITLE']:
self.title += " " + trackAPI_gw['VERSION'].strip() self.title += " " + trackAPI_gw['VERSION'].strip()
self.position = None self.position = trackAPI_gw.get('POSITION')
if 'POSITION' in trackAPI_gw:
self.position = trackAPI_gw['POSITION']
self.localTrack = int(self.id) < 0 self.localTrack = int(self.id) < 0
if self.localTrack: if self.localTrack:
@ -103,10 +101,8 @@ class Track:
self.album = { self.album = {
'id': "0", 'id': "0",
'title': trackAPI_gw['ALB_TITLE'], 'title': trackAPI_gw['ALB_TITLE'],
'pic': ""
} }
if 'ALB_PICTURE' in trackAPI_gw: self.album['pic'] = trackAPI_gw.get('ALB_PICTURE')
self.album['pic'] = trackAPI_gw['ALB_PICTURE']
self.mainArtist = { self.mainArtist = {
'id': "0", 'id': "0",
'name': trackAPI_gw['ART_NAME'], 'name': trackAPI_gw['ART_NAME'],
@ -144,15 +140,9 @@ class Track:
self.trackNumber = "0" self.trackNumber = "0"
def parseData(self, dz, settings, trackAPI_gw, trackAPI, albumAPI_gw, albumAPI): def parseData(self, dz, settings, trackAPI_gw, trackAPI, albumAPI_gw, albumAPI):
self.discNumber = None self.discNumber = trackAPI_gw.get('DISK_NUMBER')
if 'DISK_NUMBER' in trackAPI_gw: self.explicit = trackAPI_gw.get('EXPLICIT_LYRICS')
self.discNumber = trackAPI_gw['DISK_NUMBER'] self.copyright = trackAPI_gw.get('COPYRIGHT')
self.explicit = None
if 'EXPLICIT_LYRICS' in trackAPI_gw:
self.explicit = trackAPI_gw['EXPLICIT_LYRICS']
self.copyright = None
if 'COPYRIGHT' in trackAPI_gw:
self.copyright = trackAPI_gw['COPYRIGHT']
self.replayGain = "" self.replayGain = ""
if 'GAIN' in trackAPI_gw: if 'GAIN' in trackAPI_gw:
self.replayGain = "{0:.2f} dB".format((float(trackAPI_gw['GAIN']) + 18.4) * -1) self.replayGain = "{0:.2f} dB".format((float(trackAPI_gw['GAIN']) + 18.4) * -1)
@ -161,18 +151,15 @@ class Track:
self.contributors = trackAPI_gw['SNG_CONTRIBUTORS'] self.contributors = trackAPI_gw['SNG_CONTRIBUTORS']
self.lyrics = { self.lyrics = {
'id': None, 'id': trackAPI_gw.get('LYRICS_ID'),
'unsync': None, 'unsync': None,
'sync': None 'sync': None
} }
if 'LYRICS_ID' in trackAPI_gw:
self.lyrics['id'] = trackAPI_gw['LYRICS_ID']
if not "LYRICS" in trackAPI_gw and int(self.lyrics['id']) != 0: if not "LYRICS" in trackAPI_gw and int(self.lyrics['id']) != 0:
logger.info(f"[{trackAPI_gw['ART_NAME']} - {self.title}] Getting lyrics") logger.info(f"[{trackAPI_gw['ART_NAME']} - {self.title}] Getting lyrics")
trackAPI_gw["LYRICS"] = dz.get_lyrics_gw(self.id) trackAPI_gw["LYRICS"] = dz.get_lyrics_gw(self.id)
if int(self.lyrics['id']) != 0: if int(self.lyrics['id']) != 0:
if "LYRICS_TEXT" in trackAPI_gw["LYRICS"]: self.lyrics['unsync'] = trackAPI_gw["LYRICS"].get("LYRICS_TEXT")
self.lyrics['unsync'] = trackAPI_gw["LYRICS"]["LYRICS_TEXT"]
if "LYRICS_SYNC_JSON" in trackAPI_gw["LYRICS"]: if "LYRICS_SYNC_JSON" in trackAPI_gw["LYRICS"]:
self.lyrics['sync'] = "" self.lyrics['sync'] = ""
lastTimestamp = "" lastTimestamp = ""
@ -187,10 +174,8 @@ class Track:
self.mainArtist = { self.mainArtist = {
'id': trackAPI_gw['ART_ID'], 'id': trackAPI_gw['ART_ID'],
'name': trackAPI_gw['ART_NAME'], 'name': trackAPI_gw['ART_NAME'],
'pic': None 'pic': trackAPI_gw.get('ART_PICTURE')
} }
if 'ART_PICTURE' in trackAPI_gw:
self.mainArtist['pic'] = trackAPI_gw['ART_PICTURE']
self.date = None self.date = None
if 'PHYSICAL_RELEASE_DATE' in trackAPI_gw: if 'PHYSICAL_RELEASE_DATE' in trackAPI_gw:
@ -203,15 +188,13 @@ class Track:
self.album = { self.album = {
'id': trackAPI_gw['ALB_ID'], 'id': trackAPI_gw['ALB_ID'],
'title': trackAPI_gw['ALB_TITLE'], 'title': trackAPI_gw['ALB_TITLE'],
'pic': None, 'pic': trackAPI_gw.get('ALB_PICTURE'),
'barcode': "Unknown", 'barcode': "Unknown",
'label': "Unknown", 'label': "Unknown",
'explicit': False, 'explicit': False,
'date': None, 'date': None,
'genre': [] 'genre': []
} }
if 'ALB_PICTURE' in trackAPI_gw:
self.album['pic'] = trackAPI_gw['ALB_PICTURE']
try: try:
# Try the public API first (as it has more data) # Try the public API first (as it has more data)
if not albumAPI: if not albumAPI:
@ -242,24 +225,17 @@ class Track:
self.album['trackTotal'] = albumAPI['nb_tracks'] self.album['trackTotal'] = albumAPI['nb_tracks']
self.album['recordType'] = albumAPI['record_type'] self.album['recordType'] = albumAPI['record_type']
if 'upc' in albumAPI: self.album['barcode'] = albumAPI.get('upc') or self.album['barcode']
self.album['barcode'] = albumAPI['upc'] self.album['label'] = albumAPI.get('label') or self.album['label']
if 'label' in albumAPI: self.album['explicit'] = bool(albumAPI.get('explicit_lyrics'))
self.album['label'] = albumAPI['label']
if 'explicit_lyrics' in albumAPI:
self.album['explicit'] = albumAPI['explicit_lyrics']
if 'release_date' in albumAPI: if 'release_date' in albumAPI:
self.album['date'] = { self.album['date'] = {
'day': albumAPI["release_date"][8:10], 'day': albumAPI["release_date"][8:10],
'month': albumAPI["release_date"][5:7], 'month': albumAPI["release_date"][5:7],
'year': albumAPI["release_date"][0:4] 'year': albumAPI["release_date"][0:4]
} }
self.album['discTotal'] = None self.album['discTotal'] = albumAPI.get('nb_disk')
if 'nb_disk' in albumAPI: self.copyright = albumAPI.get('copyright')
self.album['discTotal'] = albumAPI['nb_disk']
self.copyright = None
if 'copyright' in albumAPI:
self.copyright = albumAPI['copyright']
if not self.album['pic']: if not self.album['pic']:
self.album['pic'] = albumAPI['cover_small'][albumAPI['cover_small'].find('cover/') + 6:-24] self.album['pic'] = albumAPI['cover_small'][albumAPI['cover_small'].find('cover/') + 6:-24]
@ -284,8 +260,7 @@ class Track:
self.album['trackTotal'] = albumAPI_gw['NUMBER_TRACK'] self.album['trackTotal'] = albumAPI_gw['NUMBER_TRACK']
self.album['discTotal'] = albumAPI_gw['NUMBER_DISK'] self.album['discTotal'] = albumAPI_gw['NUMBER_DISK']
self.album['recordType'] = "Album" self.album['recordType'] = "Album"
if 'LABEL_NAME' in albumAPI_gw: self.album['label'] = albumAPI_gw.get('LABEL_NAME') or self.album['label']
self.album['label'] = albumAPI_gw['LABEL_NAME']
if 'EXPLICIT_ALBUM_CONTENT' in albumAPI_gw and 'EXPLICIT_LYRICS_STATUS' in albumAPI_gw['EXPLICIT_ALBUM_CONTENT']: if 'EXPLICIT_ALBUM_CONTENT' in albumAPI_gw and 'EXPLICIT_LYRICS_STATUS' in albumAPI_gw['EXPLICIT_ALBUM_CONTENT']:
self.album['explicit'] = albumAPI_gw['EXPLICIT_ALBUM_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4] self.album['explicit'] = albumAPI_gw['EXPLICIT_ALBUM_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4]
if not self.album['pic']: if not self.album['pic']:

View File

@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text()
setup( setup(
name="deemix", name="deemix",
version="1.2.5", version="1.2.6",
description="A barebone deezer downloader library", description="A barebone deezer downloader library",
long_description=README, long_description=README,
long_description_content_type="text/markdown", long_description_content_type="text/markdown",