mirror of
https://github.com/nikdoof/NextAction.git
synced 2025-12-26 16:59:23 +00:00
Compare commits
19 Commits
todoist-py
...
support/0.
| Author | SHA1 | Date | |
|---|---|---|---|
| 0f29c7c4eb | |||
| 68118e5b8a | |||
| a52b5c4288 | |||
| 861c7e1b10 | |||
| cde3a631b3 | |||
| 420e06abfb | |||
| 3bf53c7436 | |||
| 4c82e9465c | |||
| f925615238 | |||
| bd3cbead8c | |||
| 130b2f8ccb | |||
| 97bd4f7fb4 | |||
| 2564f972d8 | |||
| 5baa671d83 | |||
| 5554c636d9 | |||
| 54f770f5f7 | |||
| 52aff18d90 | |||
| 360be44dd7 | |||
| a62ae17866 |
1
LICENSE
1
LICENSE
@@ -1,6 +1,7 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Adam Kramer
|
||||
Copyright (c) 2015 Andrew Williams
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
22
README.md
22
README.md
@@ -4,7 +4,7 @@ NextAction
|
||||
A more GTD-like workflow for Todoist. Uses the REST API to add and remove a `@next_action` label from tasks.
|
||||
|
||||
This program looks at every list in your Todoist account.
|
||||
Any list that ends with `--` or `=` is treated specially, and processed by NextAction.
|
||||
Any list that ends with `_` or `.` is treated specially, and processed by NextAction.
|
||||
|
||||
Note that NextAction requires Todoist Premium to function properly, as labels are a premium feature.
|
||||
|
||||
@@ -19,32 +19,22 @@ Activating NextAction
|
||||
|
||||
Sequential list processing
|
||||
--------------------------
|
||||
If a list ends with `--`, the top level of tasks will be treated as a priority queue and the most important will be labeled `@next_action`.
|
||||
If a project or task ends with `_`, the child tasks will be treated as a priority queue and the most important will be labeled `@next_action`.
|
||||
Importance is determined by order in the list
|
||||
|
||||
Parallel list processing
|
||||
------------------------
|
||||
If a list name ends with `=`, the top level of tasks will be treated as parallel `@next_action`s.
|
||||
The waterfall processing will be applied the same way as sequential lists - every parent task will be treated as sequential. This can be overridden by appending `=` to the name of the parent task.
|
||||
If a project or task name ends with `.`, the child tasks will be treated as parallel `@next_action`s.
|
||||
The waterfall processing will be applied the same way as sequential lists - every parent task will be treated as sequential. This can be overridden by appending `_` to the name of the parent task.
|
||||
|
||||
Executing NextAction
|
||||
====================
|
||||
|
||||
You can run NexAction from any system that supports Python, and also deploy to Heroku as a constant running service
|
||||
You can run NexAction from any system that supports Python.
|
||||
|
||||
Running NextAction
|
||||
------------------
|
||||
|
||||
NextAction will read your environment to retrieve your Todoist API key, so to run on a Linux/Mac OSX you can use the following commandline
|
||||
|
||||
TODOIST_API_KEY="XYZ" python nextaction.py
|
||||
|
||||
Heroku Support
|
||||
--------------
|
||||
|
||||
[](https://heroku.com/deploy)
|
||||
|
||||
This package is ready to be pushed to a Heroku instance with minimal configuration values:
|
||||
|
||||
* ```TODOIST_API_KEY``` - Your Todoist API Key
|
||||
* ```TODOIST_NEXT_ACTION_LABEL``` - The label to use in Todoist for next actions (defaults to next_action)
|
||||
python nextaction.py -a <API Key>
|
||||
|
||||
37
app.json
37
app.json
@@ -1,37 +0,0 @@
|
||||
{
|
||||
"name": "NextAction",
|
||||
"description": "Todoist API application to provide a auto-populated next action label",
|
||||
"repository": "https://github.com/nikdoof/NextAction",
|
||||
"keywords": ["python"],
|
||||
"env": {
|
||||
"TODOIST_API_KEY": {
|
||||
"description": "Your Todoist API Key",
|
||||
"required": true
|
||||
},
|
||||
"TODOIST_NEXT_ACTION_LABEL": {
|
||||
"description": "The Todoist label to use for next actions.",
|
||||
"value": "next_action",
|
||||
"required": false
|
||||
},
|
||||
"TODOIST_SYNC_DELAY": {
|
||||
"description": "The number of seconds to wait between syncs.",
|
||||
"value": "5",
|
||||
"required": false
|
||||
},
|
||||
"TODOIST_INBOX_HANDLING": {
|
||||
"description": "What method to use for the Inbox, sequence or parallel",
|
||||
"value": "parallel",
|
||||
"required": false
|
||||
},
|
||||
"TODODIST_PARALLEL_SUFFIX": {
|
||||
"description": "What sequence of characters to use to identify parallel processed projects",
|
||||
"value": "=",
|
||||
"required": false
|
||||
},
|
||||
"TODODIST_SERIAL_SUFFIX": {
|
||||
"description": "What sequence of characters to use to identify serial processed projects",
|
||||
"value": "-",
|
||||
"required": false
|
||||
}
|
||||
}
|
||||
}
|
||||
179
nextaction.py
179
nextaction.py
@@ -1,30 +1,14 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import time
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
|
||||
# noinspection PyPackageRequirements
|
||||
from todoist.api import TodoistAPI
|
||||
|
||||
|
||||
API_TOKEN = os.environ.get('TODOIST_API_KEY', None)
|
||||
NEXT_ACTION_LABEL = os.environ.get('TODOIST_NEXT_ACTION_LABEL', 'next_action')
|
||||
SYNC_DELAY = int(os.environ.get('TODOIST_SYNC_DELAY', '5'))
|
||||
INBOX_HANDLING = os.environ.get('TODOIST_INBOX_HANDLING', 'parallel')
|
||||
PARALLEL_SUFFIX = os.environ.get('TODOIST_PARALLEL_SUFFIX', '=')
|
||||
SERIAL_SUFFIX = os.environ.get('TODOIST_SERIAL_SUFFIX', '-')
|
||||
|
||||
|
||||
def get_project_type(project):
|
||||
"""Identifies how a project should be handled"""
|
||||
name = project['name'].strip()
|
||||
if project['name'] == 'Inbox':
|
||||
return INBOX_HANDLING
|
||||
elif name[-1] == PARALLEL_SUFFIX:
|
||||
return 'parallel'
|
||||
elif name[-1] == SERIAL_SUFFIX:
|
||||
return 'serial'
|
||||
import time
|
||||
import sys
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
def get_subitems(items, parent_item=None):
|
||||
@@ -43,71 +27,152 @@ def get_subitems(items, parent_item=None):
|
||||
found = True
|
||||
if item['indent'] == parent_item['indent'] and item['id'] != parent_item['id']:
|
||||
return result_items
|
||||
elif item['indent'] == required_indent and found:
|
||||
result_items.append(item)
|
||||
elif item['indent'] == required_indent:
|
||||
result_items.append(item)
|
||||
return result_items
|
||||
|
||||
|
||||
def main():
|
||||
if os.environ.get('TODOIST_DEBUG', None):
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
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)
|
||||
parser.add_argument('--debug', help='Enable debugging', action='store_true')
|
||||
parser.add_argument('--inbox', help='The method the Inbox project should be processed',
|
||||
default='parallel', choices=['parallel', 'serial'])
|
||||
parser.add_argument('--parallel_suffix', default='.')
|
||||
parser.add_argument('--serial_suffix', default='_')
|
||||
parser.add_argument('--hide_future', help='Hide future dated next actions until the specified number of days',
|
||||
default=7, type=int)
|
||||
parser.add_argument('--onetime', help='Update Todoist once and exit', action='store_true')
|
||||
args = parser.parse_args()
|
||||
|
||||
# Set debug
|
||||
if args.debug:
|
||||
log_level = logging.DEBUG
|
||||
else:
|
||||
log_level = logging.INFO
|
||||
logging.basicConfig(level=log_level)
|
||||
if not API_TOKEN:
|
||||
|
||||
# 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 = TodoistAPI(token=API_TOKEN)
|
||||
api = TodoistAPI(token=args.api_key)
|
||||
logging.debug('Syncing the current state from the API')
|
||||
api.sync(resource_types=['projects', 'labels', 'items'])
|
||||
|
||||
labels = api.labels.all(lambda x: x['name'] == NEXT_ACTION_LABEL)
|
||||
# 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']
|
||||
logging.debug('Label %s found as label id %d', NEXT_ACTION_LABEL, label_id)
|
||||
logging.debug('Label %s found as label id %d', args.label, label_id)
|
||||
else:
|
||||
logging.error("Label %s doesn't exist, please create it or change TODOIST_NEXT_ACTION_LABEL.", NEXT_ACTION_LABEL)
|
||||
logging.error("Label %s doesn't exist, please create it or change TODOIST_NEXT_ACTION_LABEL.", args.label)
|
||||
sys.exit(1)
|
||||
|
||||
def get_project_type(project_object):
|
||||
"""Identifies how a project should be handled"""
|
||||
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'
|
||||
|
||||
def get_item_type(item):
|
||||
"""Identifies how a item with sub items should be handled"""
|
||||
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)
|
||||
|
||||
# Main loop
|
||||
while True:
|
||||
api.sync(resource_types=['projects', 'labels', 'items'])
|
||||
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)
|
||||
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)
|
||||
|
||||
# Parallel
|
||||
if project_type == 'parallel':
|
||||
items = api.items.all(lambda x: x['project_id'] == project['id'])
|
||||
for item in items:
|
||||
labels = item['labels']
|
||||
if label_id not in labels:
|
||||
logging.debug('Updating %s with label', item['content'])
|
||||
labels.append(label_id)
|
||||
item.update(labels=labels)
|
||||
|
||||
# Serial
|
||||
if project_type == 'serial':
|
||||
items = sorted(api.items.all(lambda x: x['project_id'] == project['id']), key=lambda x: x['item_order'])
|
||||
|
||||
for item in items:
|
||||
labels = item['labels']
|
||||
if item['item_order'] == 1:
|
||||
|
||||
if label_id not in labels:
|
||||
labels.append(label_id)
|
||||
logging.debug('Updating %s with label', item['content'])
|
||||
item.update(labels=labels)
|
||||
# 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):
|
||||
remove_label(item, label_id)
|
||||
continue
|
||||
|
||||
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
|
||||
else:
|
||||
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 label_id in labels:
|
||||
labels.remove(label_id)
|
||||
logging.debug('Updating %s without label', item['content'])
|
||||
item.update(labels=labels)
|
||||
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)
|
||||
|
||||
api.sync(resource_types=['projects', 'labels', 'items'])
|
||||
logging.debug('Sleeping for %d seconds', SYNC_DELAY)
|
||||
time.sleep(SYNC_DELAY)
|
||||
logging.debug('%d changes queued for sync... commiting if needed', len(api.queue))
|
||||
if len(api.queue):
|
||||
api.commit()
|
||||
|
||||
if args.onetime:
|
||||
break
|
||||
logging.debug('Sleeping for %d seconds', args.delay)
|
||||
time.sleep(args.delay)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
20
setup.py
Normal file
20
setup.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from setuptools import setup
|
||||
|
||||
setup(
|
||||
name='NextAction',
|
||||
version='0.3',
|
||||
py_modules=['nextaction'],
|
||||
url='https://github.com/nikdoof/NextAction',
|
||||
license='MIT',
|
||||
author='Andrew Williams',
|
||||
author_email='andy@tensixtyone.com',
|
||||
description='A more GTD-like workflow for Todoist. Uses the REST API to add and remove a @next_action label from tasks.',
|
||||
entry_points={
|
||||
"console_scripts": [
|
||||
"nextaction=nextaction:main",
|
||||
],
|
||||
},
|
||||
install_requires=[
|
||||
'todoist-python',
|
||||
]
|
||||
)
|
||||
Reference in New Issue
Block a user