Initial changes made to REST API. Working on a Sync API workaround to find completed tasks now.

pull/30/head
Hoffelhas 2022-12-17 19:31:19 +01:00
parent 3a25e05aa3
commit 95a71524cd
1 changed files with 49 additions and 30 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/python3 #!/usr/bin/python3
from todoist.api import TodoistAPI from todoist_api_python.api import TodoistAPI
import sys import sys
import time import time
import requests import requests
@ -73,15 +73,15 @@ def query_yes_no(question, default="yes"):
# Check if label exists, if not, create it # Check if label exists, if not, create it
def verify_label_existance(api, label_name, prompt_mode):
def verify_label_existance(args, api, label_name, prompt_mode):
# Check the regeneration label exists # Check the regeneration label exists
label = api.labels.all(lambda x: x['name'] == label_name) labels = api.get_labels()
label = [x for x in labels if x.name == label_name]
if len(label) > 0: if len(label) > 0:
label_id = label[0]['id'] label_id = label[0].id
logging.debug('Label \'%s\' found as label id %d', logging.debug('Label \'%s\' found as label id %d',
args.label, label_id) label_name, label_id)
else: else:
# Create a new label in Todoist # Create a new label in Todoist
logging.info( logging.info(
@ -94,11 +94,15 @@ def verify_label_existance(args, api, label_name, prompt_mode):
response = True response = True
if response: if response:
api.labels.add(label_name) try:
api.commit() api.add_label(name=label_name)
api.sync() except Exception as error:
label = api.labels.all(lambda x: x['name'] == label_name) print(error)
label_id = label[0]['id']
labels = api.get_labels()
label = [x for x in labels if x.name == label_name]
label_id = label[0].id
logging.info("Label '{}' has been created!".format(label_name)) logging.info("Label '{}' has been created!".format(label_name))
else: else:
logging.info('Exiting Autodoist.') logging.info('Exiting Autodoist.')
@ -152,31 +156,33 @@ def initialise(args):
logging.debug('Connecting to the Todoist API') logging.debug('Connecting to the Todoist API')
api_arguments = {'token': args.api_key} api_arguments = {'token': args.api_key}
if args.nocache: if args.nocache:
logging.debug('Disabling local caching') logging.debug('Disabling local caching')
api_arguments['cache'] = None api_arguments['cache'] = None
api = TodoistAPI(**api_arguments) api = TodoistAPI(**api_arguments)
sync(api)
logging.info("Autodoist has connected and is running fine!\n")
# Check if labels exist
# If labeling argument is used # If labeling argument is used
if args.label is not None: if args.label is not None:
# Verify that the next action label exists; ask user if it needs to be created # Verify that the next action label exists; ask user if it needs to be created
label_id = verify_label_existance(args, api, args.label, 1) label_id = verify_label_existance(api, args.label, 1)
else: else:
# Label functionality not needed # Label functionality not needed
label_id = None label_id = None
logging.info("Autodoist has connected and is running fine!\n")
# If regeneration mode is used, verify labels # If regeneration mode is used, verify labels
if args.regeneration is not None: if args.regeneration is not None:
# Verify the existance of the regeneraton labels; force creation of label # Verify the existance of the regeneraton labels; force creation of label
regen_labels_id = [verify_label_existance( regen_labels_id = [verify_label_existance(
args, api, regen_label, 2) for regen_label in args.regen_label_names] api, regen_label, 2) for regen_label in args.regen_label_names]
else: else:
# Label functionality not needed # Label functionality not needed
@ -567,7 +573,12 @@ def autodoist_magic(args, api, label_id, regen_labels_id):
overview_item_ids = {} overview_item_ids = {}
overview_item_labels = {} overview_item_labels = {}
for project in api.projects.all(): try:
projects = api.get_projects()
except Exception as error:
print(error)
for project in projects:
# To determine if a sequential task was found # To determine if a sequential task was found
first_found_project = False first_found_project = False
@ -584,16 +595,24 @@ def autodoist_magic(args, api, label_id, regen_labels_id):
project['name'], project_type) project['name'], project_type)
# Get all items for the project # Get all items for the project
project_items = api.items.all( try:
lambda x: x['project_id'] == project['id']) project_tasks = api.get_tasks(project_id = project.id)
except Exception as error:
print(error)
# Run for both none-sectioned and sectioned items # Run for both none-sectioned and sectioned items
for s in [0, 1]:
# Get completed tasks: get(api._session, endpoint, api._token, '0')['items']
# for s in [0, 1]: # TEMP SECTION ONLY
for s in [1]:
if s == 0: if s == 0:
sections = [create_none_section()] sections = [create_none_section()] # TODO: Rewrite
elif s == 1: elif s == 1:
sections = api.sections.all( try:
lambda x: x['project_id'] == project['id']) sections = api.get_sections(project_id = project.id)
except Exception as error:
print(error)
for section in sections: for section in sections:
@ -611,13 +630,13 @@ def autodoist_magic(args, api, label_id, regen_labels_id):
section['name'], section_type) section['name'], section_type)
# Get all items for the section # Get all items for the section
items = [x for x in project_items if x['section_id'] tasks = [x for x in project_tasks if x.section_id
== section['id']] == section.id]
# Change top parents_id in order to sort later on # Change top parents_id in order to numerically sort later on
for item in items: for task in tasks:
if not item['parent_id']: if not task.parent_id:
item['parent_id'] = 0 task.parent_id = 0
# Sort by parent_id and filter for completable items # Sort by parent_id and filter for completable items
items = sorted(items, key=lambda x: ( items = sorted(items, key=lambda x: (
@ -963,7 +982,7 @@ def main():
# Start main loop # Start main loop
while True: while True:
start_time = time.time() start_time = time.time()
sync(api) # sync(api)
# Evaluate projects, sections, and items # Evaluate projects, sections, and items
overview_item_ids, overview_item_labels = autodoist_magic( overview_item_ids, overview_item_labels = autodoist_magic(