Compare commits

..

No commits in common. "1caf4a184cf7092b60171f2fcf74acbeefd47549" and "8b5912bed2b80fe175b3c96e23d30ef8d53bcac1" have entirely different histories.

2 changed files with 6 additions and 27 deletions

View file

@ -1,5 +1,3 @@
from dataclasses import dataclass
from aiocache import Cache
from ..async_tools import run_background_task
@ -8,25 +6,6 @@ from .types import CurrentSongResponse, MpdStateHandler
CACHE_TTL = 60 * 10 # ten minutes
@dataclass(frozen=True)
class HasArt:
data: bytes
@dataclass(frozen=True)
class HasNoArt:
data = None
ArtCacheEntry = HasArt | HasNoArt
def make_cache_entry(art: bytes | None) -> ArtCacheEntry:
if art is None:
return HasNoArt()
return HasArt(art)
def calc_album_key(song: CurrentSongResponse) -> str:
artist = song.get("albumartist", song.get("artist", "Unknown Artist"))
album = song.get("album", "Unknown Album")
@ -39,8 +18,8 @@ def calc_track_key(song: CurrentSongResponse) -> str:
class MpdArtworkCache:
mpd: MpdStateHandler
album_cache: "Cache[ArtCacheEntry]"
track_cache: "Cache[ArtCacheEntry]"
album_cache: "Cache[bytes | None]"
track_cache: "Cache[bytes | None]"
def __init__(self, mpd: MpdStateHandler):
self.mpd = mpd
@ -50,7 +29,7 @@ class MpdArtworkCache:
async def get_cached_artwork(self, song: CurrentSongResponse) -> bytes | None:
art = await self.track_cache.get(calc_track_key(song))
if art:
return art.data
return art
# If we don't have track artwork cached, go find some.
run_background_task(self.cache_artwork(song))
@ -58,12 +37,12 @@ class MpdArtworkCache:
# Even if we don't have cached track art, we can try looking for cached album art.
art = await self.album_cache.get(calc_album_key(song))
if art:
return art.data
return art
return None
async def cache_artwork(self, song: CurrentSongResponse) -> None:
art = make_cache_entry(await self.mpd.readpicture(song["file"]))
art = await self.mpd.readpicture(song["file"])
try:
await self.album_cache.add(calc_album_key(song), art, ttl=CACHE_TTL)
except ValueError:

View file

@ -93,7 +93,7 @@ class MpdStateListener(Player):
async def readpicture(self, file: str) -> bytes | None:
try:
readpic = await self.client.readpicture(file)
return readpic.get("binary")
return readpic["binary"]
except CommandError:
return None