todo.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import logging
  2. import base
  3. import tts
  4. import clips
  5. import ears
  6. import requests
  7. _LISTEN_TIMEOUT = 600
  8. _GROCERIES = ['milk', 'bread']
  9. _WUNDERLIST_CLIENT_ID = '34e0e31945afeb63e1b8'
  10. _WUNDERLIST_ACCESS_TOKEN = 'a7258e8b6596fdaffc904fc6b65d40c23afe6d2a28bfe7be1a9026d44b59'
  11. class WunderlistClient(object):
  12. lists = None
  13. def __init__(self, client_id, access_token):
  14. self.client_id = client_id
  15. self.access_token = access_token
  16. def call(self, method, url, body=None):
  17. headers = {
  18. 'x-client-id': self.client_id,
  19. 'x-access-token': self.access_token,
  20. 'content-type': 'application/json',
  21. 'accept': 'application/json'
  22. }
  23. resp = requests.request(method, url, headers=headers, json=body)
  24. if resp.status_code < 300:
  25. return resp.json()
  26. else:
  27. raise Exception(resp.content)
  28. def create_task(self, list_id, title):
  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. c = WunderlistClient(_WUNDERLIST_CLIENT_ID, _WUNDERLIST_ACCESS_TOKEN)
  34. c.init()
  35. c.create_task(c.lists['food'], 'Test #1')
  36. """
  37. class WunderbarTodo(base.BaseFunction):
  38. def init(self):
  39. self.client = WunderlistClient(_WUNDERLIST_CLIENT_ID, _WUNDERLIST_ACCESS_TOKEN)
  40. return self.client.init()
  41. def _groceries(self):
  42. tts.tts('ready')
  43. lines, terminator = ears.listen_until_terminated(timeout=_LISTEN_TIMEOUT, preferred_phrases=_GROCERIES)
  44. if terminator == 'cancel':
  45. tts.tts('cancelled')
  46. return
  47. else:
  48. items = []
  49. for line in lines:
  50. items.extend([t.strip() for t in line.split(' and ') if t.strip()])
  51. logging.info(items)
  52. tts.tts('ok')
  53. def call(self, trigger=None, **_):
  54. if trigger == 'food':
  55. self._groceries()
  56. else:
  57. tts.tts('Bad trigger')
  58. """