mirror of
https://github.com/nikdoof/NextAction.git
synced 2025-12-26 16:59:23 +00:00
Compare commits
5 Commits
todoist-py
...
0.1
| Author | SHA1 | Date | |
|---|---|---|---|
| 5554c636d9 | |||
| 54f770f5f7 | |||
| 52aff18d90 | |||
| 360be44dd7 | |||
| a62ae17866 |
10
README.md
10
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,7 +19,7 @@ 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 list ends with `-`, the top level of 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
|
||||
@@ -47,4 +47,8 @@ Heroku Support
|
||||
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)
|
||||
* ```TODOIST_NEXT_ACTION_LABEL``` - The label to use in Todoist for next actions (defaults to next_action)
|
||||
* ```TODOIST_SYNC_DELAY``` - The number of seconds to wait between syncs. (defaults to 5)
|
||||
* ```TODOIST_INBOX_HANDLING``` - What method to use for the Inbox, sequence or parallel (defaults to parallel)
|
||||
* ```TODODIST_PARALLEL_SUFFIX``` - What sequence of characters to use to identify parallel processed projects (defaults to =)
|
||||
* ```TODODIST_SERIAL_SUFFIX``` - What sequence of characters to use to identify serial processed projects (defaults to -)
|
||||
110
nextaction.py
110
nextaction.py
@@ -4,29 +4,12 @@ import time
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
import argparse
|
||||
from datetime import datetime
|
||||
|
||||
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'
|
||||
|
||||
|
||||
def get_subitems(items, parent_item=None):
|
||||
"""Search a flat item list for child items"""
|
||||
result_items = []
|
||||
@@ -49,28 +32,62 @@ def get_subitems(items, parent_item=None):
|
||||
|
||||
|
||||
def main():
|
||||
if os.environ.get('TODOIST_DEBUG', None):
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument('-a', '--api_key', help='Todoist API Key',
|
||||
default=os.environ.get('TODOIST_API_KEY', None))
|
||||
parser.add_argument('-l', '--label', help='The next action label to use',
|
||||
default=os.environ.get('TODOIST_NEXT_ACTION_LABEL', 'next_action'))
|
||||
parser.add_argument('-d', '--delay', help='Specify the delay in seconds between syncs',
|
||||
default=int(os.environ.get('TODOIST_SYNC_DELAY', '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=os.environ.get('TODOIST_INBOX_HANDLING', 'parallel'),
|
||||
choices=['parallel', 'serial'])
|
||||
parser.add_argument('--parallel_suffix', default=os.environ.get('TODOIST_PARALLEL_SUFFIX', '='))
|
||||
parser.add_argument('--serial_suffix', default=os.environ.get('TODOIST_SERIAL_SUFFIX', '-'))
|
||||
parser.add_argument('--hide_future', help='Hide future dated next actions until the specified number of days',
|
||||
default=int(os.environ.get('TODOIST_HIDE_FUTURE', '7')), type=int)
|
||||
args = parser.parse_args()
|
||||
|
||||
# Set debug
|
||||
if args.debug or os.environ.get('TODOIST_DEBUG', None):
|
||||
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'
|
||||
|
||||
# Main loop
|
||||
while True:
|
||||
api.sync(resource_types=['projects', 'labels', 'items'])
|
||||
for project in api.projects.all():
|
||||
@@ -78,23 +95,25 @@ def main():
|
||||
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)
|
||||
items = sorted(api.items.all(lambda x: x['project_id'] == project['id']), key=lambda x: x['item_order'])
|
||||
|
||||
# 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']
|
||||
for item in items:
|
||||
labels = item['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):
|
||||
if label_id in labels:
|
||||
labels.remove(label_id)
|
||||
logging.debug('Updating %s without label as its too far in the future', item['content'])
|
||||
item.update(labels=labels)
|
||||
continue
|
||||
|
||||
# Process item
|
||||
if project_type == 'serial':
|
||||
if item['item_order'] == 1:
|
||||
|
||||
if label_id not in labels:
|
||||
labels.append(label_id)
|
||||
logging.debug('Updating %s with label', item['content'])
|
||||
@@ -104,10 +123,15 @@ def main():
|
||||
labels.remove(label_id)
|
||||
logging.debug('Updating %s without label', item['content'])
|
||||
item.update(labels=labels)
|
||||
elif project_type == 'parallel':
|
||||
if label_id not in labels:
|
||||
logging.debug('Updating %s with label', item['content'])
|
||||
labels.append(label_id)
|
||||
item.update(labels=labels)
|
||||
|
||||
api.sync(resource_types=['projects', 'labels', 'items'])
|
||||
logging.debug('Sleeping for %d seconds', SYNC_DELAY)
|
||||
time.sleep(SYNC_DELAY)
|
||||
api.commit()
|
||||
logging.debug('Sleeping for %d seconds', args.delay)
|
||||
time.sleep(args.delay)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
17
setup.py
Normal file
17
setup.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from distutils.core import setup
|
||||
|
||||
setup(
|
||||
name='NextAction',
|
||||
version='0.1',
|
||||
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={
|
||||
"distutils.commands": [
|
||||
"nextaction = nextaction:main",
|
||||
],
|
||||
}
|
||||
)
|
||||
Reference in New Issue
Block a user