From 15992233842e6da3b2c1a22d0b318772332fa74a Mon Sep 17 00:00:00 2001 From: RemixDev Date: Tue, 15 Sep 2020 19:41:11 +0200 Subject: [PATCH] Fixed conversion for some spotify urls You need to remove spotifyCache.json in the config folder to use this --- deemix/app/queuemanager.py | 44 +++++++++++++++++++------------------ deemix/app/spotifyhelper.py | 36 ++++++++++++++++++++---------- 2 files changed, 47 insertions(+), 33 deletions(-) diff --git a/deemix/app/queuemanager.py b/deemix/app/queuemanager.py index 62745ce..6d6829e 100644 --- a/deemix/app/queuemanager.py +++ b/deemix/app/queuemanager.py @@ -21,7 +21,7 @@ class QueueManager: self.currentItem = "" self.sp = spotifyHelper - def generateQueueItem(self, dz, url, settings, bitrate=None, albumAPI=None, interface=None): + def generateQueueItem(self, dz, url, settings, bitrate=None, albumAPI=None, trackAPI=None, interface=None): forcedBitrate = getBitrateInt(bitrate) bitrate = forcedBitrate if forcedBitrate else settings['maxBitrate'] if 'deezer.page.link' in url: @@ -37,16 +37,16 @@ class QueueManager: elif type == "track": if id.startswith("isrc"): try: - trackAPI = dz.get_track(id) - if 'id' in trackAPI and 'title' in trackAPI: - id = trackAPI['id'] + isrcTest = dz.get_track(id) + if 'id' in isrcTest and 'title' in isrcTest: + id = isrcTest['id'] else: return QueueError(url, "Track ISRC is not available on deezer", "ISRCnotOnDeezer") except APIError as e: e = json.loads(str(e)) return QueueError(url, f"Wrong URL: {e['type']+': ' if 'type' in e else ''}{e['message'] if 'message' in e else ''}") try: - trackAPI = dz.get_track_gw(id) + trackAPI_gw = dz.get_track_gw(id) except APIError as e: e = json.loads(str(e)) message = "Wrong URL" @@ -54,25 +54,27 @@ class QueueManager: message += f": {e['DATA_ERROR']}" return QueueError(url, message) if albumAPI: - trackAPI['_EXTRA_ALBUM'] = albumAPI + trackAPI_gw['_EXTRA_ALBUM'] = albumAPI + if trackAPI: + trackAPI_gw['_EXTRA_TRACK'] = trackAPI if settings['createSingleFolder']: - trackAPI['FILENAME_TEMPLATE'] = settings['albumTracknameTemplate'] + trackAPI_gw['FILENAME_TEMPLATE'] = settings['albumTracknameTemplate'] else: - trackAPI['FILENAME_TEMPLATE'] = settings['tracknameTemplate'] - trackAPI['SINGLE_TRACK'] = True + trackAPI_gw['FILENAME_TEMPLATE'] = settings['tracknameTemplate'] + trackAPI_gw['SINGLE_TRACK'] = True - title = trackAPI['SNG_TITLE'] - if 'VERSION' in trackAPI and trackAPI['VERSION']: - title += " " + trackAPI['VERSION'] + title = trackAPI_gw['SNG_TITLE'] + if 'VERSION' in trackAPI_gw and trackAPI_gw['VERSION']: + title += " " + trackAPI_gw['VERSION'] return QISingle( id, bitrate, title, - trackAPI['ART_NAME'], - f"https://e-cdns-images.dzcdn.net/images/cover/{trackAPI['ALB_PICTURE']}/75x75-000000-80-0-0.jpg", + trackAPI_gw['ART_NAME'], + f"https://e-cdns-images.dzcdn.net/images/cover/{trackAPI_gw['ALB_PICTURE']}/75x75-000000-80-0-0.jpg", 'track', settings, - trackAPI, + trackAPI_gw, ) elif type == "album": @@ -88,7 +90,7 @@ class QueueManager: albumAPI['copyright'] = albumAPI_gw['COPYRIGHT'] if albumAPI['nb_tracks'] == 1: return self.generateQueueItem(dz, f"https://www.deezer.com/track/{albumAPI['tracks']['data'][0]['id']}", - settings, bitrate, albumAPI) + settings, bitrate, albumAPI=albumAPI, interface=interface) tracksArray = dz.get_album_tracks_gw(id) if albumAPI['nb_tracks'] == 255: albumAPI['nb_tracks'] = len(tracksArray) @@ -177,7 +179,7 @@ class QueueManager: artistAPITracks = dz.get_artist_albums(id) albumList = [] for album in artistAPITracks['data']: - albumList.append(self.generateQueueItem(dz, album['link'], settings, bitrate)) + albumList.append(self.generateQueueItem(dz, album['link'], settings, bitrate, interface=interface)) if interface: interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']}) @@ -199,7 +201,7 @@ class QueueManager: for type in artistDiscographyAPI: if type != 'all': for album in artistDiscographyAPI[type]: - albumList.append(self.generateQueueItem(dz, album['link'], settings, bitrate)) + albumList.append(self.generateQueueItem(dz, album['link'], settings, bitrate, interface=interface)) if interface: interface.send("finishAddingArtist", {'name': artistAPI['name'], 'id': artistAPI['id']}) @@ -276,14 +278,14 @@ class QueueManager: return QueueError(url, "Spotify Features is not setted up correctly.", "spotifyDisabled") try: - track_id = self.sp.get_trackid_spotify(dz, id, settings['fallbackSearch']) + (track_id, trackAPI, _) = 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, f'https://www.deezer.com/track/{track_id}', settings, bitrate) + return self.generateQueueItem(dz, f'https://www.deezer.com/track/{track_id}', settings, bitrate, trackAPI=trackAPI, interface=interface) else: logger.warn("Track not found on deezer!") return QueueError(url, "Track not found on deezer!", "trackNotOnDeezer") @@ -301,7 +303,7 @@ class QueueManager: return QueueError(url, "Something went wrong: "+str(e)) if album_id != "0": - return self.generateQueueItem(dz, f'https://www.deezer.com/album/{album_id}', settings, bitrate) + return self.generateQueueItem(dz, f'https://www.deezer.com/album/{album_id}', settings, bitrate, interface=interface) else: logger.warn("Album not found on deezer!") return QueueError(url, "Album not found on deezer!", "albumNotOnDeezer") diff --git a/deemix/app/spotifyhelper.py b/deemix/app/spotifyhelper.py index 0f5ca30..2a09d9f 100644 --- a/deemix/app/spotifyhelper.py +++ b/deemix/app/spotifyhelper.py @@ -132,27 +132,31 @@ class SpotifyHelper: else: cache = {'tracks': {}, 'albums': {}} if str(track_id) in cache['tracks']: - return cache['tracks'][str(track_id)] + dz_track = dz.get_track_by_ISRC(cache['tracks'][str(track_id)]['isrc']) + return (cache['tracks'][str(track_id)]['id'], dz_track, cache['tracks'][str(track_id)]['isrc']) singleTrack = True spotify_track = self.sp.track(track_id) else: spotify_track = spotifyTrack - dz_track = "0" + dz_id = "0" + dz_track = None + isrc = None 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_id = dz_track['id'] if 'id' in dz_track and 'title' in dz_track else "0" + isrc = spotify_track['external_ids']['isrc'] except: - dz_track = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], + dz_id = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], 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'], + dz_id = dz.get_track_from_metadata(spotify_track['artists'][0]['name'], spotify_track['name'], spotify_track['album']['name']) if singleTrack: - cache['tracks'][str(track_id)] = dz_track + cache['tracks'][str(track_id)] = {'id': dz_id, 'isrc': isrc} with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache: json.dump(cache, spotifyCache) - return dz_track + return (dz_id, dz_track, isrc) # Returns deezer album_id from spotify album_id def get_albumid_spotify(self, dz, album_id): @@ -164,20 +168,22 @@ class SpotifyHelper: else: cache = {'tracks': {}, 'albums': {}} if str(album_id) in cache['albums']: - return cache['albums'][str(album_id)] + return cache['albums'][str(album_id)]['id'] spotify_album = self.sp.album(album_id) dz_album = "0" + upc = None 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" + upc = spotify_album['external_ids']['upc'] 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" except: dz_album = "0" - cache['albums'][str(album_id)] = dz_album + cache['albums'][str(album_id)] = {'id': dz_album, 'upc': upc} with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache: json.dump(cache, spotifyCache) return dz_album @@ -240,10 +246,14 @@ class SpotifyHelper: if queueItem.cancel: return if str(track['id']) in cache['tracks']: - trackID = cache['tracks'][str(track['id'])] + trackID = cache['tracks'][str(track['id'])]['id'] + trackAPI = dz.get_track_by_ISRC(cache['tracks'][str(track['id'])]['isrc']) else: - trackID = self.get_trackid_spotify(dz, "0", queueItem.settings['fallbackSearch'], track) - cache['tracks'][str(track['id'])] = trackID + (trackID, trackAPI, isrc) = self.get_trackid_spotify(dz, "0", queueItem.settings['fallbackSearch'], track) + cache['tracks'][str(track['id'])] = { + 'id': trackID, + 'isrc': isrc + } if str(trackID) == "0": deezerTrack = { 'SNG_ID': "0", @@ -260,6 +270,8 @@ class SpotifyHelper: else: deezerTrack = dz.get_track_gw(trackID) deezerTrack['_EXTRA_PLAYLIST'] = queueItem.extra['playlistAPI'] + if trackAPI: + deezerTrack['_EXTRA_TRACK'] = trackAPI deezerTrack['POSITION'] = pos deezerTrack['SIZE'] = queueItem.size deezerTrack['FILENAME_TEMPLATE'] = queueItem.settings['playlistTracknameTemplate']