Skip to content

Instantly share code, notes, and snippets.

@dfalbel
Created April 12, 2020 03:44
Show Gist options
  • Save dfalbel/a12b8f37a00bc38f833a92f4b3156e51 to your computer and use it in GitHub Desktop.
Save dfalbel/a12b8f37a00bc38f833a92f4b3156e51 to your computer and use it in GitHub Desktop.
todoist_token <- config::get("TODOIST_TOKEN")
get_tasks_week <- function(week = 0, offset = 0) {
res <- httr::POST(
url = "https://api.todoist.com/sync/v8/activity/get",
body = list(
token = todoist_token,
limit = 100,
page = week,
offset = offset
)
)
res <- httr::content(res, "parsed")
tasks <- res$events
while (length(tasks) < res$count) {
res <- httr::POST(
url = "https://api.todoist.com/sync/v8/activity/get",
body = list(
token = todoist_token,
limit = 100,
page = week,
offset = length(tasks)
)
)
res <- httr::content(res, "parsed")
tasks <- append(tasks, res$events)
}
tasks
}
get_projects <- function() {
res <- httr::POST(
url = "https://api.todoist.com/sync/v8/sync",
body = list(
token = todoist_token,
sync_token = "*",
resource_types = '["projects"]'
)
)
httr::content(res)
}
get_colors <- function() {
col <- tibble::tribble(
~id, ~hexadecimal,
30L, "b8256f",
31L, "db4035",
32L, "ff9933",
33L, "fad000",
34L, "afb83b",
35L, "7ecc49",
36L, "299438",
37L, "6accbc",
38L, "158fad",
39L, "14aaf5",
40L, "96c3eb",
41L, "4073ff",
42L, "884dff",
43L, "af38eb",
44L, "eb96eb",
45L, "e05194",
46L, "ff8d85",
47L, "808080",
48L, "b8b8b8",
49L, "ccac93"
)
col <- dplyr::mutate(col, hexadecimal = paste0("#", hexadecimal))
col
}
make_data <- function() {
tasks <- purrr::map(0:8, get_tasks_week)
tasks <- purrr::flatten(tasks)
tasks <- purrr::discard(tasks, ~is.null(.x$parent_project_id))
tasks <- purrr::keep(tasks, ~.x$event_type == "completed")
tasks <- purrr::map_dfr(tasks, ~tibble::tibble(
id = .x$id,
event_date = .x$event_date,
parent_project_id = .x$parent_project_id,
content = .x$extra_data$content
))
projects <- get_projects()$projects
projects <- purrr::map_dfr(projects, ~tibble::tibble(
id = .x$id,
name = .x$name,
color = .x$color
))
projects <- dplyr::left_join(projects, get_colors(), by = c("color" = "id"))
tasks <- dplyr::left_join(
tasks,
projects,
by = c("parent_project_id" = "id")
)
tasks <- dplyr::mutate(tasks, event_date = lubridate::ymd_hms(event_date))
list(tasks, projects)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment