Skip to content

Instantly share code, notes, and snippets.

@a7madgamal
Last active October 6, 2023 00: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 a7madgamal/0bccd902a2f9fe6ad8513493ffb8abf7 to your computer and use it in GitHub Desktop.
Save a7madgamal/0bccd902a2f9fe6ad8513493ffb8abf7 to your computer and use it in GitHub Desktop.
Tasker Gists
const logs = [];
function logAndExit() {
setLocal("debugtext", logs.join("\n"));
exit();
}
if (typeof todoist_header !== "string") {
logs.push("missing todoist_header!");
logAndExit();
}
function setSyncToken(syncToken) {
logs.push(`setSyncToken: ${syncToken}`);
writeFile("Download/todoist_last_sync_token.txt", syncToken);
logs.push(`setSyncToken ok 👌🏻`);
}
function getSyncToken(syncToken) {
const lastToken = readFile("Download/todoist_last_sync_token.txt");
if (lastToken) {
//flash(`lastToken: ${lastToken}`);
return lastToken;
} else {
return false;
}
}
async function callTodoistApi(syncToken) {
const url = `https://api.todoist.com/sync/v9/sync?resource_types=["all"]&sync_token=${syncToken}`;
const headers = new Headers();
headers.append("Authorization", `Bearer ${todoist_header}`);
// const data = new URLSearchParams();
// data.append("sync_token", "*");
// data.append("resource_types", '["all"]');
// const data = {
// sync_token: "*",
// resource_types: '["all"]',
// };
const requestOptions = {
// method: "POST",
headers: headers,
// body: data.toString(), //`sync_token=*&resource_types=["all"]`, // ${syncToken || "*"} &resource_types='["all"]'
// redirect: "follow",
};
let httpResponse;
try {
httpResponse = await fetch(url, requestOptions);
} catch (e) {
logs.push("callTodoistApi fetch Error: " + e.message);
logAndExit();
}
if (!httpResponse.ok) {
logs.push(`Network response was not ok: ${httpResponse.statusCode}`);
log.push(JSON.stringify(httpResponse, null, 2));
logAndExit();
}
let jsonResponse;
try {
jsonResponse = await httpResponse.json();
} catch (e) {
logs.push("callTodoistApi jsonResponse Error: " + e.message);
logAndExit();
}
setSyncToken(jsonResponse.sync_token);
let formattedResponse;
try {
formattedResponse = JSON.stringify(jsonResponse, null, 2);
} catch (e) {
logs.push("callTodoistApi formattedResponse Error: " + e.message);
logAndExit();
}
logs.push("callTodoistApi ok");
logs.push(formattedResponse);
return jsonResponse;
}
const syncToken = getSyncToken();
const result = callTodoistApi(syncToken);
result
.then((r) => {
logs.push("result then");
logAndExit();
})
.catch((e) => {
logs.push("result catch");
logAndExit();
});
// exit();
// setLocal("debugtext", JSON.stringify(dataobj, null, 2));
// if (typeof http_data !== "string") {
// flash("empty data!");
// } else {
// let dataobj;
// try {
// dataobj = JSON.parse(http_data);
// } catch (e) {
// flash("json parse Error: " + e.message);
// exit();
// }
// const todos = dataobj.items;
// const todaysTodos = todos.filter((todo) => todo.due !== null);
// // checked
// // due.date
// // due.is_recurring
// // completed_at (null
// // content (title)
// //day_order (number)
// // labels (array)
// // priority
// //
// setLocal("debugtext", JSON.stringify(dataobj, null, 2));
// const keys = Object.keys(todos[0]);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment