import logging import base import tts import clips import ears import requests _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} print self.lists 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: logging.error('Could not find list %s', self.list_name) return self.initialized = True def call(self, **_): tts.tts('ready') lines, terminator = ears.listen_until_terminated(timeout=_LISTEN_TIMEOUT, preferred_phrases=_GROCERIES) if terminator == 'cancel': tts.tts('cancelled') return else: items = [] for line in lines: items.extend([t.strip() for t in line.split(' and ') if t.strip()]) for item in items: self.client.create_task(self.list_name, item) tts.tts('ok')