2014-03-10 00:11:58 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2015-04-30 20:02:50 -04:00
|
|
|
import logging
|
2015-08-29 14:15:09 -04:00
|
|
|
import argparse
|
2014-03-10 00:11:58 -04:00
|
|
|
|
2015-12-12 09:07:48 -05:00
|
|
|
# noinspection PyPackageRequirements
|
2015-04-30 20:02:50 -04:00
|
|
|
from todoist.api import TodoistAPI
|
|
|
|
|
2015-12-12 09:07:48 -05:00
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
|
2015-04-30 20:02:50 -04:00
|
|
|
|
|
|
|
def get_subitems(items, parent_item=None):
|
2016-02-20 12:25:35 -05:00
|
|
|
"""Search a flat item list for child items."""
|
2015-04-30 20:02:50 -04:00
|
|
|
result_items = []
|
|
|
|
found = False
|
|
|
|
if parent_item:
|
|
|
|
required_indent = parent_item['indent'] + 1
|
|
|
|
else:
|
|
|
|
required_indent = 1
|
|
|
|
for item in items:
|
|
|
|
if parent_item:
|
|
|
|
if not found and item['id'] != parent_item['id']:
|
|
|
|
continue
|
|
|
|
else:
|
|
|
|
found = True
|
|
|
|
if item['indent'] == parent_item['indent'] and item['id'] != parent_item['id']:
|
|
|
|
return result_items
|
2016-02-03 17:33:45 -05:00
|
|
|
elif item['indent'] == required_indent and found:
|
|
|
|
result_items.append(item)
|
2015-04-30 20:02:50 -04:00
|
|
|
elif item['indent'] == required_indent:
|
|
|
|
result_items.append(item)
|
|
|
|
return result_items
|
2014-11-15 14:45:52 -05:00
|
|
|
|
2014-03-10 00:11:58 -04:00
|
|
|
|
|
|
|
def main():
|
2016-02-20 12:25:35 -05:00
|
|
|
"""Main process function."""
|
2015-08-29 14:15:09 -04:00
|
|
|
parser = argparse.ArgumentParser()
|
2015-09-05 10:56:30 -04:00
|
|
|
parser.add_argument('-a', '--api_key', help='Todoist API Key')
|
|
|
|
parser.add_argument('-l', '--label', help='The next action label to use', default='next_action')
|
|
|
|
parser.add_argument('-d', '--delay', help='Specify the delay in seconds between syncs', default=5, type=int)
|
2015-08-29 14:15:09 -04:00
|
|
|
parser.add_argument('--debug', help='Enable debugging', action='store_true')
|
|
|
|
parser.add_argument('--inbox', help='The method the Inbox project should be processed',
|
2015-09-05 10:56:30 -04:00
|
|
|
default='parallel', choices=['parallel', 'serial'])
|
2015-12-12 09:17:25 -05:00
|
|
|
parser.add_argument('--parallel_suffix', default='.')
|
|
|
|
parser.add_argument('--serial_suffix', default='_')
|
2015-09-04 17:15:54 -04:00
|
|
|
parser.add_argument('--hide_future', help='Hide future dated next actions until the specified number of days',
|
2015-09-05 10:56:30 -04:00
|
|
|
default=7, type=int)
|
2015-12-12 09:35:08 -05:00
|
|
|
parser.add_argument('--onetime', help='Update Todoist once and exit', action='store_true')
|
2015-08-29 14:15:09 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# Set debug
|
2015-09-05 10:56:30 -04:00
|
|
|
if args.debug:
|
2014-11-15 15:31:36 -05:00
|
|
|
log_level = logging.DEBUG
|
|
|
|
else:
|
|
|
|
log_level = logging.INFO
|
|
|
|
logging.basicConfig(level=log_level)
|
2015-08-29 14:15:09 -04:00
|
|
|
|
|
|
|
# Check we have a API key
|
|
|
|
if not args.api_key:
|
2014-11-15 14:49:52 -05:00
|
|
|
logging.error('No API key set, exiting...')
|
|
|
|
sys.exit(1)
|
2015-04-30 20:02:50 -04:00
|
|
|
|
2015-08-29 14:15:09 -04:00
|
|
|
# Run the initial sync
|
2015-04-30 20:02:50 -04:00
|
|
|
logging.debug('Connecting to the Todoist API')
|
2015-08-29 14:15:09 -04:00
|
|
|
api = TodoistAPI(token=args.api_key)
|
2015-04-30 20:02:50 -04:00
|
|
|
logging.debug('Syncing the current state from the API')
|
|
|
|
api.sync(resource_types=['projects', 'labels', 'items'])
|
|
|
|
|
2015-08-29 14:15:09 -04:00
|
|
|
# Check the next action label exists
|
|
|
|
labels = api.labels.all(lambda x: x['name'] == args.label)
|
2015-04-30 20:02:50 -04:00
|
|
|
if len(labels) > 0:
|
|
|
|
label_id = labels[0]['id']
|
2015-08-29 14:15:09 -04:00
|
|
|
logging.debug('Label %s found as label id %d', args.label, label_id)
|
2015-04-30 20:02:50 -04:00
|
|
|
else:
|
2015-08-29 14:15:09 -04:00
|
|
|
logging.error("Label %s doesn't exist, please create it or change TODOIST_NEXT_ACTION_LABEL.", args.label)
|
2015-04-30 20:02:50 -04:00
|
|
|
sys.exit(1)
|
|
|
|
|
2015-08-29 14:15:09 -04:00
|
|
|
def get_project_type(project_object):
|
2016-02-20 12:25:35 -05:00
|
|
|
"""Identifies how a project should be handled."""
|
2015-08-29 14:15:09 -04:00
|
|
|
name = project_object['name'].strip()
|
|
|
|
if project['name'] == 'Inbox':
|
|
|
|
return args.inbox
|
|
|
|
elif name[-1] == args.parallel_suffix:
|
|
|
|
return 'parallel'
|
|
|
|
elif name[-1] == args.serial_suffix:
|
|
|
|
return 'serial'
|
|
|
|
|
2016-02-03 17:33:45 -05:00
|
|
|
def get_item_type(item):
|
2016-02-20 12:25:35 -05:00
|
|
|
"""Identifies how a item with sub items should be handled."""
|
2016-02-03 17:33:45 -05:00
|
|
|
name = item['content'].strip()
|
|
|
|
if name[-1] == args.parallel_suffix:
|
|
|
|
return 'parallel'
|
|
|
|
elif name[-1] == args.serial_suffix:
|
|
|
|
return 'serial'
|
|
|
|
|
|
|
|
def add_label(item, label):
|
|
|
|
if label not in item['labels']:
|
|
|
|
labels = item['labels']
|
|
|
|
logging.debug('Updating %s with label', item['content'])
|
|
|
|
labels.append(label)
|
|
|
|
api.items.update(item['id'], labels=labels)
|
|
|
|
|
|
|
|
def remove_label(item, label):
|
|
|
|
if label in item['labels']:
|
|
|
|
labels = item['labels']
|
|
|
|
logging.debug('Updating %s without label', item['content'])
|
|
|
|
labels.remove(label)
|
|
|
|
api.items.update(item['id'], labels=labels)
|
|
|
|
|
2015-08-29 14:15:09 -04:00
|
|
|
# Main loop
|
2014-11-15 14:45:52 -05:00
|
|
|
while True:
|
2015-12-12 08:54:42 -05:00
|
|
|
try:
|
|
|
|
api.sync(resource_types=['projects', 'labels', 'items'])
|
|
|
|
except Exception as e:
|
|
|
|
logging.exception('Error trying to sync with Todoist API: %s' % str(e))
|
|
|
|
else:
|
|
|
|
for project in api.projects.all():
|
|
|
|
project_type = get_project_type(project)
|
|
|
|
if project_type:
|
|
|
|
logging.debug('Project %s being processed as %s', project['name'], project_type)
|
|
|
|
|
2016-02-20 12:25:35 -05:00
|
|
|
# Get all items for the project, sort by the item_order field.
|
2015-12-12 08:54:42 -05:00
|
|
|
items = sorted(api.items.all(lambda x: x['project_id'] == project['id']), key=lambda x: x['item_order'])
|
|
|
|
|
|
|
|
for item in items:
|
|
|
|
|
|
|
|
# If its too far in the future, remove the next_action tag and skip
|
|
|
|
if args.hide_future > 0 and 'due_date_utc' in item.data and item['due_date_utc'] is not None:
|
|
|
|
due_date = datetime.strptime(item['due_date_utc'], '%a %d %b %Y %H:%M:%S +0000')
|
|
|
|
future_diff = (due_date - datetime.utcnow()).total_seconds()
|
|
|
|
if future_diff >= (args.hide_future * 86400):
|
2016-02-03 17:33:45 -05:00
|
|
|
remove_label(item, label_id)
|
2015-12-12 08:54:42 -05:00
|
|
|
continue
|
|
|
|
|
2016-02-03 17:33:45 -05:00
|
|
|
item_type = get_item_type(item)
|
|
|
|
child_items = get_subitems(items, item)
|
|
|
|
if item_type:
|
|
|
|
logging.debug('Identified %s as %s type', item['content'], item_type)
|
|
|
|
|
|
|
|
if item_type or len(child_items) > 0:
|
|
|
|
# Process serial tagged items
|
|
|
|
if item_type == 'serial':
|
|
|
|
for idx, child_item in enumerate(child_items):
|
|
|
|
if idx == 0:
|
|
|
|
add_label(child_item, label_id)
|
|
|
|
else:
|
|
|
|
remove_label(child_item, label_id)
|
|
|
|
# Process parallel tagged items or untagged parents
|
2015-12-12 08:54:42 -05:00
|
|
|
else:
|
2016-02-03 17:33:45 -05:00
|
|
|
for child_item in child_items:
|
|
|
|
add_label(child_item, label_id)
|
|
|
|
|
|
|
|
# Remove the label from the parent
|
|
|
|
remove_label(item, label_id)
|
|
|
|
|
|
|
|
# Process items as per project type on indent 1 if untagged
|
|
|
|
else:
|
|
|
|
if item['indent'] == 1:
|
|
|
|
if project_type == 'serial':
|
|
|
|
if item['item_order'] == 1:
|
|
|
|
add_label(item, label_id)
|
|
|
|
else:
|
|
|
|
remove_label(item, label_id)
|
|
|
|
elif project_type == 'parallel':
|
|
|
|
add_label(item, label_id)
|
|
|
|
|
|
|
|
if len(api.queue):
|
2016-02-20 12:25:35 -05:00
|
|
|
logging.debug('%d changes queued for sync... commiting to Todoist.', len(api.queue))
|
2016-02-03 17:33:45 -05:00
|
|
|
api.commit()
|
2016-02-20 12:25:35 -05:00
|
|
|
else:
|
|
|
|
logging.debug('No changes queued, skipping sync.')
|
2015-12-12 09:35:08 -05:00
|
|
|
|
2016-02-20 12:25:35 -05:00
|
|
|
# If onetime is set, exit after first execution.
|
2015-12-12 09:35:08 -05:00
|
|
|
if args.onetime:
|
|
|
|
break
|
2016-02-20 12:25:35 -05:00
|
|
|
|
2015-08-29 14:15:09 -04:00
|
|
|
logging.debug('Sleeping for %d seconds', args.delay)
|
|
|
|
time.sleep(args.delay)
|
2014-11-15 14:45:52 -05:00
|
|
|
|
2014-03-10 00:11:58 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2014-11-15 14:45:52 -05:00
|
|
|
main()
|