todo_wunderlist.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import logging
  2. import base
  3. import ears
  4. import requests
  5. import output
  6. _LISTEN_TIMEOUT = 600
  7. _GROCERIES = ['milk', 'bread']
  8. _WUNDERLIST_CLIENT_ID = '34e0e31945afeb63e1b8'
  9. _WUNDERLIST_ACCESS_TOKEN = 'a7258e8b6596fdaffc904fc6b65d40c23afe6d2a28bfe7be1a9026d44b59'
  10. class _WunderlistClient(object):
  11. lists = None
  12. def __init__(self, client_id, access_token):
  13. self.client_id = client_id
  14. self.access_token = access_token
  15. def call(self, method, url, body=None):
  16. headers = {
  17. 'x-client-id': self.client_id,
  18. 'x-access-token': self.access_token,
  19. 'content-type': 'application/json',
  20. 'accept': 'application/json'
  21. }
  22. resp = requests.request(method, url, headers=headers, json=body)
  23. if resp.status_code < 300:
  24. return resp.json()
  25. else:
  26. raise Exception(resp.content)
  27. def create_task(self, list_name, title):
  28. list_id = self.lists[list_name]
  29. self.call('POST', 'https://a.wunderlist.com/api/v1/tasks', {'list_id': list_id, 'title': title})
  30. def init(self):
  31. lists_res = self.call('GET', 'https://a.wunderlist.com/api/v1/lists')
  32. self.lists = {x['title'].lower(): x['id'] for x in lists_res}
  33. class Wunderlist(base.BaseFunction):
  34. client = None
  35. def __init__(self, multiple_tasks, list_name=None):
  36. self.client = _WunderlistClient(_WUNDERLIST_CLIENT_ID, _WUNDERLIST_ACCESS_TOKEN)
  37. self.multiple_tasks = multiple_tasks
  38. self.list_name = list_name
  39. self.initialized = False
  40. def init(self):
  41. self.client.init()
  42. if self.list_name and self.list_name not in self.client.lists:
  43. logging.error('Could not find list %s', self.list_name)
  44. return
  45. self.initialized = True
  46. def _get_list(self):
  47. if self.list_name:
  48. return self.list_name
  49. else:
  50. list_name = ears.listen(pause_threshold=1, preferred_phrases=self.client.lists.keys()).lower()
  51. if list_name in self.client.lists:
  52. return list_name
  53. else:
  54. return None
  55. def call(self, **_):
  56. list_name = self._get_list()
  57. if list_name is None:
  58. output.failure()
  59. return
  60. tasks = []
  61. if self.multiple_tasks:
  62. lines, terminator = ears.listen_until_terminated(timeout=_LISTEN_TIMEOUT, preferred_phrases=_GROCERIES)
  63. if terminator == 'cancel':
  64. output.cancelled()
  65. return
  66. else:
  67. for line in lines:
  68. tasks.extend([t.strip() for t in line.split(' and ') if t.strip()])
  69. else:
  70. task = ears.listen(pause_threshold=3)
  71. if task == 'cancel':
  72. output.cancelled()
  73. return
  74. else:
  75. tasks = [task]
  76. for task in tasks:
  77. self.client.create_task(self.list_name, task)
  78. output.success()