| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- import logging
- import base
- import requests
- import input_sources
- import output
- _LISTEN_TIMEOUT = 600
- _GROCERIES = ['milk', 'bread']
- _WUNDERLIST_CLIENT_ID = '34e0e31945afeb63e1b8'
- _WUNDERLIST_ACCESS_TOKEN = 'a7258e8b6596fdaffc904fc6b65d40c23afe6d2a28bfe7be1a9026d44b59'
- class _WunderlistClient(object):
- lists = None
-
- def __init__(self, client_id, access_token):
- self.client_id = client_id
- self.access_token = access_token
-
- def call(self, method, url, body=None):
- headers = {
- 'x-client-id': self.client_id,
- 'x-access-token': self.access_token,
- 'content-type': 'application/json',
- 'accept': 'application/json'
- }
- resp = requests.request(method, url, headers=headers, json=body)
-
- if resp.status_code < 300:
- return resp.json()
- else:
- raise Exception(resp.content)
- def create_task(self, list_name, title):
- list_id = self.lists[list_name]
- self.call('POST', 'https://a.wunderlist.com/api/v1/tasks', {'list_id': list_id, 'title': title})
- def init(self):
- lists_res = self.call('GET', 'https://a.wunderlist.com/api/v1/lists')
- self.lists = {x['title'].lower(): x['id'] for x in lists_res}
-
- class Wunderlist(base.BaseFunction):
- client = None
- def __init__(self, multiple_tasks, list_name=None):
- self.client = _WunderlistClient(_WUNDERLIST_CLIENT_ID, _WUNDERLIST_ACCESS_TOKEN)
- self.multiple_tasks = multiple_tasks
- self.list_name = list_name
- self.initialized = False
- def init(self):
- self.client.init()
- if self.list_name and self.list_name not in self.client.lists:
- output.failure('Could not find list %s' % self.list_name)
- return
- self.initialized = True
- def _get_list(self):
- if self.list_name:
- return self.list_name
-
- else:
- if not input_sources.token_available():
- output.text('Select list.')
- list_name = input_sources.get_token(preferred_phrases=self.client.lists.keys())
- output.debug('Selected list: %s' % list_name)
- if list_name in self.client.lists:
- return list_name
- else:
- return None
- def call(self, **_):
- list_name = self._get_list()
- if list_name is None:
- output.failure('No list available.')
- return
- tasks = []
- if self.multiple_tasks:
- t = input_sources.get_token(120)
- output.text('Specify tasks:')
- while t not in ['done', 'cancel', None]:
- tasks.append(t)
- t = input_sources.get_token(120)
- if t in ['cancel', None]:
- output.cancelled()
- return
-
- else:
- output.text('Specify task:')
- task = input_sources.get_line()
- if task == 'cancel':
- output.cancelled()
- return
- else:
- tasks = [task]
- for task in tasks:
- self.client.create_task(self.list_name, task)
- output.success()
|