__init__.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import logging
  2. import threading
  3. import traceback
  4. import input_sources
  5. import output
  6. import services.help
  7. import echo
  8. import duckduckgo_answers
  9. from config import Config
  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 Config.WEATHER_LAT and Config.WEATHER_LON:
  17. import weather
  18. res['weather'] = weather.Weather()
  19. if Config.MAILGUN_API_KEY:
  20. import email_mailgun
  21. res['email'] = email_mailgun.Email()
  22. if Config.WUNDERLIST_ACCESS_TOKEN:
  23. import todo_wunderlist
  24. res['food'] = todo_wunderlist.Wunderlist(True, 'food')
  25. res['todo'] = todo_wunderlist.Wunderlist(False)
  26. if Config.MUSIC_FOLDERS:
  27. import music
  28. res['play'] = music.Music(cclient)
  29. if Config.TZ:
  30. import current_time
  31. res['time'] = current_time.CurrentTime(Config.TZ)
  32. return res
  33. def launch_services(cclient, interrupted, trigger_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. preferred_phrases = _services.keys() + ['quit', 'cancel']
  40. output.text('Ready. Available services: %s.' % ', '.join(sorted(_services.keys())))
  41. while not interrupted():
  42. t = input_sources.get_token(1, preferred_phrases=preferred_phrases)
  43. if t is not None and t in _services and _services[t].initialized:
  44. try:
  45. _services[t].call()
  46. output.success()
  47. except Exception, e:
  48. output.failure(msg='Error: %s' % traceback.format_exc())
  49. elif t == 'quit':
  50. trigger_interrupted()
  51. return
  52. elif t == 'cancel':
  53. pass
  54. elif t is not None:
  55. output.failure('"%s" is not a valid service.' % t)
  56. if t is not None:
  57. input_sources.reset()