public
Last active

importing tasks/lists from wunderlist into todoist

  • Download Gist
import.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# -*- coding: utf8 -*-
 
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)
 
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 = 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)
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'])

How do you use this?

No seriously, how do you use this? I need it, but am not sure where to use the code. Please explain.

I'm not sure if either wunderlist's json format or todoist's API wasn't changed. If it wasn't you need:

  • export tasks from wunderlist to json,
  • place it in the same directory as this python script,
  • either rename json file to wunderlist-xxx.json or change wunderlist_dump variable value,
  • request for API (v1) token from todoist and place it in token variable,
  • $ python

I haven't used it since 6 months so I'm not sure if it still works (I'm still Todoist's happy user), I'll check it in spare time.

Thanks.

To get the API token (especially for Google login) it's easier to go to Todoist Account Settings where it is listed.

To run the script download PythonLauncher from www.python.org

When I exucute the scrtip via python import.py, I get the following error message:

Traceback (most recent call last):
  File "import.py", line 39, in <module>
    for l in j['/me/lists']:
KeyError: '/me/lists' 

Did wunderlist possibly change their json?

Yep. This is how it worked for me (Note that I am not importing completed tasks):

# -*- coding: utf-8 -*-
# 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 urllib
import urllib2
from pprint import pprint
from collections import defaultdict

token = 'SOMETOKEN'
wunderlist_dump = 'wunderlist-DUMP.json'


def api_call(api_method, parameters):
    parameters = dict(parameters)
    parameters.update({'token': token})
    params = urllib.urlencode(parameters)
    url = u'http://todoist.com/API/%s?%s' % (api_method, params)
    return json.load(urllib2.urlopen(url))


def add_list(list_name):
    print list_name
    return api_call('addProject', {'name': list_name.encode("utf8")})['id']


def add_task(list_id, task_name):
    print list_id, task_name
    return api_call('addItem', {'project_id': list_id, 'content': task_name.encode("utf8")})['id']


if __name__ == '__main__':
    with open(wunderlist_dump) as f:
        j = json.load(f)
        lists = {}
        for l in j['lists']:
           lists[l['id']] = add_list(l['title'])
        lists['inbox'] = add_list('inbox')
        for t in j['tasks']:
            if not "completed_at" in t and not "parent_id" in t:
                add_task(lists[t['list_id']], t['title'])

I've created newer version with some UX improvements ;). Currently you don't need to fiddle with the script at all, token is retrieved automatically based on passed email and password (as this password is stored in plain text in command line history please remember to wipe it afterwards).

The problem was in changed wunderlist's json file format. Right now you could use this by calling:

python import.py <path to wunderlist json file> <todoist account email> <todoist accout password>

I've tested in on simple account with default lists and 2 tasks - it worked.

Well... it sort of worked, but not really.

  • It created all the "Projects" in Todoist, but they were all empty.
  • Then it put all of those Projects's contents in "Inbox"

So the Projects have (0) and Inbox says (143).

Is that a simple fix by any chance?

I tested it on the file which you provided and... it was working on my machine :/

Not working for me. Getting a 401 error.

EDIT: Worked after changing ['token'] in Line 16 to ['api_token']

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.