From 7f1a34f859a8a471e0efee5d4566d057a48e22ad Mon Sep 17 00:00:00 2001 From: RemixDev Date: Sat, 29 Feb 2020 21:22:44 +0100 Subject: [PATCH 1/2] Implemented save sync lyrics --- deemix/api/deezer.py | 16 +--------------- deemix/app/downloader.py | 13 ++++++++++--- 2 files changed, 11 insertions(+), 18 deletions(-) diff --git a/deemix/api/deezer.py b/deemix/api/deezer.py index 79bca07..82295d0 100755 --- a/deemix/api/deezer.py +++ b/deemix/api/deezer.py @@ -191,21 +191,7 @@ class Deezer: def get_lyrics_gw(self, sng_id): body = self.gw_api_call('song.getLyrics', {'sng_id': sng_id}) - lyr = { - 'unsyncLyrics': { - 'description': "", - 'lyrics': body["results"]["LYRICS_TEXT"] - }, - 'syncLyrics': "", - } - for i in range(len(body["results"]["LYRICS_SYNC_JSON"])): - if "lrc_timestamp" in body["results"]["LYRICS_SYNC_JSON"][i]: - lyr['syncLyrics'] += body["results"]["LYRICS_SYNC_JSON"][i]["lrc_timestamp"] + \ - body["results"]["LYRICS_SYNC_JSON"][i]["line"] + "\r\n" - elif i + 1 < len(body["results"]["LYRICS_SYNC_JSON"]): - lyr['syncLyrics'] += body["results"]["LYRICS_SYNC_JSON"][i + 1]["lrc_timestamp"] + \ - body["results"]["LYRICS_SYNC_JSON"][i]["line"] + "\r\n" - return lyr + return body["results"] def get_user_playlist(self, user_id): body = self.api_call('user/' + str(user_id) + '/playlists', {'limit': -1}) diff --git a/deemix/app/downloader.py b/deemix/app/downloader.py index d9a9275..f789a93 100644 --- a/deemix/app/downloader.py +++ b/deemix/app/downloader.py @@ -124,7 +124,9 @@ def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = No track['lyrics'] = {} if 'LYRICS_ID' in trackAPI_gw: track['lyrics']['id'] = trackAPI_gw['LYRICS_ID'] - if "LYRICS" in trackAPI_gw: + if not "LYRICS" in trackAPI_gw and int(track['lyrics']['id']) != 0: + trackAPI_gw["LYRICS"] = dz.get_lyrics_gw(track['id']) + if int(track['lyrics']['id']) != 0: if "LYRICS_TEXT" in trackAPI_gw["LYRICS"]: track['lyrics']['unsync'] = trackAPI_gw["LYRICS"]["LYRICS_TEXT"] if "LYRICS_SYNC_JSON" in trackAPI_gw["LYRICS"]: @@ -256,7 +258,7 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None track['album']['picUrl'] = "http://e-cdn-images.deezer.com/images/cover/{}/{}x{}-000000-80-0-0.{}".format(track['album']['pic'], settings['embeddedArtworkSize'], settings['embeddedArtworkSize'], 'png' if settings['PNGcovers'] else 'jpg') # Generate filename and filepath from metadata - filename = generateFilename(track, trackAPI, settings) + extensions[track['selectedFormat']] + filename = generateFilename(track, trackAPI, settings) (filepath, artistPath, coverPath, extrasPath) = generateFilepath(track, trackAPI, settings) # Download and cache coverart @@ -269,7 +271,12 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None track['album']['picPath'] = None makedirs(filepath, exist_ok=True) - writepath = os.path.join(filepath, filename) + writepath = os.path.join(filepath, filename + extensions[track['selectedFormat']]) + + # Save lyrics in lrc file + if settings['syncedLyrics'] and 'sync' in track['lyrics']: + with open(os.path.join(filepath, filename + '.lrc'), 'w') as f: + f.write(track['lyrics']['sync']) track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['selectedFormat']) with open(writepath, 'wb') as stream: From 3b8e8697b16fc94af72050b7873af2bdc6fbbcef Mon Sep 17 00:00:00 2001 From: RemixDev Date: Sat, 29 Feb 2020 21:42:21 +0100 Subject: [PATCH 2/2] Implemented save local artwork for album and artist --- deemix/app/downloader.py | 26 ++++++++++++++++++++++++-- deemix/utils/pathtemplates.py | 2 +- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/deemix/app/downloader.py b/deemix/app/downloader.py index f789a93..0230c7d 100644 --- a/deemix/app/downloader.py +++ b/deemix/app/downloader.py @@ -1,7 +1,7 @@ #!/usr/bin/env python3 from deemix.api.deezer import Deezer, APIError from deemix.utils.taggers import tagID3, tagFLAC -from deemix.utils.pathtemplates import generateFilename, generateFilepath +from deemix.utils.pathtemplates import generateFilename, generateFilepath, settingsRegexAlbum, settingsRegexArtist import os.path from os import makedirs from urllib.request import urlopen @@ -191,6 +191,8 @@ def getTrackData(trackAPI_gw, trackAPI = None, albumAPI_gw = None, albumAPI = No 'id': albumAPI_gw['ART_ID'], 'name': albumAPI_gw['ART_NAME'] } + artistAPI = dz.get_artist(track['album']['artist']['id']) + track['album']['artist']['pic'] = artistAPI['picture_small'][44:-24] track['album']['trackTotal'] = albumAPI_gw['NUMBER_TRACK'] track['album']['discTotal'] = albumAPI_gw['NUMBER_DISK'] track['album']['recordType'] = "Album" @@ -255,7 +257,7 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None track['selectedFormat'] = format track['selectedFilesize'] = filesize track['album']['bitrate'] = format - track['album']['picUrl'] = "http://e-cdn-images.deezer.com/images/cover/{}/{}x{}-000000-80-0-0.{}".format(track['album']['pic'], settings['embeddedArtworkSize'], settings['embeddedArtworkSize'], 'png' if settings['PNGcovers'] else 'jpg') + track['album']['picUrl'] = "https://e-cdn-images.deezer.com/images/cover/{}/{}x{}-000000-80-0-0.{}".format(track['album']['pic'], settings['embeddedArtworkSize'], settings['embeddedArtworkSize'], 'png' if settings['PNGcovers'] else 'jpg') # Generate filename and filepath from metadata filename = generateFilename(track, trackAPI, settings) @@ -278,6 +280,26 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None with open(os.path.join(filepath, filename + '.lrc'), 'w') as f: f.write(track['lyrics']['sync']) + # Save local album art + if coverPath: + track['album']['picPathLocal'] = os.path.join(coverPath, f"{settingsRegexAlbum(settings['coverImageTemplate'], track['album'], settings)}.{'png' if settings['PNGcovers'] else 'jpg'}") + if not os.path.isfile(track['album']['picPathLocal']): + with open(track['album']['picPathLocal'], 'wb') as f: + try: + f.write(urlopen(track['album']['picUrl'].replace(f"{settings['embeddedArtworkSize']}x{settings['embeddedArtworkSize']}", f"{settings['localArtworkSize']}x{settings['localArtworkSize']}")).read()) + except HTTPError: + track['album']['picPathLocal'] = None + # Save artist art + if artistPath: + track['album']['artist']['picUrl'] = "https://cdns-images.dzcdn.net/images/artist/{}/{}x{}-000000-80-0-0.{}".format(track['album']['artist']['pic'], settings['localArtworkSize'], settings['localArtworkSize'], 'png' if settings['PNGcovers'] else 'jpg') + track['album']['artist']['picPathLocal'] = os.path.join(artistPath, f"{settingsRegexArtist(settings['artistImageTemplate'], track['album']['artist'], settings)}.{'png' if settings['PNGcovers'] else 'jpg'}") + if not os.path.isfile(track['album']['artist']['picPathLocal']): + with open(track['album']['artist']['picPathLocal'], 'wb') as f: + try: + f.write(urlopen(track['album']['artist']['picUrl']).read()) + except HTTPError: + track['album']['artist']['picPathLocal'] = None + track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['selectedFormat']) with open(writepath, 'wb') as stream: try: diff --git a/deemix/utils/pathtemplates.py b/deemix/utils/pathtemplates.py index df8a275..843440a 100644 --- a/deemix/utils/pathtemplates.py +++ b/deemix/utils/pathtemplates.py @@ -69,7 +69,7 @@ def generateFilepath(track, trackAPI, settings): (settings['createArtistFolder'] and '_EXTRA_PLAYLIST' in trackAPI and settings['savePlaylistAsCompilation']) or (settings['createArtistFolder'] and '_EXTRA_PLAYLIST' in trackAPI and settings['createStructurePlaylist']) ): - if (track['id']<0 and not 'artist' in track['album']): + if (int(track['id'])<0 and not 'artist' in track['album']): track['album']['artist'] = track['mainArtist'] filepath += antiDot(settingsRegexArtist(settings['artistNameTemplate'], track['album']['artist'], settings)) + pathSep artistPath = filepath