Wrap MPD's state into a transfer struct before finalising the Song

This commit is contained in:
Danielle McLean 2024-07-23 13:12:06 +10:00
parent 1bb2032b9f
commit b8bcdc5a83
Signed by: 00dani
GPG key ID: 6854781A0488421C
2 changed files with 19 additions and 8 deletions

View file

@ -9,16 +9,17 @@ from yarl import URL
from ..config.model import MpdConfig from ..config.model import MpdConfig
from ..player import Player from ..player import Player
from ..song import Artwork, PlaybackState, Song, to_artwork, to_brainz from ..song import PlaybackState, Song, to_artwork, to_brainz
from ..song_receiver import Receiver from ..song_receiver import Receiver
from ..tools.types import option_fmap, un_maybe_plural from ..tools.types import option_fmap, un_maybe_plural
from .artwork_cache import MpdArtworkCache from .artwork_cache import MpdArtworkCache
from .types import CurrentSongResponse, StatusResponse from .types import MpdState
def mpd_current_to_song(
status: StatusResponse, current: CurrentSongResponse, art: Artwork def mpd_state_to_song(mpd: MpdState) -> Song:
) -> Song: status = mpd.status
current = mpd.current
return Song( return Song(
state=PlaybackState(status["state"]), state=PlaybackState(status["state"]),
queue_index=int(current["pos"]), queue_index=int(current["pos"]),
@ -34,8 +35,8 @@ def mpd_current_to_song(
disc=option_fmap(int, current.get("disc")), disc=option_fmap(int, current.get("disc")),
duration=float(status["duration"]), duration=float(status["duration"]),
elapsed=float(status["elapsed"]), elapsed=float(status["elapsed"]),
musicbrainz=to_brainz(current), musicbrainz=to_brainz(mpd.current),
art=art, art=to_artwork(mpd.art),
) )
@ -77,6 +78,7 @@ class MpdStateListener(Player):
status, current = await asyncio.gather( status, current = await asyncio.gather(
self.client.status(), self.client.currentsong() self.client.status(), self.client.currentsong()
) )
state = MpdState(status, current)
if starting_idle_count != self.idle_count: if starting_idle_count != self.idle_count:
return return
@ -90,7 +92,8 @@ class MpdStateListener(Player):
if starting_idle_count != self.idle_count: if starting_idle_count != self.idle_count:
return return
song = mpd_current_to_song(status, current, to_artwork(art)) state = MpdState(status, current, art)
song = mpd_state_to_song(state)
rprint(song) rprint(song)
await self.update(song) await self.update(song)

View file

@ -1,3 +1,4 @@
from dataclasses import dataclass
from typing import Literal, NotRequired, Protocol, TypedDict from typing import Literal, NotRequired, Protocol, TypedDict
from ..song.musicbrainz import MusicBrainzTags from ..song.musicbrainz import MusicBrainzTags
@ -76,3 +77,10 @@ class CurrentSongResponse(CurrentSongTags):
ReadPictureResponse = TypedDict("ReadPictureResponse", {"binary": bytes}) ReadPictureResponse = TypedDict("ReadPictureResponse", {"binary": bytes})
@dataclass
class MpdState:
status: StatusResponse
current: CurrentSongResponse
art: bytes | None = None