From d6106f30b78565769a45fe06f2a1e3d5285890e1 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Sat, 12 Sep 2020 13:08:28 +0200 Subject: [PATCH] Cleaned up some code --- deemix/__init__.py | 2 +- deemix/app/__init__.py | 2 +- deemix/app/downloadjob.py | 7 ++---- deemix/app/queuemanager.py | 49 +++++++++++++++++++++++-------------- deemix/app/spotifyhelper.py | 2 +- deemix/app/track.py | 14 +++++------ setup.py | 2 +- 7 files changed, 43 insertions(+), 35 deletions(-) diff --git a/deemix/__init__.py b/deemix/__init__.py index 4a87828..7ce90ee 100644 --- a/deemix/__init__.py +++ b/deemix/__init__.py @@ -1,3 +1,3 @@ #!/usr/bin/env python3 -__version__ = "1.2.22" +__version__ = "1.3.0" diff --git a/deemix/app/__init__.py b/deemix/app/__init__.py index d126811..d924e42 100644 --- a/deemix/app/__init__.py +++ b/deemix/app/__init__.py @@ -9,4 +9,4 @@ class deemix: self.set = Settings(configFolder) self.dz = Deezer() self.sp = SpotifyHelper(configFolder) - self.qm = QueueManager() + self.qm = QueueManager(self.sp) diff --git a/deemix/app/downloadjob.py b/deemix/app/downloadjob.py index cb6b02f..cac4fec 100644 --- a/deemix/app/downloadjob.py +++ b/deemix/app/downloadjob.py @@ -10,7 +10,7 @@ from os import makedirs, remove, system as execute from tempfile import gettempdir from time import sleep -from deemix.app.queueitem import QIConvertable, QISingle, QICollection +from deemix.app.queueitem import QISingle, QICollection from deemix.app.track import Track from deemix.utils.misc import changeCase from deemix.utils.pathtemplates import generateFilename, generateFilepath, settingsRegexAlbum, settingsRegexArtist, settingsRegexPlaylistFile @@ -91,12 +91,9 @@ def formatDate(date, template): return template class DownloadJob: - def __init__(self, dz, sp, queueItem, interface=None): + def __init__(self, dz, queueItem, interface=None): self.dz = dz - self.sp = sp self.interface = interface - if isinstance(queueItem, QIConvertable) and queueItem.extra: - self.sp.convert_spotify_playlist(self.dz, queueItem, interface=self.interface) self.queueItem = queueItem self.settings = queueItem.settings self.bitrate = queueItem.bitrate diff --git a/deemix/app/queuemanager.py b/deemix/app/queuemanager.py index ccf3753..da4bf4b 100644 --- a/deemix/app/queuemanager.py +++ b/deemix/app/queuemanager.py @@ -15,13 +15,14 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger('deemix') class QueueManager: - def __init__(self): + def __init__(self, spotifyHelper=None): self.queue = [] self.queueList = {} self.queueComplete = [] self.currentItem = "" + self.sp = spotifyHelper - def generateQueueItem(self, dz, sp, url, settings, bitrate=None, albumAPI=None, interface=None): + def generateQueueItem(self, dz, url, settings, bitrate=None, albumAPI=None, interface=None): forcedBitrate = getBitrateInt(bitrate) bitrate = forcedBitrate if forcedBitrate else settings['maxBitrate'] if 'deezer.page.link' in url: @@ -270,15 +271,17 @@ class QueueManager: collection, ) - elif type == "spotifytrack": - if not sp.spotifyEnabled: + elif type == "spotifytrack" and self.sp: + if not self.sp.spotifyEnabled: logger.warn("Spotify Features is not setted up correctly.") return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled") try: - track_id = sp.get_trackid_spotify(dz, id, settings['fallbackSearch']) + track_id = self.sp.get_trackid_spotify(dz, id, settings['fallbackSearch']) except SpotifyException as e: return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) + except Exception as e: + return QueueError(url, "Something went wrong: "+str(e)) if track_id != "0": return self.generateQueueItem(dz, sp, f'https://www.deezer.com/track/{track_id}', settings, bitrate) @@ -286,15 +289,17 @@ class QueueManager: logger.warn("Track not found on deezer!") return QueueError(url, "Track not found on deezer!", "trackNotOnDeezer") - elif type == "spotifyalbum": - if not sp.spotifyEnabled: + elif type == "spotifyalbum" and self.sp: + if not self.sp.spotifyEnabled: logger.warn("Spotify Features is not setted up correctly.") return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled") try: - album_id = sp.get_albumid_spotify(dz, id) + album_id = self.sp.get_albumid_spotify(dz, id) except SpotifyException as e: return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) + except Exception as e: + return QueueError(url, "Something went wrong: "+str(e)) if album_id != "0": return self.generateQueueItem(dz, sp, f'https://www.deezer.com/album/{album_id}', settings, bitrate) @@ -302,21 +307,23 @@ class QueueManager: logger.warn("Album not found on deezer!") return QueueError(url, "Album not found on deezer!", "albumNotOnDeezer") - elif type == "spotifyplaylist": - if not sp.spotifyEnabled: + elif type == "spotifyplaylist" and self.sp: + if not self.sp.spotifyEnabled: logger.warn("Spotify Features is not setted up correctly.") return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled") try: - return sp.generate_playlist_queueitem(dz, id, bitrate, settings) + return self.sp.generate_playlist_queueitem(dz, id, bitrate, settings) except SpotifyException as e: return QueueError(url, "Wrong URL: "+e.msg[e.msg.find('\n')+2:]) + except Exception as e: + return QueueError(url, "Something went wrong: "+str(e)) else: logger.warn("URL not supported yet") return QueueError(url, "URL not supported yet", "unsupportedURL") - def addToQueue(self, dz, sp, url, settings, bitrate=None, interface=None): + def addToQueue(self, dz, url, settings, bitrate=None, interface=None): if not dz.logged_in: if interface: interface.send("loginNeededToDownload") @@ -327,7 +334,7 @@ class QueueManager: if link == "": return False logger.info("Generating queue item for: "+link) - return self.generateQueueItem(dz, sp, link, settings, bitrate, interface=interface) + return self.generateQueueItem(dz, link, settings, bitrate, interface=interface) if type(url) is list: queueItem = [] @@ -381,10 +388,10 @@ class QueueManager: self.queue.append(queueItem.uuid) self.queueList[queueItem.uuid] = queueItem - self.nextItem(dz, sp, interface) + self.nextItem(dz, interface) return True - def nextItem(self, dz, sp, interface=None): + def nextItem(self, dz, interface=None): if self.currentItem != "": return None else: @@ -392,20 +399,24 @@ class QueueManager: self.currentItem = self.queue.pop(0) else: return None + if isinstance(self.queueList[self.currentItem], QIConvertable) and self.queueList[self.currentItem].extra: + logger.info(f"[{self.currentItem}] Converting tracks to deezer.") + self.sp.convert_spotify_playlist(dz, self.queueList[self.currentItem], interface=interface) + logger.info(f"[{self.currentItem}] Tracks converted.") if interface: interface.send("startDownload", self.currentItem) logger.info(f"[{self.currentItem}] Started downloading.") - DownloadJob(dz, sp, self.queueList[self.currentItem], interface).start() - self.afterDownload(dz, sp, interface) + DownloadJob(dz, self.queueList[self.currentItem], interface).start() + self.afterDownload(dz, interface) - def afterDownload(self, dz, sp, interface): + def afterDownload(self, dz, interface): if self.queueList[self.currentItem].cancel: del self.queueList[self.currentItem] else: self.queueComplete.append(self.currentItem) logger.info(f"[{self.currentItem}] Finished downloading.") self.currentItem = "" - self.nextItem(dz, sp, interface) + self.nextItem(dz, interface) def getQueue(self): diff --git a/deemix/app/spotifyhelper.py b/deemix/app/spotifyhelper.py index 17e934c..0f5ca30 100644 --- a/deemix/app/spotifyhelper.py +++ b/deemix/app/spotifyhelper.py @@ -6,7 +6,7 @@ from os import mkdir import spotipy from spotipy.oauth2 import SpotifyClientCredentials from deemix.utils.localpaths import getConfigFolder -from deemix.app.queueitem import QIConvertable, QICollection +from deemix.app.queueitem import QIConvertable emptyPlaylist = { 'collaborative': False, diff --git a/deemix/app/track.py b/deemix/app/track.py index 42de325..05999e6 100644 --- a/deemix/app/track.py +++ b/deemix/app/track.py @@ -142,7 +142,7 @@ class Track: def parseData(self, dz, settings, trackAPI_gw, trackAPI, albumAPI_gw, albumAPI): self.discNumber = trackAPI_gw.get('DISK_NUMBER') - self.explicit = bool(int(trackAPI_gw.get('EXPLICIT_LYRICS') or "0")) + self.explicit = bool(int(trackAPI_gw.get('EXPLICIT_LYRICS', "0"))) self.copyright = trackAPI_gw.get('COPYRIGHT') self.replayGain = "" if 'GAIN' in trackAPI_gw: @@ -152,7 +152,7 @@ class Track: self.contributors = trackAPI_gw['SNG_CONTRIBUTORS'] self.lyrics = { - 'id': trackAPI_gw.get('LYRICS_ID'), + 'id': trackAPI_gw.get('LYRICS_ID', "0"), 'unsync': None, 'sync': None, 'syncID3': None @@ -232,16 +232,16 @@ class Track: self.album['trackTotal'] = albumAPI['nb_tracks'] self.album['recordType'] = albumAPI['record_type'] - 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')) + self.album['barcode'] = albumAPI.get('upc', self.album['barcode']) + self.album['label'] = albumAPI.get('label', self.album['label']) + self.album['explicit'] = bool(albumAPI.get('explicit_lyrics', False)) 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'] = albumAPI.get('nb_disk') + self.album['discTotal'] = albumAPI.get('nb_disk', "1") self.copyright = albumAPI.get('copyright') if not self.album['pic']: @@ -267,7 +267,7 @@ class Track: self.album['trackTotal'] = albumAPI_gw['NUMBER_TRACK'] self.album['discTotal'] = albumAPI_gw['NUMBER_DISK'] self.album['recordType'] = "Album" - self.album['label'] = albumAPI_gw.get('LABEL_NAME') or self.album['label'] + self.album['label'] = albumAPI_gw.get('LABEL_NAME', 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 02112e7..342c821 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text() setup( name="deemix", - version="1.2.22", + version="1.3.0", description="A barebone deezer downloader library", long_description=README, long_description_content_type="text/markdown",