Implemented save local artwork for album and artist

This commit is contained in:
RemixDev 2020-02-29 21:42:21 +01:00
parent 7f1a34f859
commit 3b8e8697b1
2 changed files with 25 additions and 3 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from deemix.api.deezer import Deezer, APIError from deemix.api.deezer import Deezer, APIError
from deemix.utils.taggers import tagID3, tagFLAC 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 import os.path
from os import makedirs from os import makedirs
from urllib.request import urlopen 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'], 'id': albumAPI_gw['ART_ID'],
'name': albumAPI_gw['ART_NAME'] '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']['trackTotal'] = albumAPI_gw['NUMBER_TRACK']
track['album']['discTotal'] = albumAPI_gw['NUMBER_DISK'] track['album']['discTotal'] = albumAPI_gw['NUMBER_DISK']
track['album']['recordType'] = "Album" track['album']['recordType'] = "Album"
@ -255,7 +257,7 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None
track['selectedFormat'] = format track['selectedFormat'] = format
track['selectedFilesize'] = filesize track['selectedFilesize'] = filesize
track['album']['bitrate'] = format 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 # Generate filename and filepath from metadata
filename = generateFilename(track, trackAPI, settings) 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: with open(os.path.join(filepath, filename + '.lrc'), 'w') as f:
f.write(track['lyrics']['sync']) 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']) track['downloadUrl'] = dz.get_track_stream_url(track['id'], track['MD5'], track['mediaVersion'], track['selectedFormat'])
with open(writepath, 'wb') as stream: with open(writepath, 'wb') as stream:
try: try:

View File

@ -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['savePlaylistAsCompilation']) or
(settings['createArtistFolder'] and '_EXTRA_PLAYLIST' in trackAPI and settings['createStructurePlaylist']) (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'] track['album']['artist'] = track['mainArtist']
filepath += antiDot(settingsRegexArtist(settings['artistNameTemplate'], track['album']['artist'], settings)) + pathSep filepath += antiDot(settingsRegexArtist(settings['artistNameTemplate'], track['album']['artist'], settings)) + pathSep
artistPath = filepath artistPath = filepath