todoist-taskwarrior/migrate.py

86 lines
2.2 KiB
Python
Raw Normal View History

import click
from taskw import TaskWarrior
from todoist.api import TodoistAPI
todoist = None
taskwarrior = None
""" CLI Commands """
@click.group()
@click.option('--todoist-api-key', envvar='TODOIST_API_KEY', required=True)
def cli(todoist_api_key):
# Just do some initialization
global todoist
global taskwarrior
todoist = TodoistAPI(todoist_api_key)
taskwarrior = TaskWarrior()
@cli.command()
@click.option('-i', '--interactive', is_flag=True, default=False)
@click.option('--no-sync', is_flag=True, default=False)
def migrate(interactive, no_sync):
if not no_sync:
important('Syncing tasks with todoist... ', nl=False)
todoist.sync()
success('OK')
tasks = todoist.items.all()
important(f'Starting migration of {len(tasks)}...')
for task in todoist.items.all():
2018-11-08 00:51:51 +01:00
tid = task['id']
name = task['content']
2018-11-08 01:01:16 +01:00
project = todoist.projects.get_by_id(task['project_id'])['name']
2018-11-08 01:12:48 +01:00
priority = taskwarrior_priority(task['priority'])
2018-11-08 01:01:16 +01:00
tags = [
todoist.labels.get_by_id(l_id)['name']
for l_id in task['labels']
]
2018-11-08 00:51:51 +01:00
if interactive and not click.confirm(f"Import '{name}'?"):
continue
2018-11-08 01:12:48 +01:00
add_task(tid, name, project, tags, priority)
2018-11-08 00:27:00 +01:00
2018-11-08 01:12:48 +01:00
def add_task(tid, name, project, tags, priority):
2018-11-08 00:27:00 +01:00
"""Add a taskwarrior task from todoist task
Returns the taskwarrior task.
"""
2018-11-08 01:01:16 +01:00
info(f"Importing '{name}' ({project}) - ", nl=False)
2018-11-08 00:27:00 +01:00
try:
2018-11-08 01:12:48 +01:00
tw_task = taskwarrior.task_add(name, project=project, tags=tags, priority=priority)
2018-11-08 00:27:00 +01:00
except:
error('FAILED')
else:
success('OK')
2018-11-08 01:01:16 +01:00
return tw_task
2018-11-08 00:27:00 +01:00
""" Utils """
2018-11-08 00:27:00 +01:00
def important(msg, **kwargs):
click.echo(click.style(msg, fg='blue', bold=True), **kwargs)
def info(msg, **kwargs):
click.echo(msg, **kwargs)
2018-11-08 00:27:00 +01:00
def success(msg, **kwargs):
click.echo(click.style(msg, fg='green', bold=True))
2018-11-08 00:27:00 +01:00
def error(msg, **kwargs):
click.echo(click.style(msg, fg='red', bold=True))
2018-11-08 01:12:48 +01:00
PRIORITIES = {1: None, 2: 'L', 3: 'M', 4: 'H'}
def taskwarrior_priority(priority):
"""Converts a priority from Todiost (1-4) to taskwarrior (None, L, M, H) """
return PRIORITIES[priority]
""" Entrypoint """
if __name__ == '__main__':
cli()