Skip to content

Instantly share code, notes, and snippets.

@error9900
Created November 10, 2023 06:42
Show Gist options
  • Save error9900/5c15002c0052a0e3c998499bfd2dfd5c to your computer and use it in GitHub Desktop.
Save error9900/5c15002c0052a0e3c998499bfd2dfd5c to your computer and use it in GitHub Desktop.
Get completed items from Todoist
"""
Put your token in data/token.txt.
Completed items are written to `output/completed_items.py` as a Python dictionary.
Reference: https://developer.todoist.com/sync/v9/#get-all-completed-items
"""
import requests
def load_token():
with open("data/token.txt", "r", encoding="utf-8") as f:
line = f.readline()
token = line.strip()
return token
def get_completed_items():
parameters = {"limit": 200}
response = requests.post(
"https://api.todoist.com/sync/v9/completed/get_all",
headers={"Authorization": f"Bearer {load_token()}"},
json=parameters,
timeout=60,
)
response.raise_for_status()
return response.json()
def write_completed_items_to_file(completed_items):
file_path = "output/completed_items.py"
with open(file_path, "w", encoding="utf-8") as f:
f.write(f"completed_items = {str(completed_items)}")
def main():
write_completed_items_to_file(get_completed_items())
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment