· 7 years ago · Apr 02, 2018, 11:32 PM
1#!/usr/bin/python3
2# Todoist Quick Add CLI
3# jeffgreenca 2018
4#
5# A single-purpose tool to enable quick-add Todoist entries from the command line.
6#
7# Example:
8#
9# ./todoist-add.py remember to get milk tomorrow #Errands @walking p1
10#
11# Depending on your Todoist settings, this will tag "remember to get milk"
12# with a due date of "tomorrow", in the project Errands, with the label
13# "walking", as a priority 1 entry.
14#
15# Requires todoist client, which can be installed with:
16# pip install todoist-python
17import sys
18from pprint import pprint
19from pathlib import Path
20
21CONFIG_FILE = str(Path.home()) + '/.todoist'
22
23def pdot():
24 """Makeshift progress bar"""
25 print('.', end='', flush=True)
26
27# Configure
28try:
29 with open(CONFIG_FILE, 'r') as f:
30 SECRET_KEY = f.read()
31except FileNotFoundError:
32 SECRET_KEY = input("Enter your API token (will be saved to %s): " % CONFIG_FILE)
33 with open(CONFIG_FILE, 'w') as f:
34 f.write(SECRET_KEY)
35
36if len(sys.argv) > 1:
37 # Consider the entire command line arguments as the task to add
38 task = ' '.join(sys.argv[1:])
39
40 # These tasks take longer, so show a "progress" bar
41 pdot()
42 from todoist import TodoistAPI
43 pdot()
44 api = TodoistAPI(SECRET_KEY)
45 pdot()
46 retval = api.quick.add(task)
47 pdot()
48
49 # Quick add may "eat" some of the task, but not all
50 # For example #myproject becomes a projectid field
51 if retval and 'content' in retval.keys() and retval['content'] in task:
52 print("OK", end='')
53 else:
54 print("Error!")
55 pprint(retval)
56else:
57 print("Todoist Quick Add Task, jeffgreenca 2018\n")
58 print("Usage: ./todoist-add.py <task in quick add syntax>\n")
59 print("Example:")
60 print(" ./todoist-add.py remember to get milk tomorrow #errands @walking p1")
61 print("\nExpects Todoist API token in ~/.todoist (or prompts to create it)")