From d2b094f816bddf04d4df741991d019e6aaa2a7ae Mon Sep 17 00:00:00 2001 From: moorsey Date: Sat, 6 Jan 2024 11:14:19 +0000 Subject: [PATCH] Added chunking code for API limits --- autodoist.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/autodoist.py b/autodoist.py index 957fa9d..da7ada9 100644 --- a/autodoist.py +++ b/autodoist.py @@ -19,6 +19,18 @@ import os import re import json + +#chunking + +def sync_in_chunks(api): + chunk_size = 99 # Maximum commands per request + while api.queue: + chunk = api.queue[:chunk_size] # Get the next chunk + api.queue = api.queue[chunk_size:] # Remove the processed chunk from the queue + sync(api, chunk) # Sync the current chunk + + logging.info('Synced chunk of %d commands.', chunk_size) + # Connect to SQLite database @@ -491,8 +503,7 @@ def commit_labels_update(api, overview_task_ids, overview_task_labels): # Update tasks in batch with Todoist Sync API - -def sync(api): +def sync(api, chunk): # # This approach does not seem to work correctly. # BASE_URL = "https://api.todoist.com" # SYNC_VERSION = "v9" @@ -510,7 +521,7 @@ def sync(api): } data = 'sync_token=' + api.sync_token + \ - '&commands=' + json.dumps(api.queue) + '&commands=' + json.dumps(chunk) response = requests.post( 'https://api.todoist.com/sync/v9/sync', headers=headers, data=data) @@ -1520,8 +1531,10 @@ def main(): overview_task_labels) # Sync all queued up changes +# if api.queue: +# sync(api) if api.queue: - sync(api) + sync_in_chunks(api) num_changes = len(api.queue)+len(api.overview_updated_ids)