| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import subprocess
- import os
- import base
- import output
- _FOLDER = '/mnt/nas/music/albums'
- class Music(base.BaseService):
- folders = None
- current_process = None
- def __init__(self):
- self.initialized = False
- def init(self):
- folders = [unicode(os.path.join(_FOLDER, f).decode('utf-8')) for f in os.listdir(_FOLDER)]
- folders = [f for f in folders if os.path.isdir(f)]
- self.folders = {os.path.basename(f).lower(): f for f in folders}
- self.initialized = True
- def _search(self, tokens):
- if not tokens:
- return
- eligible = self.folders
- for t in tokens:
- t = t.lower()
- eligible = {f: v for f, v in eligible.iteritems() if t in f}
-
- output.debug('tokens: %s; eligible: %s', tokens, eligible.keys())
- if eligible:
- return eligible[eligible.keys()[0]]
- def stop(self):
- if self.current_process:
- self.current_process.terminate()
- self.current_process = None
- def call(self):
- tokens = ears.listen(pause_threshold=3, preferred_phrases=self.folders.keys()[:300])
- f = self._search(tokens)
- if f:
- output.debug('playing %s' % f)
- self.current_process = subprocess.Popen(['cvlc', '-Z', f, ])
- else:
- output.failure()
|