todo_wunderlist.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import requests
  2. import input_sources
  3. import output
  4. import base
  5. _LISTEN_TIMEOUT = 600
  6. _GROCERIES = ['milk', 'bread']
  7. _WUNDERLIST_CLIENT_ID = '34e0e31945afeb63e1b8'
  8. _WUNDERLIST_ACCESS_TOKEN = 'a7258e8b6596fdaffc904fc6b65d40c23afe6d2a28bfe7be1a9026d44b59'
  9. class _WunderlistClient(object):
  10. lists = None
  11. def __init__(self, client_id, access_token):
  12. self.client_id = client_id
  13. self.access_token = access_token
  14. def call(self, method, url, body=None):
  15. headers = {
  16. 'x-client-id': self.client_id,
  17. 'x-access-token': self.access_token,
  18. 'content-type': 'application/json',
  19. 'accept': 'application/json'
  20. }
  21. resp = requests.request(method, url, headers=headers, json=body)
  22. if resp.status_code < 300:
  23. return resp.json()
  24. else:
  25. raise Exception(resp.content)
  26. def create_task(self, list_name, title):
  27. list_id = self.lists[list_name]
  28. self.call('POST', 'https://a.wunderlist.com/api/v1/tasks', {'list_id': list_id, 'title': title})
  29. def init(self):
  30. lists_res = self.call('GET', 'https://a.wunderlist.com/api/v1/lists')
  31. self.lists = {x['title'].lower(): x['id'] for x in lists_res}
  32. class Wunderlist(base.BaseService):
  33. def __init__(self, multiple_tasks, list_name=None):
  34. self.client = _WunderlistClient(_WUNDERLIST_CLIENT_ID, _WUNDERLIST_ACCESS_TOKEN)
  35. self.multiple_tasks = multiple_tasks
  36. self.list_name = list_name
  37. self.initialized = False
  38. def init(self):
  39. self.client.init()
  40. if self.list_name and self.list_name not in self.client.lists:
  41. output.failure('Could not find list %s' % self.list_name)
  42. return
  43. self.initialized = True
  44. def _get_list(self):
  45. if self.list_name:
  46. return self.list_name
  47. else:
  48. if not input_sources.token_available():
  49. output.text('Select list.')
  50. list_name = input_sources.get_token(preferred_phrases=self.client.lists.keys())
  51. output.debug('Selected list: %s' % list_name)
  52. if list_name in self.client.lists:
  53. return list_name
  54. else:
  55. return None
  56. def call(self, **_):
  57. list_name = self._get_list()
  58. if list_name is None:
  59. output.failure('No list available.')
  60. return
  61. tasks = []
  62. if self.multiple_tasks:
  63. t = input_sources.get_token(120)
  64. output.text('Specify tasks:')
  65. while t not in ['done', 'cancel', None]:
  66. tasks.append(t)
  67. t = input_sources.get_token(120)
  68. if t in ['cancel', None]:
  69. output.cancelled()
  70. return
  71. else:
  72. output.text('Specify task:')
  73. task = input_sources.get_line()
  74. if task == 'cancel':
  75. output.cancelled()
  76. return
  77. else:
  78. tasks = [task]
  79. for task in tasks:
  80. self.client.create_task(self.list_name, task)
  81. output.success()
  82. def help(self):
  83. if self.list_name:
  84. if self.multiple_tasks:
  85. return 'Adds multiple tasks to the Wunderlist list %s' % self.list_name
  86. else:
  87. return 'Adds a task to the Wunderlist list %s' % self.list_name
  88. else:
  89. if self.multiple_tasks:
  90. return 'Adds multiple tasks to a Wunderlist list.'
  91. else:
  92. return 'Adds a task to a wunderlist list.'