__init__.py 1.5 KB

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