Support multivalued song tags (fixes #1)

python-mpd2 unreliably returns either a single value or a list of
values for commands like currentsong, which is super fun if you're
trying to write type stubs for it that statically describe its
behaviour. Whee.

Anyway, I ended up changing my internal song model to always use lists
for tags like artist and genre which are likely to have multiple values.
There's some finagling involved in massaging python-mpd2's output into
lists every time. However it's much nicer to work with an object that
always has a list of artists, even if it's a list of one or zero
artists, rather than an object that can have a single artist, a list of
multiple artists, or a null. So it's worth it.

The MPNowPlayingInfoCenter in MacOS only works with single string values
for these tags, not lists, so we have to join the artists and such into
a single string for its consumption. I'm using commas for the separator
at the moment, but I may make this a config option later on if there's
interest.
This commit is contained in:
Danielle McLean 2024-06-23 17:24:37 +10:00
parent 2f70c6f7fa
commit bc56686fc4
Signed by: 00dani
GPG key ID: 6854781A0488421C
6 changed files with 81 additions and 31 deletions

View file

@ -6,6 +6,7 @@ from yarl import URL
from ..cache import Cache, make_cache
from ..tools.asyncio import run_background_task
from ..tools.types import un_maybe_plural
from .types import CurrentSongResponse, MpdStateHandler
CACHE_TTL = 60 * 60 # seconds = 1 hour
@ -16,9 +17,11 @@ class ArtCacheEntry(TypedDict):
def calc_album_key(song: CurrentSongResponse) -> str:
artist = song.get("albumartist", song.get("artist", "Unknown Artist"))
album = song.get("album", "Unknown Album")
return ":".join(t.replace(":", "-") for t in (artist, album))
artist = sorted(
un_maybe_plural(song.get("albumartist", song.get("artist", "Unknown Artist")))
)
album = sorted(un_maybe_plural(song.get("album", "Unknown Album")))
return ":".join(";".join(t).replace(":", "-") for t in (artist, album))
def calc_track_key(song: CurrentSongResponse) -> str: