Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import os
import webbrowser
from datetime import datetime
from dotenv import load_dotenv
from todoist.api import TodoistAPI
load_dotenv()
try:
api_key = os.environ['TODOIST_KEY']
except:
print('"TODOIST_KEY" not found in your environment variables')
exit()
api = TodoistAPI(api_key)
api.sync()
def get_all_tasks():
return api.state['items']
def get_all_projects():
return api.state['projects']
def pick_project(todoist_projects):
projects = []
print('todoist projects')
for n, project in enumerate(todoist_projects):
project_name = project['name'].encode(
'ascii', 'ignore').decode('ascii') # remove emojis
project_id = project['id']
projects.append(project_id)
print(f'{n + 1}: {project_name}')
project = int(input('which project id? '))
return projects[project - 1]
def find_tasks(todoist_tasks, todoist_project):
tasks = []
for task in todoist_tasks:
if task.data['project_id'] == todoist_project:
content = task.data['content']
when = task.data['due']['date'] if task.data['due'] else ''
creation_date = task.data['date_added']
# if markdown link task then separate out
if content[0] == '[' and content[-1] == ')':
title = content.split('](')[0][1:].strip()
notes = content.split('](')[1][:-1].strip()
else:
title = content
notes = ''
tasks.append({'title': title, 'notes': notes,
'when': when, 'creation_date': creation_date})
return tasks
def make_project():
things_project = f'IMPORT {datetime.now().strftime("%m-%y %X")}'
purl = f'things:///add-project?title={things_project}&reveal=true'
webbrowser.open(purl)
return things_project
def add_to_things(things_tasks):
# create a new project/list to hold the imported tasks
list_title = make_project()
# tags must already exist, comma separated
tags = 'todoist'
for task in things_tasks:
title = task['title']
notes = task['notes']
when = task['when']
creation_date = task['creation_date']
print(f'...moving {title[:30]}...')
turl = f'things:///add?title={title}&notes={notes}&when={when}&tags={tags}&list={list_title}&creation-date={creation_date}'
webbrowser.open(turl)
print(f'tasks moved to things. find them in project {list_title}')
if __name__ == '__main__':
todoist_tasks = get_all_tasks()
todoist_projects = get_all_projects()
todoist_project = pick_project(todoist_projects)
things_tasks = find_tasks(todoist_tasks, todoist_project)
print(f'found {len(things_tasks)} matching tasks...')
cont = input('continue? (y/n) ')
if cont.lower() != 'y':
print('no tasks were moved.')
exit()
else:
print('starting to move your tasks...')
add_to_things(things_tasks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment