todo_wunderlist.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import logging
  2. import base
  3. import requests
  4. import input_sources
  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. if not input_sources.token_available():
  51. output.text('Select list.')
  52. list_name = input_sources.get_token(preferred_phrases=self.client.lists.keys())
  53. output.text('Selected list: %s' % list_name)
  54. if list_name in self.client.lists:
  55. return list_name
  56. else:
  57. return None
  58. def call(self, **_):
  59. list_name = self._get_list()
  60. if list_name is None:
  61. output.failure()
  62. return
  63. tasks = []
  64. if self.multiple_tasks:
  65. t = input_sources.get_token(120)
  66. output.text('Specify tasks:')
  67. while t not in ['done', 'cancel', None]:
  68. tasks.append(t)
  69. t = input_sources.get_token(120)
  70. if t in ['cancel', None]:
  71. output.cancelled()
  72. return
  73. else:
  74. output.text('Specify task:')
  75. task = input_sources.get_line()
  76. if task == 'cancel':
  77. output.cancelled()
  78. return
  79. else:
  80. tasks = [task]
  81. for task in tasks:
  82. self.client.create_task(self.list_name, task)
  83. output.success()