Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunking code added to meet API limits #44

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,13 @@ To build the docker container, check out the repository and run:
To run autodoist inside the docker container:

docker run -it autodoist:latest

Docker-compose example:

version: "3.7"
services:
autodoist:
image: ghcr.io/Hoffelhas/autodoist:laster
container_name: autodoist
command: -l=next -hf=2 -a=**apikey**
restart: unless-stopped
19 changes: 15 additions & 4 deletions autodoist.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,18 @@
import re
import json


#chunking code to meet API limits of 100 calls per request

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


Expand Down Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -1521,7 +1532,7 @@ def main():

# Sync all queued up changes
if api.queue:
sync(api)
sync_in_chunks(api)

num_changes = len(api.queue)+len(api.overview_updated_ids)

Expand Down