todo_wunderlist.py 3.4 KB

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