__init__.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import threading
  2. import traceback
  3. import todo_wunderlist
  4. import input_sources
  5. import output
  6. import services.help
  7. import echo
  8. import duckduckgo_answers
  9. import conf
  10. def _get_services(cclient):
  11. res = {
  12. 'echo': echo.Echo(),
  13. 'duck': duckduckgo_answers.DuckDuckGo(),
  14. }
  15. res['help'] = services.help.Help(res)
  16. if conf.WEATHER_LAT and conf.WEATHER_LON:
  17. import weather
  18. res['weather'] = weather.Weather()
  19. if conf.MAILGUN_API_KEY:
  20. import email_mailgun
  21. res['email'] = email_mailgun.Email()
  22. if conf.WUNDERLIST_ACCESS_TOKEN:
  23. import todo_wunderlist
  24. res['food'] = todo_wunderlist.Wunderlist(True, 'food')
  25. res['todo'] = todo_wunderlist.Wunderlist(False)
  26. if conf.MUSIC_FOLDERS:
  27. import music
  28. res['play'] = music.Music(cclient)
  29. if conf.TZ:
  30. import current_time
  31. res['time'] = current_time.CurrentTime(conf.TZ)
  32. return res
  33. def launch_services(cclient, interrupted):
  34. _services = _get_services(cclient)
  35. for s in _services.values():
  36. if not s.initialized:
  37. t = threading.Thread(target=s.init)
  38. t.start()
  39. output.text('Ready. Available services: %s.' % ', '.join(sorted(_services.keys())))
  40. while not interrupted():
  41. input_sources.PREFERRED_PHRASES = _services.keys() + ['quit']
  42. t = input_sources.get_token(1)
  43. if t is not None and t in _services and _services[t].initialized:
  44. try:
  45. _services[t].call()
  46. except Exception, e:
  47. output.failure(s='Error: %s' % traceback.format_exc())
  48. elif t == 'quit':
  49. return
  50. elif t is not None:
  51. output.failure('"%s" is not a valid service.' % t)