audiogen_mpdrandom.py 654 B

123456789101112131415161718192021
  1. import logging
  2. log = logging.getLogger('mpdrandom')
  3. import random
  4. from mpd import MPDClient
  5. def generate_by_artist(spec):
  6. '''choose HOWMANY random artists, and for each one choose a random song'''
  7. spec.setdefault('howmany', 1)
  8. log.info('generating')
  9. c = MPDClient()
  10. c.connect('localhost', 6600) # TODO: read global options somehow
  11. artists = c.list('artist')
  12. log.debug("got %d artists" % len(artists))
  13. if not artists:
  14. raise ValueError("no artists in your mpd database")
  15. for _ in xrange(spec['howmany']):
  16. artist = random.choice(artists)
  17. yield random.choice(c.find('artist', artist))['file']