Added cover caching

This commit is contained in:
RemixDev 2020-02-27 18:37:40 +01:00
parent 03a3e29097
commit 0a7380110a
2 changed files with 23 additions and 11 deletions

View File

@ -4,9 +4,14 @@ from deemix.utils.taggers import tagID3, tagFLAC
from deemix.utils.pathtemplates import generateFilename, generateFilepath from deemix.utils.pathtemplates import generateFilename, generateFilepath
import os.path import os.path
from os import makedirs from os import makedirs
from urllib.request import urlopen
from urllib.error import HTTPError from urllib.error import HTTPError
from tempfile import gettempdir
dz = Deezer() dz = Deezer()
TEMPDIR = os.path.join(gettempdir(), 'deezloader-imgs')
if not os.path.isdir(TEMPDIR):
makedirs(TEMPDIR)
extensions = { extensions = {
9: '.flac', 9: '.flac',
@ -243,19 +248,26 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None
print('Downloading: {} - {}'.format(track['mainArtist']['name'], track['title'])) print('Downloading: {} - {}'.format(track['mainArtist']['name'], track['title']))
# Get the selected bitrate # Get the selected bitrate
if overwriteBitrate: bitrate = overwriteBitrate if overwriteBitrate else settings['maxBitrate']
bitrate = overwriteBitrate
else:
bitrate = settings['maxBitrate']
(format, filesize) = getPreferredBitrare(track['filesize'], bitrate) (format, filesize) = getPreferredBitrare(track['filesize'], bitrate)
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.jpg".format(track['album']['pic'], settings['embeddedArtworkSize'], settings['embeddedArtworkSize']) track['album']['picUrl'] = "http://e-cdn-images.deezer.com/images/cover/{}/{}x{}-000000-80-0-0.jpg".format(track['album']['pic'], settings['embeddedArtworkSize'], settings['embeddedArtworkSize'])
# Create the filename
# Generate filename and filepath from metadata
filename = generateFilename(track, trackAPI, settings) + extensions[track['selectedFormat']] filename = generateFilename(track, trackAPI, settings) + extensions[track['selectedFormat']]
(filepath, artistPath, coverPath, extrasPath) = generateFilepath(track, trackAPI, settings) (filepath, artistPath, coverPath, extrasPath) = generateFilepath(track, trackAPI, settings)
# Download and cache coverart
track['album']['picPath'] = os.path.join(TEMPDIR, f"alb{track['album']['id']}_{settings['embeddedArtworkSize']}.jpg")
if not os.path.isfile(track['album']['picPath']):
with open(track['album']['picPath'], 'wb') as f:
try:
f.write(urlopen(track['album']['picUrl']).read())
except HTTPError:
track['album']['picPath'] = None
makedirs(filepath, exist_ok=True) makedirs(filepath, exist_ok=True)
writepath = os.path.join(filepath, filename) writepath = os.path.join(filepath, filename)

View File

@ -1,6 +1,4 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from urllib.request import urlopen
from mutagen.flac import FLAC, Picture from mutagen.flac import FLAC, Picture
from mutagen.id3 import ID3, ID3NoHeaderError, TXXX, TIT2, TPE1, TALB, TPE2, TRCK, TPOS, TCON, TYER, TDAT, TLEN, TBPM, \ from mutagen.id3 import ID3, ID3NoHeaderError, TXXX, TIT2, TPE1, TALB, TPE2, TRCK, TPOS, TCON, TYER, TDAT, TLEN, TBPM, \
TPUB, TSRC, USLT, APIC, IPLS, TCOM, TCOP TPUB, TSRC, USLT, APIC, IPLS, TCOM, TCOP
@ -56,8 +54,9 @@ def tagID3(stream, track, save):
tag.add(IPLS(people=involved_people)) tag.add(IPLS(people=involved_people))
if save['copyright']: if save['copyright']:
tag.add(TCOP(text=track['copyright'])) tag.add(TCOP(text=track['copyright']))
if save['cover']: if save['cover'] and track['album']['picPath']:
tag.add(APIC(3, 'image/jpeg', 3, data=urlopen(track['album']['picUrl']).read())) with open(track['album']['picPath'], 'rb') as f:
tag.add(APIC(3, 'image/jpeg', 3, data=f.read()))
tag.save(stream, v1=2, v2_version=3, v23_sep=None) tag.save(stream, v1=2, v2_version=3, v23_sep=None)
@ -112,11 +111,12 @@ def tagFLAC(stream, track, save):
if save['copyright']: if save['copyright']:
tag["COPYRIGHT"] = track['copyright'] tag["COPYRIGHT"] = track['copyright']
if save['cover']: if save['cover'] and track['album']['picPath']:
image = Picture() image = Picture()
image.type = 3 image.type = 3
image.mime = 'image/jpeg' image.mime = 'image/jpeg'
image.data = urlopen(track['album']['picUrl']).read() with open(track['album']['picPath'], 'rb') as f:
image.data = f.read()
tag.add_picture(image) tag.add_picture(image)
tag.save(deleteid3=True) tag.save(deleteid3=True)