From e478e5b031238fb1a30e6267f5af7b94f0a397ef Mon Sep 17 00:00:00 2001 From: RemixDev Date: Tue, 23 Jun 2020 00:27:43 +0200 Subject: [PATCH] Added playlist cover download, even when not saved as compilation Added %explicit% to playlist folder template This fixes #41 --- deemix/app/downloader.py | 53 +++++++++++++++++++++++++---------- deemix/app/queuemanager.py | 4 +++ deemix/app/spotify.py | 4 +++ deemix/utils/pathtemplates.py | 10 ++++--- setup.py | 2 +- 5 files changed, 53 insertions(+), 20 deletions(-) diff --git a/deemix/app/downloader.py b/deemix/app/downloader.py index 6ced547..4741c13 100644 --- a/deemix/app/downloader.py +++ b/deemix/app/downloader.py @@ -593,38 +593,42 @@ def downloadTrackObj(dz, trackAPI, settings, bitrate, queueItem, extraTrack=None 'error': "Track is not available in Reality Audio 360."}) return result track['selectedFormat'] = format - if settings['tags']['savePlaylistAsCompilation'] and "_EXTRA_PLAYLIST" in trackAPI: + if "_EXTRA_PLAYLIST" in trackAPI: + track['playlist'] = {} if 'dzcdn.net' in trackAPI["_EXTRA_PLAYLIST"]['picture_small']: - track['album']['picUrl'] = trackAPI["_EXTRA_PLAYLIST"]['picture_small'][:-24] + "/{}x{}-{}".format( + track['playlist']['picUrl'] = trackAPI["_EXTRA_PLAYLIST"]['picture_small'][:-24] + "/{}x{}-{}".format( settings['embeddedArtworkSize'], settings['embeddedArtworkSize'], 'none-100-0-0.png' if settings['PNGcovers'] else f'000000-{settings["jpegImageQuality"]}-0-0.jpg') else: - track['album']['picUrl'] = trackAPI["_EXTRA_PLAYLIST"]['picture_xl'] - track['album']['title'] = trackAPI["_EXTRA_PLAYLIST"]['title'] - track['album']['mainArtist'] = { + track['playlist']['picUrl'] = trackAPI["_EXTRA_PLAYLIST"]['picture_xl'] + track['playlist']['title'] = trackAPI["_EXTRA_PLAYLIST"]['title'] + track['playlist']['mainArtist'] = { 'id': trackAPI["_EXTRA_PLAYLIST"]['various_artist']['id'], 'name': trackAPI["_EXTRA_PLAYLIST"]['various_artist']['name'], 'pic': trackAPI["_EXTRA_PLAYLIST"]['various_artist']['picture_small'][ trackAPI["_EXTRA_PLAYLIST"]['various_artist']['picture_small'].find('artist/') + 7:-24] } if settings['albumVariousArtists']: - track['album']['artist'] = {"Main": [trackAPI["_EXTRA_PLAYLIST"]['various_artist']['name'], ]} - track['album']['artists'] = [trackAPI["_EXTRA_PLAYLIST"]['various_artist']['name'], ] + track['playlist']['artist'] = {"Main": [trackAPI["_EXTRA_PLAYLIST"]['various_artist']['name'], ]} + track['playlist']['artists'] = [trackAPI["_EXTRA_PLAYLIST"]['various_artist']['name'], ] else: - track['album']['artist'] = {"Main": []} - track['album']['artists'] = [] + track['playlist']['artist'] = {"Main": []} + track['playlist']['artists'] = [] track['trackNumber'] = trackAPI["POSITION"] - track['album']['trackTotal'] = trackAPI["_EXTRA_PLAYLIST"]['nb_tracks'] - track['album']['recordType'] = "Compilation" - track['album']['barcode'] = "" - track['album']['label'] = "" - track['album']['date'] = { + track['playlist']['trackTotal'] = trackAPI["_EXTRA_PLAYLIST"]['nb_tracks'] + track['playlist']['recordType'] = "Compilation" + track['playlist']['barcode'] = "" + track['playlist']['label'] = "" + track['playlist']['explicit'] = trackAPI['_EXTRA_PLAYLIST']['explicit'] + track['playlist']['date'] = { 'day': trackAPI["_EXTRA_PLAYLIST"]["creation_date"][8:10], 'month': trackAPI["_EXTRA_PLAYLIST"]["creation_date"][5:7], 'year': trackAPI["_EXTRA_PLAYLIST"]["creation_date"][0:4] } track['discNumber'] = "1" - track['album']['discTotal'] = "1" + track['playlist']['discTotal'] = "1" + if settings['tags']['savePlaylistAsCompilation'] and "playlist" in track: + track['album'] = {**track['album'], **track['playlist']} else: if 'date' in track['album']: track['date'] = track['album']['date'] @@ -728,6 +732,18 @@ def downloadTrackObj(dz, trackAPI, settings, bitrate, queueItem, extraTrack=None if extrasPath: result['extrasPath'] = extrasPath result['playlistPosition'] = writepath[len(extrasPath):] + if "playlist" in track: + if 'dzcdn.net' in track['playlist']['picUrl']: + result['playlistURL'] = track['playlist']['picUrl'].replace( + f"{settings['embeddedArtworkSize']}x{settings['embeddedArtworkSize']}", + f"{settings['localArtworkSize']}x{settings['localArtworkSize']}") + else: + result['playlistURL'] = track['playlist']['picUrl'] + track['playlist']['id'] = "pl_" + str(trackAPI['_EXTRA_PLAYLIST']['id']) + track['playlist']['genre'] = ["Compilation", ] + track['playlist']['bitrate'] = format + track['playlist']['dateString'] = formatDate(track['playlist']['date'], settings['dateFormat']) + result['playlistCover'] = f"{settingsRegexAlbum(settings['coverImageTemplate'], track['playlist'], settings, trackAPI['_EXTRA_PLAYLIST'])}.{'png' if settings['PNGcovers'] else 'jpg'}" track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['selectedFormat']) @@ -895,6 +911,8 @@ def download(dz, queueItem, interface=None): def after_download(tracks, settings, queueItem): extrasPath = None playlist = [None] * len(tracks) + playlistCover = None + playlistURL = None errors = "" searched = "" for index in range(len(tracks)): @@ -909,6 +927,9 @@ def after_download(tracks, settings, queueItem): searched += result['searched'] + "\r\n" if not extrasPath and 'extrasPath' in result: extrasPath = result['extrasPath'] + if not playlistCover and 'playlistCover' in result: + playlistCover = result['playlistCover'] + playlistURL = result['playlistURL'] if settings['saveArtwork'] and 'albumPath' in result: downloadImage(result['albumURL'], result['albumPath'], settings['overwriteFile']) if settings['saveArtworkArtist'] and 'artistPath' in result: @@ -922,6 +943,8 @@ def after_download(tracks, settings, queueItem): if settings['logErrors'] and errors != "": with open(os.path.join(extrasPath, 'errors.txt'), 'wb') as f: f.write(errors.encode('utf-8')) + if settings['saveArtwork'] and playlistCover and not settings['tags']['savePlaylistAsCompilation']: + downloadImage(playlistURL, os.path.join(extrasPath, playlistCover), settings['overwriteFile']) if settings['logSearched'] and searched != "": with open(os.path.join(extrasPath, 'searched.txt'), 'wb') as f: f.write(searched.encode('utf-8')) diff --git a/deemix/app/queuemanager.py b/deemix/app/queuemanager.py index 2384b45..776c514 100644 --- a/deemix/app/queuemanager.py +++ b/deemix/app/queuemanager.py @@ -187,11 +187,15 @@ def generateQueueItem(dz, sp, url, settings, bitrate=None, albumAPI=None, interf totalSize = len(playlistTracksAPI) result['collection'] = [] for pos, trackAPI in enumerate(playlistTracksAPI, start=1): + if 'EXPLICIT_TRACK_CONTENT' in trackAPI and 'EXPLICIT_LYRICS_STATUS' in trackAPI['EXPLICIT_TRACK_CONTENT'] and trackAPI['EXPLICIT_TRACK_CONTENT']['EXPLICIT_LYRICS_STATUS'] in [1,4]: + playlistAPI['explicit'] = True trackAPI['_EXTRA_PLAYLIST'] = playlistAPI trackAPI['POSITION'] = pos trackAPI['SIZE'] = totalSize trackAPI['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate'] result['collection'].append(trackAPI) + if not 'explicit' in playlistAPI: + playlistAPI['explicit'] = False elif type == "artist": artistAPI = dz.get_artist(id) diff --git a/deemix/app/spotify.py b/deemix/app/spotify.py index b572b42..1fe687d 100644 --- a/deemix/app/spotify.py +++ b/deemix/app/spotify.py @@ -227,11 +227,15 @@ 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['POSITION'] = pos deezerTrack['SIZE'] = totalSize deezerTrack['FILENAME_TEMPLATE'] = settings['playlistTracknameTemplate'] result['collection'].append(deezerTrack) + if not 'explicit' in playlistAPI: + playlistAPI['explicit'] = False with open(path.join(self.configFolder, 'spotifyCache.json'), 'w') as spotifyCache: json.dump(cache, spotifyCache) return result diff --git a/deemix/utils/pathtemplates.py b/deemix/utils/pathtemplates.py index 6e47641..38aeafe 100644 --- a/deemix/utils/pathtemplates.py +++ b/deemix/utils/pathtemplates.py @@ -158,8 +158,13 @@ def settingsRegex(filename, track, settings, playlist=None): def settingsRegexAlbum(foldername, album, settings, playlist=None): if playlist and settings['tags']['savePlaylistAsCompilation']: foldername = foldername.replace("%album_id%", "pl_" + str(playlist['id'])) + foldername = foldername.replace("%genre%", "Compilation") else: foldername = foldername.replace("%album_id%", str(album['id'])) + if len(album['genre']) > 0: + foldername = foldername.replace("%genre%", fixName(album['genre'][0], settings['illegalCharacterReplacer'])) + else: + foldername = foldername.replace("%genre%", "Unknown") foldername = foldername.replace("%album%", fixName(album['title'], settings['illegalCharacterReplacer'])) foldername = foldername.replace("%artist%", fixName(album['mainArtist']['name'], settings['illegalCharacterReplacer'])) @@ -171,10 +176,6 @@ def settingsRegexAlbum(foldername, album, settings, playlist=None): foldername = foldername.replace("%upc%", album['barcode']) foldername = foldername.replace("%explicit%", "(Explicit)" if album['explicit'] else "") foldername = foldername.replace("%label%", fixName(album['label'], settings['illegalCharacterReplacer'])) - if len(album['genre']) > 0: - foldername = foldername.replace("%genre%", fixName(album['genre'][0], settings['illegalCharacterReplacer'])) - else: - foldername = foldername.replace("%genre%", "Unknown") foldername = foldername.replace("%year%", str(album['date']['year'])) foldername = foldername.replace("%date%", album['dateString']) foldername = foldername.replace("%bitrate%", bitrateLabels[int(album['bitrate'])]) @@ -198,6 +199,7 @@ def settingsRegexPlaylist(foldername, playlist, settings): foldername = foldername.replace("%owner_id%", str(playlist['creator']['id'])) foldername = foldername.replace("%year%", str(playlist['creation_date'][:4])) foldername = foldername.replace("%date%", str(playlist['creation_date'][:10])) + foldername = foldername.replace("%explicit%", "(Explicit)" if playlist['explicit'] else "") foldername = foldername.replace('\\', pathSep).replace('/', pathSep) return antiDot(fixLongName(foldername)) diff --git a/setup.py b/setup.py index 9bfc582..fa5908b 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ README = (HERE / "README.md").read_text() setup( name="deemix", - version="1.0.25", + version="1.0.26", description="A barebone deezer downloader library", long_description=README, long_description_content_type="text/markdown",