From 2a662928f93f558ad442f3d07ccc2f3f2773015a Mon Sep 17 00:00:00 2001 From: RemixDev Date: Tue, 18 Aug 2020 15:13:32 +0200 Subject: [PATCH] Polished some code --- deemix/__init__.py | 2 +- deemix/app/downloadjob.py | 8 +++--- deemix/app/queueitem.py | 4 +-- deemix/app/queuemanager.py | 8 +++--- deemix/app/spotifyhelper.py | 20 ++++++------- deemix/app/track.py | 57 +++++++++++-------------------------- setup.py | 2 +- 7 files changed, 37 insertions(+), 64 deletions(-) diff --git a/deemix/__init__.py b/deemix/__init__.py index b4a4399..5386532 100644 --- a/deemix/__init__.py +++ b/deemix/__init__.py @@ -1,3 +1,3 @@ #!/usr/bin/env python3 -__version__ = "1.2.5" +__version__ = "1.2.6" diff --git a/deemix/app/downloadjob.py b/deemix/app/downloadjob.py index 74cbf5e..cefc303 100644 --- a/deemix/app/downloadjob.py +++ b/deemix/app/downloadjob.py @@ -161,7 +161,7 @@ class DownloadJob: # Log errors to file if 'error' in result: 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" # Log searched to file if 'searched' in result: @@ -228,7 +228,7 @@ class DownloadJob: elif not track.searched and self.settings['fallbackSearch']: 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']) - if searchedId != 0: + if searchedId != "0": newTrack = self.dz.get_track_gw(searchedId) track.parseEssentialData(self.dz, newTrack) track.searched = True @@ -248,7 +248,7 @@ class DownloadJob: 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") 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) track.parseEssentialData(self.dz, newTrack) track.searched = True @@ -437,7 +437,7 @@ class DownloadJob: elif not track.searched and self.settings['fallbackSearch']: 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']) - if searchedId != 0: + if searchedId != "0": newTrack = self.dz.get_track_gw(searchedId) track.parseEssentialData(self.dz, newTrack) track.searched = True diff --git a/deemix/app/queueitem.py b/deemix/app/queueitem.py index 582bef0..90ef7bc 100644 --- a/deemix/app/queueitem.py +++ b/deemix/app/queueitem.py @@ -14,9 +14,7 @@ class QueueItem: self.failed = queueItemDict['failed'] self.errors = queueItemDict['errors'] self.progress = queueItemDict['progress'] - self.settings = None - if 'settings' in queueItemDict: - self.settings = queueItemDict['settings'] + self.settings = queueItemDict.get('settings') else: self.title = title self.artist = artist diff --git a/deemix/app/queuemanager.py b/deemix/app/queuemanager.py index e57edaf..7f498af 100644 --- a/deemix/app/queuemanager.py +++ b/deemix/app/queuemanager.py @@ -139,7 +139,7 @@ class QueueManager: totalSize = len(playlistTracksAPI) collection = [] 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 trackAPI['_EXTRA_PLAYLIST'] = playlistAPI trackAPI['POSITION'] = pos @@ -245,7 +245,7 @@ class QueueManager: totalSize = len(artistTopTracksAPI_gw) collection = [] 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 trackAPI['_EXTRA_PLAYLIST'] = playlistAPI trackAPI['POSITION'] = pos @@ -277,7 +277,7 @@ class QueueManager: except SpotifyException as e: 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) else: logger.warn("Track not found on deezer!") @@ -293,7 +293,7 @@ class QueueManager: except SpotifyException as e: 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) else: logger.warn("Album not found on deezer!") diff --git a/deemix/app/spotifyhelper.py b/deemix/app/spotifyhelper.py index 3f31eaf..260e0dc 100644 --- a/deemix/app/spotifyhelper.py +++ b/deemix/app/spotifyhelper.py @@ -137,14 +137,14 @@ class SpotifyHelper: spotify_track = self.sp.track(track_id) else: spotify_track = spotifyTrack - dz_track = 0 + dz_track = "0" if 'external_ids' in spotify_track and 'isrc' in spotify_track['external_ids']: try: 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: 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: dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], spotify_track['album']['name']) @@ -166,17 +166,17 @@ class SpotifyHelper: if str(album_id) in cache['albums']: return cache['albums'][str(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']: try: 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: try: 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: - dz_album = 0 + dz_album = "0" cache['albums'][str(album_id)] = dz_album with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache: json.dump(cache, spotifyCache) @@ -240,11 +240,11 @@ class SpotifyHelper: if str(track['id']) in cache['tracks']: trackID = cache['tracks'][str(track['id'])] 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 - if trackID == 0: + if trackID == "0": deezerTrack = { - 'SNG_ID': 0, + 'SNG_ID': "0", 'SNG_TITLE': track['name'], 'DURATION': 0, 'MD5_ORIGIN': 0, diff --git a/deemix/app/track.py b/deemix/app/track.py index c9826d6..0fbd866 100644 --- a/deemix/app/track.py +++ b/deemix/app/track.py @@ -12,12 +12,10 @@ class Track: self.parseEssentialData(dz, trackAPI_gw) 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.position = None - if 'POSITION' in trackAPI_gw: - self.position = trackAPI_gw['POSITION'] + self.position = trackAPI_gw.get('POSITION') self.localTrack = int(self.id) < 0 if self.localTrack: @@ -103,10 +101,8 @@ class Track: self.album = { 'id': "0", 'title': trackAPI_gw['ALB_TITLE'], - 'pic': "" } - if 'ALB_PICTURE' in trackAPI_gw: - self.album['pic'] = trackAPI_gw['ALB_PICTURE'] + self.album['pic'] = trackAPI_gw.get('ALB_PICTURE') self.mainArtist = { 'id': "0", 'name': trackAPI_gw['ART_NAME'], @@ -144,15 +140,9 @@ class Track: self.trackNumber = "0" def parseData(self, dz, settings, trackAPI_gw, trackAPI, albumAPI_gw, albumAPI): - self.discNumber = None - if 'DISK_NUMBER' in trackAPI_gw: - self.discNumber = trackAPI_gw['DISK_NUMBER'] - 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.discNumber = trackAPI_gw.get('DISK_NUMBER') + self.explicit = trackAPI_gw.get('EXPLICIT_LYRICS') + self.copyright = trackAPI_gw.get('COPYRIGHT') self.replayGain = "" if 'GAIN' in trackAPI_gw: 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.lyrics = { - 'id': None, + 'id': trackAPI_gw.get('LYRICS_ID'), 'unsync': 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: logger.info(f"[{trackAPI_gw['ART_NAME']} - {self.title}] Getting lyrics") trackAPI_gw["LYRICS"] = dz.get_lyrics_gw(self.id) if int(self.lyrics['id']) != 0: - if "LYRICS_TEXT" in trackAPI_gw["LYRICS"]: - self.lyrics['unsync'] = trackAPI_gw["LYRICS"]["LYRICS_TEXT"] + self.lyrics['unsync'] = trackAPI_gw["LYRICS"].get("LYRICS_TEXT") if "LYRICS_SYNC_JSON" in trackAPI_gw["LYRICS"]: self.lyrics['sync'] = "" lastTimestamp = "" @@ -187,10 +174,8 @@ class Track: self.mainArtist = { 'id': trackAPI_gw['ART_ID'], '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 if 'PHYSICAL_RELEASE_DATE' in trackAPI_gw: @@ -203,15 +188,13 @@ class Track: self.album = { 'id': trackAPI_gw['ALB_ID'], 'title': trackAPI_gw['ALB_TITLE'], - 'pic': None, + 'pic': trackAPI_gw.get('ALB_PICTURE'), 'barcode': "Unknown", 'label': "Unknown", 'explicit': False, 'date': None, 'genre': [] } - if 'ALB_PICTURE' in trackAPI_gw: - self.album['pic'] = trackAPI_gw['ALB_PICTURE'] try: # Try the public API first (as it has more data) if not albumAPI: @@ -242,24 +225,17 @@ class Track: self.album['trackTotal'] = albumAPI['nb_tracks'] self.album['recordType'] = albumAPI['record_type'] - if 'upc' in albumAPI: - self.album['barcode'] = albumAPI['upc'] - if 'label' in albumAPI: - self.album['label'] = albumAPI['label'] - if 'explicit_lyrics' in albumAPI: - self.album['explicit'] = albumAPI['explicit_lyrics'] + self.album['barcode'] = albumAPI.get('upc') or self.album['barcode'] + self.album['label'] = albumAPI.get('label') or self.album['label'] + self.album['explicit'] = bool(albumAPI.get('explicit_lyrics')) if 'release_date' in albumAPI: self.album['date'] = { 'day': albumAPI["release_date"][8:10], 'month': albumAPI["release_date"][5:7], 'year': albumAPI["release_date"][0:4] } - self.album['discTotal'] = None - if 'nb_disk' in albumAPI: - self.album['discTotal'] = albumAPI['nb_disk'] - self.copyright = None - if 'copyright' in albumAPI: - self.copyright = albumAPI['copyright'] + self.album['discTotal'] = albumAPI.get('nb_disk') + self.copyright = albumAPI.get('copyright') if not self.album['pic']: 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['discTotal'] = albumAPI_gw['NUMBER_DISK'] self.album['recordType'] = "Album" - if 'LABEL_NAME' in albumAPI_gw: - self.album['label'] = albumAPI_gw['LABEL_NAME'] + self.album['label'] = albumAPI_gw.get('LABEL_NAME') or self.album['label'] 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] if not self.album['pic']: diff --git a/setup.py b/setup.py index b0e82ce..b66de2e 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text() setup( name="deemix", - version="1.2.5", + version="1.2.6", description="A barebone deezer downloader library", long_description=README, long_description_content_type="text/markdown",