Created
July 23, 2024 15:28
-
-
Save zYeoman/f87836ade61a69af76c7800d5dce9916 to your computer and use it in GitHub Desktop.
google app sync habitica
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const HABITICA_TOKEN = "token"; | |
const HABITICA_ID = "id"; | |
function syncToHabbitica() { | |
const habTaskURL = "https://habitica.com/api/v3/tasks/"; | |
const today = new Date(); | |
const agenda = CalendarApp.getDefaultCalendar() | |
// const agenda = CalendarApp.getCalendarsByName(CALENDAR_NAME)[0]; | |
// const events = agenda.getEventsForDay(today); | |
var events = [] | |
// 获取所有日历 | |
var calendars = CalendarApp.getAllCalendars(); | |
// 遍历每个日历并获取今天的事件 | |
for (var i = 0; i < calendars.length; i++) { | |
var calendar = calendars[i]; | |
var es = calendar.getEventsForDay(today); | |
// 遍历并输出事件标题 | |
for (var j = 0; j < es.length; j++) { | |
events.push(es[j]); | |
event = es[j]; | |
Logger.log('Calendar: ' + calendar.getName() + ', Event Title: ' + event.getTitle()); | |
} | |
} | |
const templateParams = { | |
_post: { | |
method: "post", | |
headers: { "x-api-user": HABITICA_ID, "x-api-key": HABITICA_TOKEN }, | |
}, | |
_get: { | |
contentType: "application/json", | |
method: "get", | |
headers: { "x-api-user": HABITICA_ID, "x-api-key": HABITICA_TOKEN }, | |
}, | |
_delete: { | |
method: "delete", | |
headers: { "x-api-user": HABITICA_ID, "x-api-key": HABITICA_TOKEN }, | |
}, | |
}; | |
const newTasks = []; | |
const existingTasks = fetchExistingTasks(habTaskURL, templateParams); | |
const completedTasksContent = fetchTodayCompletedTasks( | |
habTaskURL, | |
templateParams, | |
today | |
); | |
deleteCalendarTasks(habTaskURL, existingTasks, templateParams); | |
for (i = 0; i < events.length; i++) { | |
console.log(events[i].getTitle()) | |
if (newTasks.indexOf(events[i].getTitle()) === -1) { | |
newTasks.push(events[i].getTitle()); | |
const params = templateParams._post; | |
params["payload"] = { | |
text: ":calendar: " + events[i].getTitle(), | |
notes: createTaskNote(events[i].getTitle(), events[i].getLocation()), | |
type: "daily", | |
priority: "2", | |
repeat: "false", | |
frequency: "daily", | |
}; | |
const paramsText = completedTasksContent.indexOf(params.payload.text); | |
if (completedTasksContent.indexOf(params.payload.text) === -1) { | |
UrlFetchApp.fetch(habTaskURL + "user", params); | |
} | |
} | |
} | |
} | |
function fetchExistingTasks(habTaskURL, templateParams) { | |
const response = UrlFetchApp.fetch( | |
habTaskURL + "user?type=dailys", | |
templateParams._get | |
); | |
return JSON.parse(response.getContentText()); | |
} | |
function deleteCalendarTasks(habTaskURL, habTasks, templateParams) { | |
for (j = 0; j < habTasks.data.length; j++) { | |
if (habTasks.data[j].text.indexOf(":calendar: ") > -1) { | |
UrlFetchApp.fetch( | |
habTaskURL + habTasks.data[j].id, | |
templateParams._delete | |
); | |
} | |
} | |
} | |
function fetchTodayCompletedTasks(habTaskURL, templateParams, today) { | |
const tasksContent = []; | |
const response = UrlFetchApp.fetch( | |
habTaskURL + "user?type=dailys", | |
templateParams._get | |
); | |
const tasks = JSON.parse(response.getContentText()); | |
for (i = 0; i < tasks.data.length; i++) { | |
if (tasks.data[i].text.indexOf(":calendar: ") > -1) { | |
const taskDate = new Date(tasks.data[i].createdAt).getDate(); | |
if (taskDate + 12 !== today.getDate()) { | |
tasksContent.push(tasks.data[i].text); | |
} | |
} | |
} | |
return tasksContent; | |
} | |
//optional call, remove if you dont't need it | |
function createTaskNote(title, location) { | |
if (location) { | |
if (location.indexOf("Some recurrent event title you want to filter in") > -1) { | |
return ( | |
location + "" //markdown sintax | |
); | |
} else if (title.indexOf("Another recurrent event title you want to filter in") > -1) { | |
return location + ""; //markdown sintax | |
} else { | |
return location; | |
} | |
} else { | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment