Skip to content

Instantly share code, notes, and snippets.

@theptrk
Last active January 4, 2023 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theptrk/8faa263c1e8c092fad80ae92f2d49c47 to your computer and use it in GitHub Desktop.
Save theptrk/8faa263c1e8c092fad80ae92f2d49c47 to your computer and use it in GitHub Desktop.
# https://developer.todoist.com/sync/v9#summary-of-contents
def request_todoist_activity(api_key):
url = "https://api.todoist.com/sync/v9/activity/get"
headers = {
"Authorization": f"Bearer { api_key }",
}
limit: int = 100
# page represents week BUT its limited by the max limit of 100
# 1 - 1 WEEK
# 13 - 3 MONTHS
# 52 - 1 YEAR
page: int = 0
offset: int = 0
result: list = []
# default is two weeks
# so if user is on free plan, every week after 2nd week will return empty arrays
# https://developer.todoist.com/sync/v9#pagination-details
# - https://developer.todoist.com/sync/v9#user-plan-limits
while page <= 3:
events_fetched_for_this_week = []
# resource_type = ["activity"]
data = {"page": page, "limit": limit, "offset": offset}
response = requests.post(url, headers=headers, data=data)
response_data = response.json()
events = response_data["events"]
events_fetched_for_this_week += events
# the endpoint is maxed at 100, so you need to query with offsets to get all
target_events_for_week = response_data["count"]
while target_events_for_week > len(events_fetched_for_this_week):
offset = len(events_fetched_for_this_week)
data = {"page": page, "limit": limit, "offset": offset}
response = requests.post(url, headers=headers, data=data)
response_data = response.json()
events_fetched_for_this_week += response_data["events"]
result += events_fetched_for_this_week
page += 1
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment