| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- import os
- import base
- import output
- from config import Config
- import input_sources
- class Music(base.BaseService):
- folders = None
- current_process = None
- def __init__(self, cclient):
- self.initialized = False
- self.cclient = cclient
- def init(self):
- if not Config.MUSIC_FOLDERS:
- return
- folders = []
- for folder in Config.MUSIC_FOLDERS:
- folders.extend(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):
- phrases = self.folders.keys()[:300]
- tokens = []
- while input_sources.token_available():
- tokens.append(input_sources.get_token(0, preferred_phrases=phrases))
- if not tokens:
- output.failure('No play command specified.')
- return
-
- if tokens[0] == 'stop':
- self.stop()
-
- else:
- f = self._search(tokens)
- if f:
- output.debug('playing %s' % f)
- self.current_process = self.cclient.call(['cvlc', '-Z', f, ], block_until_terminated=False)
- else:
- output.failure()
- def help(self):
- return 'Plays music. Example: "play Coldplay".'
|