From a991ad04ec55d5c064474bbb0d9963fbd6937a8f Mon Sep 17 00:00:00 2001 From: RemixDev Date: Fri, 14 Aug 2020 19:50:37 +0200 Subject: [PATCH] Moved spotify conversion outside creating queue item --- deemix/app/downloader.py | 5 ++++- deemix/app/queuemanager.py | 22 ++++++++----------- deemix/app/spotify.py | 45 +++++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 27 deletions(-) diff --git a/deemix/app/downloader.py b/deemix/app/downloader.py index 4f0e034..afd108e 100644 --- a/deemix/app/downloader.py +++ b/deemix/app/downloader.py @@ -947,12 +947,14 @@ def downloadTrackObj_wrap(dz, track, settings, bitrate, queueItem, interface): return result -def download(dz, queueItem, interface=None): +def download(dz, sp, queueItem, interface=None): global downloadPercentage, lastPercentage settings = queueItem['settings'] bitrate = queueItem['bitrate'] downloadPercentage = 0 lastPercentage = 0 + if '_EXTRA' in queueItem: + sp.convert_spotify_playlist(dz, queueItem, settings, interface=interface) if 'single' in queueItem: try: result = downloadTrackObj(dz, queueItem['single'], settings, bitrate, queueItem, interface=interface) @@ -988,6 +990,7 @@ def download(dz, queueItem, interface=None): interface.send("finishDownload", queueItem['uuid']) return { 'dz': dz, + 'sp': sp, 'interface': interface, 'download_path': download_path } diff --git a/deemix/app/queuemanager.py b/deemix/app/queuemanager.py index 9fde329..1bbda4c 100644 --- a/deemix/app/queuemanager.py +++ b/deemix/app/queuemanager.py @@ -53,10 +53,10 @@ def slimQueueItems(items): def slimQueueItem(item): light = item.copy() - if 'single' in light: - del light['single'] - if 'collection' in light: - del light['collection'] + propertiesToDelete = ['single', 'collection', 'unconverted', '_EXTRA'] + for property in propertiesToDelete: + if property in light: + del light[property] return light def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interface=None): @@ -377,18 +377,14 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf result['error'] = "Spotify Features is not setted up correctly." result['errid'] = "spotifyDisabled" return result - if interface: - interface.send("startConvertingSpotifyPlaylist", str(id)) try: - playlist = sp.convert_spotify_playlist(dz, id, settings) + playlist = sp.adapt_spotify_playlist(dz, id, settings) except SpotifyException as e: result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:] return result playlist['bitrate'] = bitrate playlist['uuid'] = f"{playlist['type']}_{id}_{bitrate}" result = playlist - if interface: - interface.send("finishConvertingSpotifyPlaylist", str(id)) else: logger.warn("URL not supported yet") result['error'] = "URL not supported yet" @@ -447,11 +443,11 @@ def addToQueue(dz, sp, url, settings, bitrate=None, interface=None): logger.info(f"[{queueItem['uuid']}] Added to queue.") queue.append(queueItem['uuid']) queueList[queueItem['uuid']] = queueItem - nextItem(dz, interface) + nextItem(dz, sp, interface) return True -def nextItem(dz, interface=None): +def nextItem(dz, sp, interface=None): global currentItem, queueList, queue if currentItem != "": return None @@ -463,7 +459,7 @@ def nextItem(dz, interface=None): if interface: interface.send("startDownload", currentItem) logger.info(f"[{currentItem}] Started downloading.") - result = download(dz, queueList[currentItem], interface) + result = download(dz, sp, queueList[currentItem], interface) callbackQueueDone(result) @@ -475,7 +471,7 @@ def callbackQueueDone(result): queueComplete.append(currentItem) logger.info(f"[{currentItem}] Finished downloading.") currentItem = "" - nextItem(result['dz'], result['interface']) + nextItem(result['dz'], result['sp'], result['interface']) def getQueue(): diff --git a/deemix/app/spotify.py b/deemix/app/spotify.py index 5542d74..5e2d2f4 100644 --- a/deemix/app/spotify.py +++ b/deemix/app/spotify.py @@ -174,7 +174,7 @@ class SpotifyHelper: json.dump(cache, spotifyCache) return dz_album - def convert_spotify_playlist(self, dz, playlist_id, settings): + def adapt_spotify_playlist(self, dz, playlist_id, settings): if not self.spotifyEnabled: raise spotifyFeaturesNotEnabled spotify_playlist = self.sp.playlist(playlist_id) @@ -199,21 +199,34 @@ class SpotifyHelper: playlistAPI['various_artist'] = dz.get_artist(5080) tracklistTmp = spotify_playlist['tracks']['items'] result['collection'] = [] - tracklist = [] + result['_EXTRA'] = {} + result['_EXTRA']['unconverted'] = [] while spotify_playlist['tracks']['next']: spotify_playlist['tracks'] = self.sp.next(spotify_playlist['tracks']) tracklistTmp += spotify_playlist['tracks']['items'] for item in tracklistTmp: if item['track']: - tracklist.append(item['track']) - totalSize = len(tracklist) + if item['track']['explicit']: + playlistAPI['explicit'] = True + result['_EXTRA']['unconverted'].append(item['track']) + totalSize = len(result['_EXTRA']['unconverted']) result['size'] = totalSize + if not 'explicit' in playlistAPI: + playlistAPI['explicit'] = False + result['_EXTRA']['playlistAPI'] = playlistAPI + return result + + def convert_spotify_playlist(self, dz, item, settings, interface=None): + convertPercentage = 0 + lastPercentage = 0 if path.isfile(path.join(self.configFolder, 'spotifyCache.json')): with open(path.join(self.configFolder, 'spotifyCache.json'), 'r') as spotifyCache: cache = json.load(spotifyCache) else: cache = {'tracks': {}, 'albums': {}} - for pos, track in enumerate(tracklist, start=1): + if interface: + interface.send("startConversion", item['uuid']) + for pos, track in enumerate(item['_EXTRA']['unconverted'], start=1): if str(track['id']) in cache['tracks']: trackID = cache['tracks'][str(track['id'])] else: @@ -234,18 +247,24 @@ class SpotifyHelper: } else: deezerTrack = dz.get_track_gw(trackID) - if 'EXPLICIT_LYRICS' in deezerTrack and deezerTrack['EXPLICIT_LYRICS'] == "1": - playlistAPI['explicit'] = True - deezerTrack['_EXTRA_PLAYLIST'] = playlistAPI + deezerTrack['_EXTRA_PLAYLIST'] = item['_EXTRA']['playlistAPI'] deezerTrack['POSITION'] = pos - deezerTrack['SIZE'] = totalSize + deezerTrack['SIZE'] = item['size'] deezerTrack['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate'] - result['collection'].append(deezerTrack) - if not 'explicit' in playlistAPI: - playlistAPI['explicit'] = False + item['collection'].append(deezerTrack) + + convertPercentage = (pos / item['size']) * 100 + print(convertPercentage) + if round(convertPercentage) != lastPercentage and round(convertPercentage) % 2 == 0: + lastPercentage = round(convertPercentage) + if interface: + interface.send("updateQueue", {'uuid': item['uuid'], 'conversion': lastPercentage}) + + del item['_EXTRA'] with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache: json.dump(cache, spotifyCache) - return result + if interface: + interface.send("startDownload", item['uuid']) def get_user_playlists(self, user): if not self.spotifyEnabled: