Skip to content

Instantly share code, notes, and snippets.

@m-roberts
Created February 9, 2020 14:52
Show Gist options
  • Save m-roberts/56448509f9a15b8208fee87859a8f6fb to your computer and use it in GitHub Desktop.
Save m-roberts/56448509f9a15b8208fee87859a8f6fb to your computer and use it in GitHub Desktop.
Apply label and due string to all tasks in a Todoist given project
#!/usr/local/bin/python3
import uuid, requests, json
ACCESS_TOKEN="<access_token>"
PROJECT_TO_MODIFY="<project_to_modify>"
LABEL_TO_APPLY="<label_to_apply>"
DUE_STRING_TO_APPLY="<due_string_to_apply>"
api_url="https://api.todoist.com/rest/v1/"
auth_header={"Authorization": "Bearer %s" % ACCESS_TOKEN}
def get_dict_of(type):
items = requests.get(api_url + type, headers=auth_header).json()
items_dict = {}
for item in items:
items_dict.update({item["name"]: item["id"]})
return items_dict
def get_tasks_in_project(project_id):
return requests.get(
api_url + "tasks",
params={"project_id": project_id},
headers=auth_header
).json()
tasks = get_tasks_in_project(
get_dict_of("projects")[PROJECT_TO_MODIFY]
)
labels = get_dict_of("labels")
for task in tasks:
print("Updating task: %s" % task["content"])
label_ids = task["label_ids"]
if labels[LABEL_TO_APPLY] not in label_ids:
label_ids.append(labels[LABEL_TO_APPLY])
headers_obj = {
"Content-Type": "application/json",
"X-Request-Id": str(uuid.uuid4()),
}
headers_obj.update(auth_header)
requests.post(
api_url + "tasks/%s" % task["id"],
data=json.dumps({
"label_ids": label_ids,
"due_string": DUE_STRING_TO_APPLY
}),
headers=headers_obj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment