Created
October 28, 2020 18:35
-
-
Save lybekk/86d5b75003fdc4d084ab285709e91b52 to your computer and use it in GitHub Desktop.
Interact with Todoist's API with Python
This file contains 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 characters
""" Todoist project/task exporter | |
* WORK IN PROGRESS * | |
Uses Todoist's Official library | |
https://github.com/Doist/todoist-python | |
""" | |
import os | |
import sys | |
from string import Template | |
import argparse | |
from todoist.api import TodoistAPI | |
""" SETTINGS | |
If using an environment variable is not preferred, | |
replace the TOKEN variable's contents with the API Token directly | |
""" | |
TOKEN = os.environ["TODOIST_API_TOKEN"] | |
def sync(*arguments): | |
result = api.sync() | |
print("Synced ", len(result["items"]), " items") | |
def projects(*arguments): | |
projects_archived = [] | |
projects_deleted = [] | |
result = api.state["projects"] | |
for p in result: | |
template_string = """>>>>>>>> | |
Project: $project | |
Structure: | |
$item | |
\n<<<<<<<< | |
""" | |
mapping = { | |
"project": p["name"], | |
"item": p, | |
} | |
template_object = Template(template_string) | |
template_ready = template_object.substitute(**mapping) | |
print(template_ready) | |
if p["is_archived"] == 1: | |
projects_archived.append(p["name"]) | |
if p["is_deleted"] == 1: | |
projects_deleted.append(p["name"]) | |
print(8 * "#") | |
print("Archived projects") | |
for x in projects_archived: | |
print(x) | |
print(8 * "#") | |
print(8 * "#") | |
print("Deleted projects") | |
for x in projects_deleted: | |
print(x) | |
print(8 * "#") | |
def project(arguments): | |
"""TODO: print all items belonging to the project | |
Implement offset w/pagination to retrieve all items | |
https://developer.todoist.com/sync/v8/?python#get-all-completed-items | |
""" | |
proj_id = arguments.id | |
if proj_id == 0: | |
proj_id = input("Insert ID:") | |
project_object = api.projects.get(proj_id) | |
print(project_object) | |
completed_items = api.completed.get_all(project_id=proj_id, limit=200) | |
print(completed_items) | |
"""WIP | |
print("\n Tasks") | |
print(8 * ">") | |
dump = api.state | |
for x in dump["items"]: | |
print(type(x)) | |
""" | |
def everything(*arguments): | |
""" | |
Prints (almost) everything. Pipable. | |
TODO: Convert all output to JSON. | |
Classes Item, Project .. will need iteration. | |
Alternative: Use managers | |
""" | |
keys_to_remove = [ | |
'day_orders_timestamp', | |
'filters', | |
'query', | |
"collaborator_states", | |
"collaborators", | |
'day_orders', | |
#"projects", | |
"labels", | |
] | |
dictionary_updater = {key: None for key in keys_to_remove} | |
dump = api.state | |
dump.update(dictionary_updater) | |
print(dump) | |
api = TodoistAPI(TOKEN) | |
parser = argparse.ArgumentParser( | |
prog='Todoist API script', | |
usage='%(prog)s [options] path. -h for help', | |
description='Various ways to interact with todoist', | |
epilog='Concluded :)') | |
subparsers = parser.add_subparsers(help='Sub-parsers help') | |
parser_sync = subparsers.add_parser('sync', help='Sync Todoist') | |
parser_sync.set_defaults(func=sync) | |
parser_projects = subparsers.add_parser('projects', help='View projects') | |
parser_projects.set_defaults(func=projects) | |
parser_project = subparsers.add_parser('project', help='View one project') | |
parser_project.add_argument('-i', '--id', type=int, default=0, help="Project ID") | |
parser_project.set_defaults(func=project) | |
parser_everything = subparsers.add_parser('everything', help='View everything') | |
parser_everything.set_defaults(func=everything) | |
if len(sys.argv) <= 1: | |
sys.argv.append('--help') | |
args = parser.parse_args() | |
print(args) | |
args.func(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment