Moved spotify conversion outside creating queue item

This commit is contained in:
RemixDev 2020-08-14 19:50:37 +02:00
parent 3f8d4d5ae9
commit a991ad04ec
3 changed files with 45 additions and 27 deletions

View File

@ -947,12 +947,14 @@ def downloadTrackObj_wrap(dz, track, settings, bitrate, queueItem, interface):
return result return result
def download(dz, queueItem, interface=None): def download(dz, sp, queueItem, interface=None):
global downloadPercentage, lastPercentage global downloadPercentage, lastPercentage
settings = queueItem['settings'] settings = queueItem['settings']
bitrate = queueItem['bitrate'] bitrate = queueItem['bitrate']
downloadPercentage = 0 downloadPercentage = 0
lastPercentage = 0 lastPercentage = 0
if '_EXTRA' in queueItem:
sp.convert_spotify_playlist(dz, queueItem, settings, interface=interface)
if 'single' in queueItem: if 'single' in queueItem:
try: try:
result = downloadTrackObj(dz, queueItem['single'], settings, bitrate, queueItem, interface=interface) 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']) interface.send("finishDownload", queueItem['uuid'])
return { return {
'dz': dz, 'dz': dz,
'sp': sp,
'interface': interface, 'interface': interface,
'download_path': download_path 'download_path': download_path
} }

View File

@ -53,10 +53,10 @@ def slimQueueItems(items):
def slimQueueItem(item): def slimQueueItem(item):
light = item.copy() light = item.copy()
if 'single' in light: propertiesToDelete = ['single', 'collection', 'unconverted', '_EXTRA']
del light['single'] for property in propertiesToDelete:
if 'collection' in light: if property in light:
del light['collection'] del light[property]
return light return light
def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interface=None): 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['error'] = "Spotify Features is not setted up correctly."
result['errid'] = "spotifyDisabled" result['errid'] = "spotifyDisabled"
return result return result
if interface:
interface.send("startConvertingSpotifyPlaylist", str(id))
try: try:
playlist = sp.convert_spotify_playlist(dz, id, settings) playlist = sp.adapt_spotify_playlist(dz, id, settings)
except SpotifyException as e: except SpotifyException as e:
result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:] result['error'] = "Wrong URL: "+e.msg[e.msg.find('\n')+2:]
return result return result
playlist['bitrate'] = bitrate playlist['bitrate'] = bitrate
playlist['uuid'] = f"{playlist['type']}_{id}_{bitrate}" playlist['uuid'] = f"{playlist['type']}_{id}_{bitrate}"
result = playlist result = playlist
if interface:
interface.send("finishConvertingSpotifyPlaylist", str(id))
else: else:
logger.warn("URL not supported yet") logger.warn("URL not supported yet")
result['error'] = "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.") logger.info(f"[{queueItem['uuid']}] Added to queue.")
queue.append(queueItem['uuid']) queue.append(queueItem['uuid'])
queueList[queueItem['uuid']] = queueItem queueList[queueItem['uuid']] = queueItem
nextItem(dz, interface) nextItem(dz, sp, interface)
return True return True
def nextItem(dz, interface=None): def nextItem(dz, sp, interface=None):
global currentItem, queueList, queue global currentItem, queueList, queue
if currentItem != "": if currentItem != "":
return None return None
@ -463,7 +459,7 @@ def nextItem(dz, interface=None):
if interface: if interface:
interface.send("startDownload", currentItem) interface.send("startDownload", currentItem)
logger.info(f"[{currentItem}] Started downloading.") logger.info(f"[{currentItem}] Started downloading.")
result = download(dz, queueList[currentItem], interface) result = download(dz, sp, queueList[currentItem], interface)
callbackQueueDone(result) callbackQueueDone(result)
@ -475,7 +471,7 @@ def callbackQueueDone(result):
queueComplete.append(currentItem) queueComplete.append(currentItem)
logger.info(f"[{currentItem}] Finished downloading.") logger.info(f"[{currentItem}] Finished downloading.")
currentItem = "" currentItem = ""
nextItem(result['dz'], result['interface']) nextItem(result['dz'], result['sp'], result['interface'])
def getQueue(): def getQueue():

View File

@ -174,7 +174,7 @@ class SpotifyHelper:
json.dump(cache, spotifyCache) json.dump(cache, spotifyCache)
return dz_album 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: if not self.spotifyEnabled:
raise spotifyFeaturesNotEnabled raise spotifyFeaturesNotEnabled
spotify_playlist = self.sp.playlist(playlist_id) spotify_playlist = self.sp.playlist(playlist_id)
@ -199,21 +199,34 @@ class SpotifyHelper:
playlistAPI['various_artist'] = dz.get_artist(5080) playlistAPI['various_artist'] = dz.get_artist(5080)
tracklistTmp = spotify_playlist['tracks']['items'] tracklistTmp = spotify_playlist['tracks']['items']
result['collection'] = [] result['collection'] = []
tracklist = [] result['_EXTRA'] = {}
result['_EXTRA']['unconverted'] = []
while spotify_playlist['tracks']['next']: while spotify_playlist['tracks']['next']:
spotify_playlist['tracks'] = self.sp.next(spotify_playlist['tracks']) spotify_playlist['tracks'] = self.sp.next(spotify_playlist['tracks'])
tracklistTmp += spotify_playlist['tracks']['items'] tracklistTmp += spotify_playlist['tracks']['items']
for item in tracklistTmp: for item in tracklistTmp:
if item['track']: if item['track']:
tracklist.append(item['track']) if item['track']['explicit']:
totalSize = len(tracklist) playlistAPI['explicit'] = True
result['_EXTRA']['unconverted'].append(item['track'])
totalSize = len(result['_EXTRA']['unconverted'])
result['size'] = totalSize 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')): if path.isfile(path.join(self.configFolder, 'spotifyCache.json')):
with open(path.join(self.configFolder, 'spotifyCache.json'), 'r') as spotifyCache: with open(path.join(self.configFolder, 'spotifyCache.json'), 'r') as spotifyCache:
cache = json.load(spotifyCache) cache = json.load(spotifyCache)
else: else:
cache = {'tracks': {}, 'albums': {}} 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']: if str(track['id']) in cache['tracks']:
trackID = cache['tracks'][str(track['id'])] trackID = cache['tracks'][str(track['id'])]
else: else:
@ -234,18 +247,24 @@ class SpotifyHelper:
} }
else: else:
deezerTrack = dz.get_track_gw(trackID) deezerTrack = dz.get_track_gw(trackID)
if 'EXPLICIT_LYRICS' in deezerTrack and deezerTrack['EXPLICIT_LYRICS'] == "1": deezerTrack['_EXTRA_PLAYLIST'] = item['_EXTRA']['playlistAPI']
playlistAPI['explicit'] = True
deezerTrack['_EXTRA_PLAYLIST'] = playlistAPI
deezerTrack['POSITION'] = pos deezerTrack['POSITION'] = pos
deezerTrack['SIZE'] = totalSize deezerTrack['SIZE'] = item['size']
deezerTrack['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate'] deezerTrack['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate']
result['collection'].append(deezerTrack) item['collection'].append(deezerTrack)
if not 'explicit' in playlistAPI:
playlistAPI['explicit'] = False 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: with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache:
json.dump(cache, spotifyCache) json.dump(cache, spotifyCache)
return result if interface:
interface.send("startDownload", item['uuid'])
def get_user_playlists(self, user): def get_user_playlists(self, user):
if not self.spotifyEnabled: if not self.spotifyEnabled: