Last active
September 24, 2023 21:10
-
-
Save kbl/5970131 to your computer and use it in GitHub Desktop.
Revisions
-
kbl revised this gist
Mar 8, 2014 . 1 changed file with 7 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,13 +2,12 @@ import json import urllib2 import urllib import sys import os from argparse import ArgumentParser from collections import defaultdict def log(message): sys.stdout.write(message + os.linesep) @@ -20,12 +19,13 @@ def _api_call(self, api_method, parameters, add_token=True): parameters = dict(parameters) if add_token: parameters.update({'token': self.token}) params = urllib.urlencode(parameters) url = u'https://todoist.com/API/%s?%s' % (api_method, params) log('Calling url %s' % url) return_value = json.load(urllib2.urlopen(url)) if type(return_value) is unicode: raise Exception('Call finished with error! %s' % return_value) return return_value def add_list(self, list_name): log('Adding list %s' % list_name) -
kbl revised this gist
Feb 22, 2014 . 1 changed file with 40 additions and 25 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,43 +1,58 @@ # -*- coding: utf8 -*- import json import urllib2 import sys import os from argparse import ArgumentParser from collections import defaultdict url_replace = {u'ł': 'l', u'ć': 'c', ' ': '+', u'ż': 'z', u'ó': 'o', u'ą': 'a', u'ę': 'e', u'ś': 's'} def log(message): sys.stdout.write(message + os.linesep) class TodoistAPI(object): def __init__(self, email, password): self.token = self._api_call('login', {'email': email, 'password': password}, add_token=False)['token'] def _api_call(self, api_method, parameters, add_token=True): parameters = dict(parameters) if add_token: parameters.update({'token': self.token}) params = '&'.join(['%s=%s' % (k, v) for k, v in parameters.items()]) for f, t in url_replace.items(): params = params.replace(f, t) url = u'http://todoist.com/API/%s?%s' % (api_method, params) log('Calling url %s' % url) return json.load(urllib2.urlopen(url)) def add_list(self, list_name): log('Adding list %s' % list_name) return self._api_call('addProject', {'name': list_name})['id'] def add_task(self, list_id, task_name): log('Adding task %s to list %d' % (task_name, list_id)) return self._api_call('addItem', {'project_id': list_id, 'content': task_name})['id'] def create_parser(): parser = ArgumentParser() parser.add_argument('wunderlist_dump_json') parser.add_argument('todoist_email') parser.add_argument('todoist_password') return parser if __name__ == '__main__': parser = create_parser() args = parser.parse_args() todoist_api = TodoistAPI(args.todoist_email, args.todoist_password) with open(args.wunderlist_dump_json) as f: j = json.load(f) lists = {} for l in j['lists']: lists[l['id']] = todoist_api.add_list(l['title']) lists['inbox'] = todoist_api.add_list('inbox') for t in j['tasks']: todoist_api.add_task(lists[t['list_id']], t['title']) -
kbl created this gist
Jul 10, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ # -*- coding: utf8 -*- # to get API token send request to # http://todoist.com/API/login?email=<your_email>&password=<your password> # and copy api_token field value from returned json, # it should be sth like "2acd79190f40ee06aXXXXXXXXXXXXXXXXXXXXXXX" import json import urllib2 from pprint import pprint from collections import defaultdict token = 'token' wunderlist_dump = 'wunderlist-xxx.json' url_replace = {u'ł': 'l', u'ć': 'c', ' ': '+', u'ż': 'z', u'ó': 'o', u'ą': 'a', u'ę': 'e', u'ś': 's'} def api_call(api_method, parameters): parameters = dict(parameters) parameters.update({'token': token}) params = '&'.join(['%s=%s' % (k, v) for k, v in parameters.items()]) for f, t in url_replace.items(): params = params.replace(f, t) url = u'http://todoist.com/API/%s?%s' % (api_method, params) return json.load(urllib2.urlopen(url)) def add_list(list_name): return api_call('addProject', {'name': list_name})['id'] def add_task(list_id, task_name): return api_call('addItem', {'project_id': list_id, 'content': task_name})['id'] if __name__ == '__main__': with open(wunderlist_dump) as f: j = json.load(f) lists = {} for l in j['/me/lists']: lists[l['id']] = add_list(l['title']) lists['inbox'] = add_list('inbox') for t in j['/me/tasks']: add_task(lists[t['list_id']], t['title'])