Skip to content

Instantly share code, notes, and snippets.

@bmorrisondev
Created March 2, 2021 22:16
Show Gist options
  • Save bmorrisondev/54c9b84b787c4ead72452e618a459fbf to your computer and use it in GitHub Desktop.
Save bmorrisondev/54c9b84b787c4ead72452e618a459fbf to your computer and use it in GitHub Desktop.
Sync Todoist Tasks to a Notion Database
from notion.client import NotionClient
import requests
import schedule
import time
from datetime import datetime
def get_todoist_tasks():
token = "todoist_token"
headers = {
'Authorization': f'Bearer {token}'
}
res = requests.get(
'https://api.todoist.com/rest/v1/tasks', headers=headers)
return res.json()
def complete_todoist_task(taskId):
token = "todoist_token"
headers = {
'Authorization': f'Bearer {token}'
}
res = requests.post(
f'https://api.todoist.com/rest/v1/tasks/{taskId}/close', headers=headers)
def init_notion_collection():
token = "notion_token" # this can be viewed by inspecting cookies in chrome
tasksUrl = "notion_database_url"
client = NotionClient(token_v2=token)
return client.get_collection_view(tasksUrl)
def import_tasks_to_notion(collection):
log("Starting Notions sync")
tasks = get_todoist_tasks()
if(len(tasks) > 0):
for task in tasks:
record = collection.collection.add_row()
record.name = task["content"]
complete_todoist_task(task["id"])
log(f"{len(tasks)} tasks were synced!")
else:
log("No tasks to sync.")
def log(message):
print(f"{datetime.now().isoformat()}: {message}")
def main():
log("notion-sync starting!")
collection = init_notion_collection()
schedule.every(5).minutes.do(import_tasks_to_notion, collection=collection)
while True:
schedule.run_pending()
time.sleep(1)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment