From 0a7380110aca8c753d4534bf1aef4dbee4ac06a2 Mon Sep 17 00:00:00 2001 From: RemixDev Date: Thu, 27 Feb 2020 18:37:40 +0100 Subject: [PATCH] Added cover caching --- deemix/app/downloader.py | 22 +++++++++++++++++----- deemix/utils/taggers.py | 12 ++++++------ 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/deemix/app/downloader.py b/deemix/app/downloader.py index 5bcdd71..51bc4a0 100644 --- a/deemix/app/downloader.py +++ b/deemix/app/downloader.py @@ -4,9 +4,14 @@ from deemix.utils.taggers import tagID3, tagFLAC from deemix.utils.pathtemplates import generateFilename, generateFilepath import os.path from os import makedirs +from urllib.request import urlopen from urllib.error import HTTPError +from tempfile import gettempdir dz = Deezer() +TEMPDIR = os.path.join(gettempdir(), 'deezloader-imgs') +if not os.path.isdir(TEMPDIR): + makedirs(TEMPDIR) extensions = { 9: '.flac', @@ -243,19 +248,26 @@ def downloadTrackObj(trackAPI, settings, overwriteBitrate=False, extraTrack=None print('Downloading: {} - {}'.format(track['mainArtist']['name'], track['title'])) # Get the selected bitrate - if overwriteBitrate: - bitrate = overwriteBitrate - else: - bitrate = settings['maxBitrate'] + bitrate = overwriteBitrate if overwriteBitrate else settings['maxBitrate'] (format, filesize) = getPreferredBitrare(track['filesize'], bitrate) 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.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']] (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) writepath = os.path.join(filepath, filename) diff --git a/deemix/utils/taggers.py b/deemix/utils/taggers.py index e79f6db..c9ab758 100644 --- a/deemix/utils/taggers.py +++ b/deemix/utils/taggers.py @@ -1,6 +1,4 @@ #!/usr/bin/env python3 -from urllib.request import urlopen - from mutagen.flac import FLAC, Picture 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 @@ -56,8 +54,9 @@ def tagID3(stream, track, save): tag.add(IPLS(people=involved_people)) if save['copyright']: tag.add(TCOP(text=track['copyright'])) - if save['cover']: - tag.add(APIC(3, 'image/jpeg', 3, data=urlopen(track['album']['picUrl']).read())) + if save['cover'] and track['album']['picPath']: + 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) @@ -112,11 +111,12 @@ def tagFLAC(stream, track, save): if save['copyright']: tag["COPYRIGHT"] = track['copyright'] - if save['cover']: + if save['cover'] and track['album']['picPath']: image = Picture() image.type = 3 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.save(deleteid3=True)