Merge branch 'release/0.5'

pull/15/head
shadowgate15 2020-03-10 22:27:57 -05:00
commit 0341048d69
5 changed files with 42 additions and 27 deletions

2
.gitignore vendored
View File

@ -56,3 +56,5 @@ target/
# IDEA # IDEA
.idea/ .idea/
.env

View File

@ -1 +1 @@
nextaction: python nextaction.py web: python nextaction.py -a $TODOIST_API_KEY -l $TODOIST_NEXT_ACTION_LABEL $DEBUG

View File

@ -3,33 +3,34 @@
import logging import logging
import argparse import argparse
# noinspection PyPackageRequirements
from todoist.api import TodoistAPI from todoist.api import TodoistAPI
import time import time
import sys import sys
from datetime import datetime from datetime import datetime
CHECKED = 1
def get_subitems(items, parent_item=None): def get_subitems(items, parent_item=None):
"""Search a flat item list for child items.""" """Search a flat item list for child items."""
result_items = [] result_items = []
found = False found = False
if parent_item: if parent_item:
required_indent = parent_item['indent'] + 1 required_parent_id = parent_item['parent_id'] + 1
else: else:
required_indent = 1 required_parent_id = 1
for item in items: for item in items:
if parent_item: if parent_item:
if not found and item['id'] != parent_item['id']: if not found and item['id'] != parent_item['id']:
continue continue
else: else:
found = True found = True
if item['indent'] == parent_item['indent'] and item['id'] != parent_item['id']: if item['parent_id'] == parent_item['parent_id'] and item['id'] != parent_item['id']:
return result_items return result_items
elif item['indent'] == required_indent and found: elif item['parent_id'] == required_parent_id and found:
result_items.append(item) result_items.append(item)
elif item['indent'] == required_indent: elif item['parent_id'] == required_parent_id:
result_items.append(item) result_items.append(item)
return result_items return result_items
@ -48,6 +49,7 @@ def main():
parser.add_argument('--hide_future', help='Hide future dated next actions until the specified number of days', parser.add_argument('--hide_future', help='Hide future dated next actions until the specified number of days',
default=7, type=int) default=7, type=int)
parser.add_argument('--onetime', help='Update Todoist once and exit', action='store_true') 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')
args = parser.parse_args() args = parser.parse_args()
# Set debug # Set debug
@ -64,23 +66,29 @@ def main():
# Run the initial sync # Run the initial sync
logging.debug('Connecting to the Todoist API') logging.debug('Connecting to the Todoist API')
api = TodoistAPI(token=args.api_key)
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') logging.debug('Syncing the current state from the API')
api.sync(resource_types=['projects', 'labels', 'items']) api.sync()
# Check the next action label exists # Check the next action label exists
labels = api.labels.all(lambda x: x['name'] == args.label) labels = api.labels.all(lambda x: x['name'] == args.label)
if len(labels) > 0: if len(labels) > 0:
label_id = labels[0]['id'] label_id = labels[0]['id']
logging.debug('Label %s found as label id %d', args.label, label_id) logging.debug('Label \'%s\' found as label id %d', args.label, label_id)
else: else:
logging.error("Label %s doesn't exist, please create it or change TODOIST_NEXT_ACTION_LABEL.", args.label) logging.error("Label \'%s\' doesn't exist, please create it or change TODOIST_NEXT_ACTION_LABEL.", args.label)
sys.exit(1) sys.exit(1)
def get_project_type(project_object): def get_project_type(project_object):
"""Identifies how a project should be handled.""" """Identifies how a project should be handled."""
name = project_object['name'].strip() name = project_object['name'].strip()
if project['name'] == 'Inbox': if name == 'Inbox':
return args.inbox return args.inbox
elif name[-1] == args.parallel_suffix: elif name[-1] == args.parallel_suffix:
return 'parallel' return 'parallel'
@ -98,31 +106,32 @@ def main():
def add_label(item, label): def add_label(item, label):
if label not in item['labels']: if label not in item['labels']:
labels = item['labels'] labels = item['labels']
logging.debug('Updating %s with label', item['content']) logging.debug('Updating \'%s\' with label', item['content'])
labels.append(label) labels.append(label)
api.items.update(item['id'], labels=labels) api.items.update(item['id'], labels=labels)
def remove_label(item, label): def remove_label(item, label):
if label in item['labels']: if label in item['labels']:
labels = item['labels'] labels = item['labels']
logging.debug('Updating %s without label', item['content']) logging.debug('Updating \'%s\' without label', item['content'])
labels.remove(label) labels.remove(label)
api.items.update(item['id'], labels=labels) api.items.update(item['id'], labels=labels)
# Main loop # Main loop
while True: while True:
try: try:
api.sync(resource_types=['projects', 'labels', 'items']) api.sync()
except Exception as e: except Exception as e:
logging.exception('Error trying to sync with Todoist API: %s' % str(e)) logging.exception('Error trying to sync with Todoist API: %s' % str(e))
else: else:
for project in api.projects.all(): for project in api.projects.all():
project_type = get_project_type(project) project_type = get_project_type(project)
if project_type: if project_type:
logging.debug('Project %s being processed as %s', project['name'], project_type) logging.debug('Project \'%s\' being processed as %s', project['name'], project_type)
# Get all items for the project, sort by the item_order field. # Get all items for the project, sort by the item_order field.
items = sorted(api.items.all(lambda x: x['project_id'] == project['id']), key=lambda x: x['item_order']) items = sorted(api.items.all(lambda x: x['project_id'] == project['id']),
key=lambda x: x['child_order'])
for item in items: for item in items:
@ -135,16 +144,20 @@ def main():
continue continue
item_type = get_item_type(item) item_type = get_item_type(item)
child_items = get_subitems(items, item) # child_items = get_subitems(items, item)
child_items = sorted(list(filter(lambda x: x['parent_id'] == item['id'], items)),
key=lambda x: x['child_order'])
if item_type: if item_type:
logging.debug('Identified %s as %s type', item['content'], item_type) logging.debug('Identified \'%s\' as %s type', item['content'], item_type)
if item_type or len(child_items) > 0: if item_type or len(child_items) > 0:
# Process serial tagged items # Process serial tagged items
if item_type == 'serial': if item_type == 'serial':
for idx, child_item in enumerate(child_items): first_found = False
if idx == 0: for child_item in child_items:
if child_item['checked'] == 0 and not first_found:
add_label(child_item, label_id) add_label(child_item, label_id)
first_found = True
else: else:
remove_label(child_item, label_id) remove_label(child_item, label_id)
# Process parallel tagged items or untagged parents # Process parallel tagged items or untagged parents
@ -155,11 +168,11 @@ def main():
# Remove the label from the parent # Remove the label from the parent
remove_label(item, label_id) remove_label(item, label_id)
# Process items as per project type on indent 1 if untagged # Process items(w/ project as parent and no children) per project type
else: else:
if item['indent'] == 1: if item['parent_id'] is None:
if project_type == 'serial': if project_type == 'serial':
if item['item_order'] == 1: if item['child_order'] == 1:
add_label(item, label_id) add_label(item, label_id)
else: else:
remove_label(item, label_id) remove_label(item, label_id)

View File

@ -1 +1 @@
todoist-python==0.2.26 todoist-python>=8.1.1

View File

@ -2,7 +2,7 @@ from setuptools import setup
setup( setup(
name='NextAction', name='NextAction',
version='0.4', version='0.5',
py_modules=['nextaction'], py_modules=['nextaction'],
url='https://github.com/nikdoof/NextAction', url='https://github.com/nikdoof/NextAction',
license='MIT', license='MIT',