From 37166e146de459930575c5f5f625075c96748cb3 Mon Sep 17 00:00:00 2001 From: Danielle McLean Date: Fri, 17 May 2024 10:18:54 +1000 Subject: [PATCH] Try both readpicture and albumart, prefer the former --- src/mpd_now_playable/mpd/artwork_cache.py | 4 ++-- src/mpd_now_playable/mpd/listener.py | 6 ++++++ src/mpd_now_playable/mpd/types.py | 6 ++---- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/mpd_now_playable/mpd/artwork_cache.py b/src/mpd_now_playable/mpd/artwork_cache.py index 298effe..f99dbf2 100644 --- a/src/mpd_now_playable/mpd/artwork_cache.py +++ b/src/mpd_now_playable/mpd/artwork_cache.py @@ -6,7 +6,7 @@ from ..async_tools import run_background_task from ..cache import Cache, make_cache from .types import CurrentSongResponse, MpdStateHandler -CACHE_TTL = 60 * 60 # seconds = 1 hour +CACHE_TTL = 60 * 60 # seconds = 1 hour class ArtCacheEntry(TypedDict): @@ -49,7 +49,7 @@ class MpdArtworkCache: return None async def cache_artwork(self, song: CurrentSongResponse) -> None: - art = ArtCacheEntry(data=await self.mpd.readpicture(song["file"])) + art = ArtCacheEntry(data=await self.mpd.get_art(song["file"])) try: await self.album_cache.add(calc_album_key(song), art, ttl=CACHE_TTL) except ValueError: diff --git a/src/mpd_now_playable/mpd/listener.py b/src/mpd_now_playable/mpd/listener.py index 960dc8c..db46f19 100644 --- a/src/mpd_now_playable/mpd/listener.py +++ b/src/mpd_now_playable/mpd/listener.py @@ -95,6 +95,12 @@ class MpdStateListener(Player): print(song) listener.update(song) + async def get_art(self, file: str) -> bytes | None: + picture = await self.readpicture(file) + if picture: + return picture + return await self.albumart(file) + async def albumart(self, file: str) -> bytes | None: try: albumart = await self.client.albumart(file) diff --git a/src/mpd_now_playable/mpd/types.py b/src/mpd_now_playable/mpd/types.py index 4fe2d25..a5aa8a3 100644 --- a/src/mpd_now_playable/mpd/types.py +++ b/src/mpd_now_playable/mpd/types.py @@ -2,11 +2,9 @@ from typing import Literal, NotRequired, Protocol, TypedDict class MpdStateHandler(Protocol): - async def readpicture(self, file: str) -> bytes | None: - ... + async def get_art(self, file: str) -> bytes | None: ... - async def refresh(self) -> None: - ... + async def refresh(self) -> None: ... BooleanFlag = Literal["0", "1"]