From bd8a1948d41d63c492ae6b1b8a6c99050f7aba7b Mon Sep 17 00:00:00 2001 From: RemixDev Date: Thu, 11 Feb 2021 19:05:06 +0100 Subject: [PATCH] Started queuemanager refactoring --- deemix/app/__init__.py | 5 ++--- deemix/app/downloadjob.py | 15 +++++++-------- deemix/app/queuemanager.py | 34 ++++++++++++++++++++++++---------- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/deemix/app/__init__.py b/deemix/app/__init__.py index 9b628bb..225936f 100644 --- a/deemix/app/__init__.py +++ b/deemix/app/__init__.py @@ -6,7 +6,6 @@ from deemix.app.spotifyhelper import SpotifyHelper class deemix: def __init__(self, configFolder=None, overwriteDownloadFolder=None): self.set = Settings(configFolder, overwriteDownloadFolder=overwriteDownloadFolder) - self.dz = Deezer() - self.dz.set_accept_language(self.set.settings.get('tagsLanguage')) + self.dz = Deezer(self.set.settings.get('tagsLanguage')) self.sp = SpotifyHelper(configFolder) - self.qm = QueueManager(self.sp) + self.qm = QueueManager(self.dz, self.sp) diff --git a/deemix/app/downloadjob.py b/deemix/app/downloadjob.py index d436422..d001b46 100644 --- a/deemix/app/downloadjob.py +++ b/deemix/app/downloadjob.py @@ -1,6 +1,5 @@ import requests -get = requests.get -request_exception = requests.exceptions +from requests import get from concurrent.futures import ThreadPoolExecutor from time import sleep @@ -68,7 +67,7 @@ def downloadImage(url, path, overwrite=OverwriteOption.DONT_OVERWRITE): with open(path, 'wb') as f: f.write(image.content) return path - except request_exception.HTTPError: + except requests.exceptions.HTTPError: if 'cdns-images.dzcdn.net' in url: urlBase = url[:url.rfind("/")+1] pictureUrl = url[len(urlBase):] @@ -78,7 +77,7 @@ def downloadImage(url, path, overwrite=OverwriteOption.DONT_OVERWRITE): sleep(1) return downloadImage(urlBase+pictureUrl.replace(str(pictureSize)+"x"+str(pictureSize), '1200x1200'), path, overwrite) logger.error("Image not found: "+url) - except (request_exception.ConnectionError, request_exception.ChunkedEncodingError, u3SSLError) as e: + except (requests.exceptions.ConnectionError, requests.exceptions.ChunkedEncodingError, u3SSLError) as e: logger.error("Couldn't download Image, retrying in 5 seconds...: "+url+"\n") sleep(5) return downloadImage(url, path, overwrite) @@ -492,7 +491,7 @@ class DownloadJob: except DownloadCancelled: if writepath.is_file(): writepath.unlink() raise DownloadCancelled - except (request_exception.HTTPError, DownloadEmpty): + except (requests.exceptions.HTTPError, DownloadEmpty): if writepath.is_file(): writepath.unlink() if track.fallbackId != "0": logger.warn(f"[{track.mainArtist.name} - {track.title}] Track not available, using fallback id") @@ -523,7 +522,7 @@ class DownloadJob: raise DownloadFailed("notAvailableNoAlternative") else: raise DownloadFailed("notAvailable") - except (request_exception.ConnectionError, request_exception.ChunkedEncodingError) as e: + except (requests.exceptions.ConnectionError, requests.exceptions.ChunkedEncodingError) as e: if writepath.is_file(): writepath.unlink() logger.warn(f"[{track.mainArtist.name} - {track.title}] Error while downloading the track, trying again in 5s...") sleep(5) @@ -616,7 +615,7 @@ class DownloadJob: try: request.raise_for_status() return formatNumber - except request_exception.HTTPError: # if the format is not available, Deezer returns a 403 error + except requests.exceptions.HTTPError: # if the format is not available, Deezer returns a 403 error pass if not shouldFallback: raise PreferredBitrateNotFound @@ -680,7 +679,7 @@ class DownloadJob: except (SSLError, u3SSLError) as e: logger.info(f'{itemName} retrying from byte {chunkLength}') return self.streamTrack(stream, track, chunkLength) - except (request_exception.ConnectionError, requests.exceptions.ReadTimeout): + except (requests.exceptions.ConnectionError, requests.exceptions.ReadTimeout): sleep(2) return self.streamTrack(stream, track, start) diff --git a/deemix/app/queuemanager.py b/deemix/app/queuemanager.py index 0305945..cf2f9dc 100644 --- a/deemix/app/queuemanager.py +++ b/deemix/app/queuemanager.py @@ -1,8 +1,11 @@ from deemix.app.downloadjob import DownloadJob from deemix.utils import getIDFromLink, getTypeFromLink, getBitrateInt + +from deezer import Deezer from deezer.gw import APIError as gwAPIError, LyricsStatus from deezer.api import APIError from deezer.utils import map_user_playlist + from spotipy.exceptions import SpotifyException from deemix.app.queueitem import QueueItem, QISingle, QICollection, QIConvertable import logging @@ -16,14 +19,16 @@ logging.basicConfig(level=logging.INFO) logger = logging.getLogger('deemix') class QueueManager: - def __init__(self, spotifyHelper=None): + def __init__(self, deezerHelper=None, spotifyHelper=None): self.queue = [] self.queueList = {} self.queueComplete = [] self.currentItem = "" + self.dz = deezerHelper or Deezer() self.sp = spotifyHelper - def generateTrackQueueItem(self, dz, id, settings, bitrate, trackAPI=None, albumAPI=None): + def generateTrackQueueItem(self, id, settings, bitrate, trackAPI=None, albumAPI=None, dz=None): + if not dz: dz = self.dz # Check if is an isrc: url if str(id).startswith("isrc"): try: @@ -72,7 +77,8 @@ class QueueManager: single=trackAPI_gw, ) - def generateAlbumQueueItem(self, dz, id, settings, bitrate, rootArtist=None): + def generateAlbumQueueItem(self, id, settings, bitrate, rootArtist=None, dz=None): + if not dz: dz = self.dz # Get essential album info try: albumAPI = dz.api.get_album(id) @@ -125,7 +131,8 @@ class QueueManager: collection=collection, ) - def generatePlaylistQueueItem(self, dz, id, settings, bitrate): + def generatePlaylistQueueItem(self, id, settings, bitrate, dz=None): + if not dz: dz = self.dz # Get essential playlist info try: playlistAPI = dz.api.get_playlist(id) @@ -178,7 +185,8 @@ class QueueManager: collection=collection, ) - def generateArtistQueueItem(self, dz, id, settings, bitrate, interface=None): + def generateArtistQueueItem(self, id, settings, bitrate, dz=None, interface=None): + if not dz: dz = self.dz # Get essential artist info try: artistAPI = dz.api.get_artist(id) @@ -201,7 +209,8 @@ class QueueManager: if interface: interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']}) return albumList - def generateArtistDiscographyQueueItem(self, dz, id, settings, bitrate, interface=None): + def generateArtistDiscographyQueueItem(self, id, settings, bitrate, dz=None, interface=None): + if not dz: dz = self.dz # Get essential artist info try: artistAPI = dz.api.get_artist(id) @@ -225,7 +234,8 @@ class QueueManager: if interface: interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']}) return albumList - def generateArtistTopQueueItem(self, dz, id, settings, bitrate, interface=None): + def generateArtistTopQueueItem(self, id, settings, bitrate, dz=None, interface=None): + if not dz: dz = self.dz # Get essential artist info try: artistAPI = dz.api.get_artist(id) @@ -293,7 +303,8 @@ class QueueManager: collection=collection, ) - def generateQueueItem(self, dz, url, settings, bitrate=None, interface=None): + def generateQueueItem(self, url, settings, bitrate=None, dz=None, interface=None): + if not dz: dz = self.dz bitrate = getBitrateInt(bitrate) or settings['maxBitrate'] if 'deezer.page.link' in url: url = urlopen(url).url if 'link.tospotify.com' in url: url = urlopen(url).url @@ -359,7 +370,9 @@ class QueueManager: logger.warn("URL not supported yet") return QueueError(url, "URL not supported yet", "unsupportedURL") - def addToQueue(self, dz, url, settings, bitrate=None, interface=None, ack=None): + def addToQueue(self, url, settings, bitrate=None, dz=None, interface=None, ack=None): + if not dz: dz = self.dz + if not dz.logged_in: if interface: interface.send("loginNeededToDownload") return False @@ -430,7 +443,8 @@ class QueueManager: self.nextItem(dz, interface) return True - def nextItem(self, dz, interface=None): + def nextItem(self, dz=None, interface=None): + if not dz: dz = self.dz # Check that nothing is already downloading and # that the queue is not empty if self.currentItem != "": return None