fix mpdrandom for new python2-mpd library

This commit is contained in:
boyska 2021-06-13 15:27:38 +02:00
parent e9a37cf0f2
commit c330bdeff4

View file

@ -9,8 +9,7 @@ log = logging.getLogger(__name__)
def generate_by_artist(spec):
"""choose HOWMANY random artists, and for each one choose a random song"""
"""Choose HOWMANY random artists, and for each one choose a random song."""
spec.setdefault("howmany", 1)
prefix = spec.get("prefix", "").rstrip("/")
log.info("generating")
@ -19,7 +18,8 @@ def generate_by_artist(spec):
c.connect(conf["MPD_HOST"], conf["MPD_PORT"])
if prefix:
# TODO: listallinfo is discouraged. how else could we achieve the same result?
# TODO: listallinfo is discouraged.
# how else could we achieve the same result?
artists = list(
{r["artist"] for r in c.listallinfo(prefix) if "artist" in r}
)
@ -29,6 +29,9 @@ def generate_by_artist(spec):
raise ValueError("no artists in your mpd database")
for _ in range(spec["howmany"]):
artist = random.choice(artists) # pick one artist
if type(artist) is not str:
# different mpd library versions have different behavior
artist = artist['artist']
# pick one song from that artist
artist_songs = (res["file"] for res in c.find("artist", artist))
if prefix: