| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- import threading
- import traceback
- import todo_wunderlist
- import input_sources
- import output
- import services.help
- import echo
- import duckduckgo_answers
- import conf
- def _get_services(cclient):
- res = {
- 'echo': echo.Echo(),
- 'duck': duckduckgo_answers.DuckDuckGo(),
- }
- res['help'] = services.help.Help(res)
- if conf.WEATHER_LAT and conf.WEATHER_LON:
- import weather
- res['weather'] = weather.Weather()
- if conf.MAILGUN_API_KEY:
- import email_mailgun
- res['email'] = email_mailgun.Email()
- if conf.WUNDERLIST_ACCESS_TOKEN:
- import todo_wunderlist
- res['food'] = todo_wunderlist.Wunderlist(True, 'food')
- res['todo'] = todo_wunderlist.Wunderlist(False)
- if conf.MUSIC_FOLDERS:
- import music
- res['play'] = music.Music(cclient)
- if conf.TZ:
- import current_time
- res['time'] = current_time.CurrentTime(conf.TZ)
- return res
- def launch_services(cclient, interrupted):
- _services = _get_services(cclient)
- for s in _services.values():
- if not s.initialized:
- t = threading.Thread(target=s.init)
- t.start()
- output.text('Ready. Available services: %s.' % ', '.join(sorted(_services.keys())))
- while not interrupted():
- input_sources.PREFERRED_PHRASES = _services.keys() + ['quit']
- t = input_sources.get_token(1)
- if t is not None and t in _services and _services[t].initialized:
- try:
- _services[t].call()
- except Exception, e:
- output.failure(s='Error: %s' % traceback.format_exc())
- elif t == 'quit':
- return
- elif t is not None:
- output.failure('"%s" is not a valid service.' % t)
|