-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.gs
116 lines (101 loc) · 3.3 KB
/
index.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const CALENDAR_NAME = "Your calendar name goes here";
const HABITICA_TOKEN = "Your habitica token goes here";
const HABITICA_ID = "Your habitica id goes here";
function syncToHabbitica() {
const habTaskURL = "https://habitica.com/api/v3/tasks/";
const today = new Date();
const agenda = CalendarApp.getCalendarsByName(CALENDAR_NAME)[0];
const events = agenda.getEventsForDay(today);
console.log(events[0]);
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++) {
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",
date: "today",
repeat: "false",
};
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 + "![image](some_valid_url)" //markdown sintax
);
} else if (title.indexOf("Another recurrent event title you want to filter in") > -1) {
return location + "![image](some_valid_url)"; //markdown sintax
} else {
return location;
}
} else {
return "";
}
}