2020-05-09 11:08:53 -04:00
|
|
|
#!/usr/bin/python3
|
2020-05-21 04:00:06 -04:00
|
|
|
# Autodoist v1.0.3
|
2020-05-09 11:08:53 -04:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import argparse
|
2020-05-24 05:11:17 -04:00
|
|
|
import requests
|
2020-05-09 11:08:53 -04:00
|
|
|
import time
|
|
|
|
import sys
|
|
|
|
from datetime import datetime
|
|
|
|
|
2020-05-24 05:11:17 -04:00
|
|
|
from todoist.api import TodoistAPI
|
|
|
|
|
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
def main():
|
2020-05-24 05:11:17 -04:00
|
|
|
|
|
|
|
# Version
|
|
|
|
current_version = 'v1.1'
|
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
"""Main process function."""
|
|
|
|
parser = argparse.ArgumentParser()
|
2020-05-21 04:00:06 -04:00
|
|
|
parser.add_argument('-a', '--api_key', help='Todoist API Key')
|
2020-05-24 05:11:17 -04:00
|
|
|
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=10, type=int)
|
|
|
|
parser.add_argument(
|
|
|
|
'-r', '--recurring', help='Enable re-use of recurring lists', action='store_true')
|
|
|
|
parser.add_argument('--debug', help='Enable debugging',
|
|
|
|
action='store_true')
|
2020-05-09 11:08:53 -04:00
|
|
|
parser.add_argument('--inbox', help='The method the Inbox project should be processed',
|
2020-05-17 12:02:38 -04:00
|
|
|
default=None, choices=['parallel', 'sequential'])
|
2020-05-09 11:08:53 -04:00
|
|
|
parser.add_argument('--parallel_suffix', default='//')
|
2020-05-17 12:02:38 -04:00
|
|
|
parser.add_argument('--sequential_suffix', default='--')
|
2020-05-09 11:08:53 -04:00
|
|
|
parser.add_argument('--hide_future', help='Hide future dated next actions until the specified number of days',
|
|
|
|
default=7, type=int)
|
2020-05-24 05:11:17 -04:00
|
|
|
parser.add_argument(
|
|
|
|
'--onetime', help='Update Todoist once and exit', action='store_true')
|
|
|
|
parser.add_argument(
|
|
|
|
'--nocache', help='Disables caching data to disk for quicker syncing', action='store_true')
|
2020-05-09 11:08:53 -04:00
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
def initialise(args):
|
|
|
|
# Set debug
|
|
|
|
if args.debug:
|
|
|
|
log_level = logging.DEBUG
|
|
|
|
else:
|
|
|
|
log_level = logging.INFO
|
2020-05-09 16:03:43 -04:00
|
|
|
|
|
|
|
logging.basicConfig(handlers=[logging.FileHandler('DEBUG.log', 'w+', 'utf-8')],
|
2020-05-24 05:11:17 -04:00
|
|
|
level=log_level,
|
|
|
|
format='%(asctime)s %(levelname)-8s %(message)s',
|
2020-05-09 16:03:43 -04:00
|
|
|
datefmt='%Y-%m-%d %H:%M:%S')
|
2020-05-09 11:08:53 -04:00
|
|
|
|
|
|
|
# Check we have a API key
|
|
|
|
if not args.api_key:
|
|
|
|
logging.error('No API key set, exiting...')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Run the initial sync
|
|
|
|
logging.debug('Connecting to the Todoist API')
|
|
|
|
|
|
|
|
api_arguments = {'token': args.api_key}
|
|
|
|
if args.nocache:
|
|
|
|
logging.debug('Disabling local caching')
|
|
|
|
api_arguments['cache'] = None
|
|
|
|
|
|
|
|
api = TodoistAPI(**api_arguments)
|
|
|
|
logging.debug('Syncing the current state from the API')
|
|
|
|
api.sync()
|
|
|
|
|
|
|
|
# Check the next action label exists
|
|
|
|
labels = api.labels.all(lambda x: x['name'] == args.label)
|
|
|
|
if len(labels) > 0:
|
|
|
|
label_id = labels[0]['id']
|
2020-05-24 05:11:17 -04:00
|
|
|
logging.debug('Label \'%s\' found as label id %d',
|
|
|
|
args.label, label_id)
|
2020-05-09 11:08:53 -04:00
|
|
|
else:
|
2020-05-24 05:11:17 -04:00
|
|
|
logging.error(
|
|
|
|
"Label \'%s\' doesn't exist, please create it or change TODOIST_NEXT_ACTION_LABEL.", args.label)
|
2020-05-09 11:08:53 -04:00
|
|
|
sys.exit(1)
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
return api, label_id
|
|
|
|
|
2020-05-24 05:11:17 -04:00
|
|
|
def check_for_update(current_version):
|
|
|
|
updateurl = 'https://api.github.com/repos/Hoffelhas/autodoist/releases'
|
|
|
|
|
|
|
|
try:
|
|
|
|
r = requests.get(updateurl)
|
|
|
|
r.raise_for_status()
|
|
|
|
release_info_json = r.json()
|
|
|
|
|
|
|
|
if not current_version == release_info_json[0]['tag_name']:
|
|
|
|
logging.info("Your version is not up-to-date! \nYour version: {}\nLatest version: {}\nSee latest version at: {}".format(
|
|
|
|
current_version, release_info_json[0]['tag_name'], release_info_json[0]['html_url']))
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
except requests.exceptions.ConnectionError as e:
|
|
|
|
logging.error(
|
|
|
|
"Error while checking for updates (Connection error): {}".format(e))
|
|
|
|
return 1
|
|
|
|
except requests.exceptions.HTTPError as e:
|
|
|
|
logging.error(
|
|
|
|
"Error while checking for updates (HTTP error): {}".format(e))
|
|
|
|
return 1
|
|
|
|
except requests.exceptions.RequestException as e:
|
|
|
|
logging.error("Error while checking for updates: {}".format(e))
|
|
|
|
return 1
|
|
|
|
|
|
|
|
def get_type(object, key):
|
2020-05-17 12:02:38 -04:00
|
|
|
len_suffix = [len(args.parallel_suffix), len(args.sequential_suffix)]
|
2020-05-09 11:08:53 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
old_type = object[key]
|
|
|
|
except Exception as e:
|
2020-05-09 16:03:43 -04:00
|
|
|
# logging.debug('No defined project_type: %s' % str(e))
|
2020-05-24 05:11:17 -04:00
|
|
|
old_type = None
|
2020-05-09 11:08:53 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
name = object['name'].strip()
|
|
|
|
except:
|
|
|
|
name = object['content'].strip()
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
if name == 'Inbox':
|
|
|
|
current_type = args.inbox
|
|
|
|
elif name[-len_suffix[0]:] == args.parallel_suffix:
|
2020-05-24 05:11:17 -04:00
|
|
|
current_type = 'parallel'
|
2020-05-17 12:02:38 -04:00
|
|
|
elif name[-len_suffix[1]:] == args.sequential_suffix:
|
|
|
|
current_type = 'sequential'
|
2020-05-09 11:08:53 -04:00
|
|
|
else:
|
|
|
|
current_type = None
|
|
|
|
|
|
|
|
# Check if project type changed with respect to previous run
|
|
|
|
if old_type == current_type:
|
|
|
|
type_changed = 0
|
|
|
|
else:
|
|
|
|
type_changed = 1
|
|
|
|
object[key] = current_type
|
|
|
|
|
|
|
|
return current_type, type_changed
|
|
|
|
|
|
|
|
def get_project_type(project_object):
|
|
|
|
"""Identifies how a project should be handled."""
|
2020-05-24 05:11:17 -04:00
|
|
|
project_type, project_type_changed = get_type(
|
|
|
|
project_object, 'project_type')
|
2020-05-09 11:08:53 -04:00
|
|
|
|
|
|
|
return project_type, project_type_changed
|
|
|
|
|
|
|
|
def get_item_type(item, project_type):
|
|
|
|
"""Identifies how a item with sub items should be handled."""
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
if project_type is None and item['parent_id'] != 0:
|
|
|
|
try:
|
|
|
|
item_type = item['parent_type']
|
|
|
|
item_type_changed = 1
|
|
|
|
except:
|
2020-05-24 05:11:17 -04:00
|
|
|
item_type, item_type_changed = get_type(item, 'item_type')
|
2020-05-09 11:08:53 -04:00
|
|
|
else:
|
2020-05-24 05:11:17 -04:00
|
|
|
item_type, item_type_changed = get_type(item, 'item_type')
|
2020-05-09 11:08:53 -04:00
|
|
|
|
|
|
|
return item_type, item_type_changed
|
|
|
|
|
|
|
|
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('Removing \'%s\' of its label', item['content'])
|
|
|
|
labels.remove(label)
|
|
|
|
api.items.update(item['id'], labels=labels)
|
2020-05-24 05:11:17 -04:00
|
|
|
|
|
|
|
# Check for updates
|
|
|
|
check_for_update(current_version)
|
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
# Initialise api
|
|
|
|
api, label_id = initialise(args)
|
|
|
|
|
|
|
|
# Main loop
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
api.sync()
|
|
|
|
except Exception as e:
|
2020-05-24 05:11:17 -04:00
|
|
|
logging.exception(
|
|
|
|
'Error trying to sync with Todoist API: %s' % str(e))
|
2020-05-09 11:08:53 -04:00
|
|
|
else:
|
|
|
|
for project in api.projects.all():
|
|
|
|
|
|
|
|
# Get project type
|
|
|
|
project_type, project_type_changed = get_project_type(project)
|
2020-05-24 05:11:17 -04:00
|
|
|
logging.debug('Project \'%s\' being processed as %s',
|
|
|
|
project['name'], project_type)
|
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
# Get all items for the project
|
2020-05-24 05:11:17 -04:00
|
|
|
items = api.items.all(
|
|
|
|
lambda x: x['project_id'] == project['id'])
|
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
# Change top parents_id in order to sort later on
|
|
|
|
for item in items:
|
|
|
|
if not item['parent_id']:
|
|
|
|
item['parent_id'] = 0
|
|
|
|
|
|
|
|
# Sort by parent_id and filter for completable items
|
2020-05-24 05:11:17 -04:00
|
|
|
items = sorted(items, key=lambda x: (
|
|
|
|
x['parent_id'], x['child_order']))
|
|
|
|
items = list(
|
|
|
|
filter(lambda x: not x['content'].startswith('*'), items))
|
2020-05-09 11:08:53 -04:00
|
|
|
|
|
|
|
# If project type has been changed, clean everything for good measure
|
|
|
|
if project_type_changed == 1:
|
|
|
|
[remove_label(item, label_id) for item in items]
|
|
|
|
|
2020-05-24 05:11:17 -04:00
|
|
|
# To determine if a task was found on sequential level
|
2020-05-21 04:00:06 -04:00
|
|
|
first_found_project = False
|
2020-05-24 05:11:17 -04:00
|
|
|
first_found_item = True
|
2020-05-17 12:02:38 -04:00
|
|
|
|
2020-05-21 04:00:06 -04:00
|
|
|
for item in items:
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-21 04:00:06 -04:00
|
|
|
# Determine which child_items exist, both all and the ones that have not been checked yet
|
2020-05-24 05:11:17 -04:00
|
|
|
non_checked_items = list(
|
|
|
|
filter(lambda x: x['checked'] == 0, items))
|
|
|
|
child_items_all = list(
|
|
|
|
filter(lambda x: x['parent_id'] == item['id'], items))
|
|
|
|
child_items = list(
|
|
|
|
filter(lambda x: x['parent_id'] == item['id'], non_checked_items))
|
|
|
|
|
2020-05-21 04:00:06 -04:00
|
|
|
# Logic for recurring lists
|
2020-05-09 16:03:43 -04:00
|
|
|
if not args.recurring:
|
2020-05-09 11:08:53 -04:00
|
|
|
try:
|
2020-05-21 04:00:06 -04:00
|
|
|
# If old label is present, reset it
|
2020-05-09 11:08:53 -04:00
|
|
|
if item['r_tag'] == 1:
|
|
|
|
item['r_tag'] = 0
|
|
|
|
except Exception as e:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
if item['parent_id'] == 0:
|
|
|
|
try:
|
|
|
|
if item['due']['is_recurring']:
|
|
|
|
try:
|
2020-05-21 04:00:06 -04:00
|
|
|
# Check if the T0 task date has changed
|
2020-05-09 11:08:53 -04:00
|
|
|
if item['due']['date'] != item['old_date']:
|
|
|
|
# Save the new date
|
|
|
|
item['due']['date'] = item['old_date']
|
|
|
|
|
|
|
|
# Mark children for action
|
2020-05-21 04:00:06 -04:00
|
|
|
for child_item in child_items_all:
|
2020-05-09 11:08:53 -04:00
|
|
|
child_item['r_tag'] = 1
|
|
|
|
except Exception as e:
|
2020-05-21 04:00:06 -04:00
|
|
|
# If date has never been saved before, create a new entry
|
2020-05-24 05:11:17 -04:00
|
|
|
logging.debug(
|
|
|
|
'New recurring task detected: %s' % str(e))
|
2020-05-09 11:08:53 -04:00
|
|
|
item['old_date'] = item['due']['date']
|
|
|
|
api.items.update(item['id'])
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
except Exception as e:
|
2020-05-24 05:11:17 -04:00
|
|
|
logging.debug(
|
|
|
|
'Parent not recurring: %s' % str(e))
|
2020-05-09 11:08:53 -04:00
|
|
|
pass
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
try:
|
|
|
|
if item['r_tag'] == 1:
|
|
|
|
item.update(checked=0)
|
|
|
|
item.update(in_history=0)
|
|
|
|
item['r_tag'] = 0
|
|
|
|
api.items.update(item['id'])
|
|
|
|
|
2020-05-21 04:00:06 -04:00
|
|
|
for child_item in child_items_all:
|
2020-05-09 11:08:53 -04:00
|
|
|
child_item['r_tag'] = 1
|
|
|
|
except Exception as e:
|
|
|
|
logging.debug('Child not recurring: %s' % str(e))
|
|
|
|
pass
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
# Skip processing an item if it has already been checked
|
|
|
|
if item['checked'] == 1:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# Check item type
|
2020-05-24 05:11:17 -04:00
|
|
|
item_type, item_type_changed = get_item_type(
|
|
|
|
item, project_type)
|
|
|
|
logging.debug('Identified \'%s\' as %s type',
|
|
|
|
item['content'], item_type)
|
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
if project_type is None and item_type is None and project_type_changed == 1:
|
|
|
|
# Clean the item and its children
|
|
|
|
remove_label(item, label_id)
|
|
|
|
for child_item in child_items:
|
|
|
|
child_item['parent_type'] = None
|
|
|
|
|
|
|
|
# We can immediately continue
|
|
|
|
continue
|
|
|
|
else:
|
2020-05-21 04:00:06 -04:00
|
|
|
# Define the item_type
|
2020-05-09 11:08:53 -04:00
|
|
|
if item_type is None:
|
|
|
|
item_type = project_type
|
2020-05-21 04:00:06 -04:00
|
|
|
else:
|
|
|
|
# Reset in case that task is tagged as sequential
|
|
|
|
first_found_item = False
|
|
|
|
|
|
|
|
# If it is a parentless task
|
2020-05-12 17:12:14 -04:00
|
|
|
if len(child_items) == 0:
|
2020-05-09 11:08:53 -04:00
|
|
|
if item['parent_id'] == 0:
|
2020-05-17 12:02:38 -04:00
|
|
|
if project_type == 'sequential':
|
2020-05-21 04:00:06 -04:00
|
|
|
if not first_found_project:
|
2020-05-09 11:08:53 -04:00
|
|
|
add_label(item, label_id)
|
2020-05-21 04:00:06 -04:00
|
|
|
first_found_project = True
|
|
|
|
elif not first_found_item:
|
|
|
|
add_label(item, label_id)
|
2020-05-24 05:11:17 -04:00
|
|
|
first_found_item = True
|
2020-05-09 11:08:53 -04:00
|
|
|
else:
|
|
|
|
remove_label(item, label_id)
|
|
|
|
elif project_type == 'parallel':
|
|
|
|
add_label(item, label_id)
|
|
|
|
else:
|
|
|
|
# If only the item type has been defined
|
|
|
|
if item_type:
|
|
|
|
add_label(item, label_id)
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
# If there are children, label them instead
|
|
|
|
if len(child_items) > 0:
|
|
|
|
child_first_found = False
|
|
|
|
|
|
|
|
# Check if state has changed, if so clean for good measure
|
|
|
|
if item_type_changed == 1:
|
2020-05-24 05:11:17 -04:00
|
|
|
[remove_label(child_item, label_id)
|
|
|
|
for child_item in child_items]
|
2020-05-09 11:08:53 -04:00
|
|
|
|
2020-05-21 04:00:06 -04:00
|
|
|
# Process sequential tagged items (item_type can overrule project_type)
|
2020-05-17 12:02:38 -04:00
|
|
|
if item_type == 'sequential':
|
2020-05-09 11:08:53 -04:00
|
|
|
for child_item in child_items:
|
2020-05-21 04:00:06 -04:00
|
|
|
if child_item['checked'] == 0 and not child_first_found and not first_found_project:
|
|
|
|
first_found_project = True
|
|
|
|
child_first_found = True
|
|
|
|
add_label(child_item, label_id)
|
|
|
|
child_item['parent_type'] = item_type
|
|
|
|
elif child_item['checked'] == 0 and not child_first_found and not first_found_item:
|
|
|
|
first_found_item = True
|
2020-05-09 11:08:53 -04:00
|
|
|
child_first_found = True
|
|
|
|
add_label(child_item, label_id)
|
|
|
|
child_item['parent_type'] = item_type
|
|
|
|
else:
|
|
|
|
remove_label(child_item, label_id)
|
|
|
|
# Process parallel tagged items or untagged parents
|
|
|
|
elif item_type == 'parallel':
|
|
|
|
for child_item in child_items:
|
|
|
|
if child_item['checked'] == 0:
|
|
|
|
child_first_found = True
|
|
|
|
add_label(child_item, label_id)
|
|
|
|
child_item['parent_type'] = item_type
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-21 04:00:06 -04:00
|
|
|
# Remove the label from the parent (needed for if recurring list is reset)
|
2020-05-09 11:08:53 -04:00
|
|
|
if item_type and child_first_found:
|
|
|
|
remove_label(item, label_id)
|
|
|
|
|
|
|
|
# If item is 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:
|
2020-05-24 05:11:17 -04:00
|
|
|
due_date = datetime.strptime(
|
|
|
|
item['due_date_utc'], '%a %d %b %Y %H:%M:%S +0000')
|
|
|
|
future_diff = (
|
|
|
|
due_date - datetime.utcnow()).total_seconds()
|
2020-05-09 11:08:53 -04:00
|
|
|
if future_diff >= (args.hide_future * 86400):
|
|
|
|
remove_label(item, label_id)
|
|
|
|
continue
|
|
|
|
if len(api.queue):
|
2020-05-24 05:11:17 -04:00
|
|
|
logging.debug(
|
|
|
|
'%d changes queued for sync... commiting to Todoist.', len(api.queue))
|
2020-05-09 11:08:53 -04:00
|
|
|
api.commit()
|
|
|
|
else:
|
|
|
|
logging.debug('No changes queued, skipping sync.')
|
|
|
|
|
|
|
|
# If onetime is set, exit after first execution.
|
|
|
|
if args.onetime:
|
|
|
|
break
|
|
|
|
|
|
|
|
logging.debug('Sleeping for %d seconds', args.delay)
|
|
|
|
time.sleep(args.delay)
|
|
|
|
|
2020-05-24 05:11:17 -04:00
|
|
|
|
2020-05-09 11:08:53 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|