audiogen_mpdrandom.py 701 B

12345678910111213141516171819202122232425
  1. import logging
  2. log = logging.getLogger("mpdrandom")
  3. import random
  4. from mpd import MPDClient
  5. from .config import get_conf
  6. def generate_by_artist(spec):
  7. """choose HOWMANY random artists, and for each one choose a random song"""
  8. spec.setdefault("howmany", 1)
  9. log.info("generating")
  10. conf = get_conf()
  11. c = MPDClient(use_unicode=True)
  12. c.connect(conf["MPD_HOST"], conf["MPD_PORT"])
  13. artists = c.list("artist")
  14. log.debug("got %d artists", len(artists))
  15. if not artists:
  16. raise ValueError("no artists in your mpd database")
  17. for _ in range(spec["howmany"]):
  18. artist = random.choice(artists)
  19. yield random.choice(c.find("artist", artist))["file"]