Skip to content

Commit 791000f

Browse files
committed
Fix non-JPG url metadata album covers
1 parent f667fb6 commit 791000f

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/modules/url_metadata.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import ujson as json
1111
from utils import get_yt_id
1212
from meta import BUNDLE_IDENTIFIER
13+
from PIL import Image
14+
from io import BytesIO
1315

1416
def tbr_audio_key(item):
1517
return (item.get('tbr', 0) or 0) * (item.get('vcodec', 'none') == 'none')
@@ -70,6 +72,7 @@ class URLMetadata:
7072
DB_COLUMNS = {'title', 'artist', 'album', 'length', 'url', 'audio_url', 'ext', 'art', 'expiry', 'id', 'pl_src', 'live', 'type'}
7173
MAPPED_FIELDS = {'url': 'src', 'ytid': 'id', 'is_live': 'live', 'art': 'album_cover_url'}
7274
FIELDS_TO_IGNORE = set()
75+
ALBUM_COVER_CACHE_DIR = Path(appdirs.user_cache_dir()) / BUNDLE_IDENTIFIER / 'Cache' / 'Album Covers'
7376

7477
def __init__(self, src: str, url_type: str, title: str, artist: str, album: str, live: bool | None = None, length: float | None = None,
7578
url: str | None = None, audio_url: str | None = None, ext: str | None = None, expiry=None, id=None, album_cover_url=None,
@@ -209,7 +212,7 @@ def hash(self) -> str:
209212

210213
@property
211214
def image_cache_path(self):
212-
return Path(appdirs.user_cache_dir()) / BUNDLE_IDENTIFIER / 'Cache' / 'Album Covers' / f'{self.hash()}.jpg'
215+
return self.ALBUM_COVER_CACHE_DIR / f'{self.hash()}.jpg'
213216

214217
@property
215218
def is_expired(self):
@@ -218,13 +221,13 @@ def is_expired(self):
218221
return self.expiry < time.time()
219222

220223
def get_cover_image(self) -> bytes:
221-
if self.image_cache_path.exists():
222-
with open(self.image_cache_path, 'rb') as f:
223-
return b64encode(f.read())
224-
if self.album_cover_url:
225-
img_data = requests.get(self.album_cover_url).content
226-
self.image_cache_path.parent.mkdir(parents=True, exist_ok=True)
227-
with open(self.image_cache_path, 'wb') as f:
228-
f.write(img_data)
229-
return b64encode(img_data)
230-
return custom_art('URL')
224+
if not self.image_cache_path.exists():
225+
if not self.album_cover_url:
226+
return custom_art('URL')
227+
Image.open(BytesIO(requests.get(self.album_cover_url).content)).convert('RGB').save(self.image_cache_path, 'JPEG', quality=95)
228+
with open(self.image_cache_path, 'rb') as f:
229+
return b64encode(f.read())
230+
231+
232+
# only run once to reduce OS calls
233+
URLMetadata.ALBUM_COVER_CACHE_DIR.mkdir(parents=True, exist_ok=True)

0 commit comments

Comments
 (0)