/
calendar.js
243 lines (228 loc) · 6.57 KB
/
calendar.js
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
const MY_NOTION_TOKEN = "YOUR_NOTION_TOKEN";
const DATABASE_ID = "TARGET_DATABASE_ID";
function main() {
logSyncedEvents("primary", false);
}
/**
* Retrieve and log events from the given calendar that have been modified
* since the last sync. If the sync token is missing or invalid, log all
* events from up to a month ago (a full sync).
*
* @param {string} calendarId The ID of the calender to retrieve events from.
* @param {boolean} fullSync If true, throw out any existing sync token and
* perform a full sync; if false, use the existing sync token if possible.
*/
function logSyncedEvents(calendarId, fullSync) {
var properties = PropertiesService.getUserProperties();
var options = {
maxResults: 100,
};
var syncToken = properties.getProperty("syncToken");
if (syncToken && !fullSync) {
options.syncToken = syncToken;
} else {
// Sync events up to thirty days in the past.
options.timeMin = getRelativeDate(-30, 0).toISOString();
}
// Retrieve events one page at a time.
var events;
var pageToken;
do {
try {
options.pageToken = pageToken;
events = Calendar.Events.list(calendarId, options);
} catch (e) {
// Check to see if the sync token was invalidated by the server;
// if so, perform a full sync instead.
if (
e.message === "Sync token is no longer valid, a full sync is required."
) {
properties.deleteProperty("syncToken");
logSyncedEvents(calendarId, true);
return;
} else {
throw new Error(e.message);
}
}
if (events.items && events.items.length > 0) {
for (var i = 0; i < events.items.length; i++) {
var event = events.items[i];
if (event.status === "cancelled") {
console.log("Event id %s was cancelled.", event.id);
} else {
var start;
var end;
if (event.start.date) {
// All-day event.
var start = new Date(event.start.date);
var end = new Date(event.end.date);
console.log("%s (%s)", event.summary, start.toLocaleDateString());
} else {
// Events that don't last all day; they have defined start times.
var start = new Date(event.start.dateTime);
var end = new Date(event.end.dateTime);
console.log("%s (%s)", event.summary, start.toLocaleString());
}
console.log(event.attendees);
if (doesEventExistInJournal(event.summary, start, end)) {
console.log("Event Already exists in database");
} else {
createNewPageInJournal(event.summary, start, end, event.attendees);
}
}
}
} else {
console.log("No events found.");
}
pageToken = events.nextPageToken;
} while (pageToken);
properties.setProperty("syncToken", events.nextSyncToken);
}
function getRelativeDate(daysOffset, hour) {
var date = new Date();
date.setDate(date.getDate() + daysOffset);
date.setHours(hour);
date.setMinutes(0);
date.setSeconds(0);
date.setMilliseconds(0);
return date;
}
function getNotionHeaders() {
return {
Authorization: `Bearer ${MY_NOTION_TOKEN}`,
Accept: "application/json",
"Notion-Version": "2022-02-22",
"Content-Type": "application/json",
};
}
function createNewPageInJournal(title, startDate, endDate, attendees) {
const create_page_url = "https://api.notion.com/v1/pages";
const data = {
parent: {
database_id: DATABASE_ID,
},
icon: {
type: "emoji",
emoji: "📆",
},
properties: {
Name: {
title: [
{
text: {
content: title,
},
},
],
},
Date: {
date: {
start: getFormattedDateString(startDate),
end: getFormattedDateString(endDate),
time_zone: "Asia/Kolkata",
},
},
},
};
if (attendees != undefined) {
const attendeeString = convertAttendeesToString(attendees);
data["children"] = [
{
object: "block",
type: "heading_2",
heading_2: {
rich_text: [
{
type: "text",
text: {
content: "Attendees",
link: null,
},
},
],
color: "default",
},
},
{
object: "block",
type: "paragraph",
paragraph: {
rich_text: [
{
type: "text",
text: {
content: attendeeString,
link: null,
},
},
],
color: "default",
},
},
{
object: "block",
type: "divider",
divider: {},
},
];
}
const response = UrlFetchApp.fetch(create_page_url, {
headers: getNotionHeaders(),
payload: JSON.stringify(data),
muteHttpExceptions: true,
});
// const responseData = JSON.parse(response.getContentText())
// console.log(responseData)
console.log(`${title} page created`);
}
function convertAttendeesToString(attendees) {
return attendees
.reduce((acc, attendee) => {
return `${attendee.email} ${
attendee.displayName ? " - " + attendee.displayName : ""
}\n${acc}`;
}, "")
.trim();
}
function getPagesFromJournal() {
const query_url = `https://api.notion.com/v1/databases/${DATABASE_ID}/query`;
const data = {
page_size: 20,
};
const response = UrlFetchApp.fetch(query_url, {
headers: getNotionHeaders(),
payload: JSON.stringify(data),
});
const responseData = JSON.parse(response.getContentText());
console.log(responseData);
for (page of responseData.results) {
console.log(page.properties.Name.title);
}
}
function doesEventExistInJournal(eventName, start, end) {
const search_url = "https://api.notion.com/v1/search";
const data = {
query: eventName,
};
const response = UrlFetchApp.fetch(search_url, {
headers: getNotionHeaders(),
payload: JSON.stringify(data),
});
const responseData = JSON.parse(response.getContentText());
// console.log(responseData);
for (page of responseData.results) {
if (
page.properties.Name.title[0].plain_text == eventName &&
getFormattedDateString(new Date(page.properties.Date.date.start)) ==
getFormattedDateString(start) &&
getFormattedDateString(new Date(page.properties.Date.date.end)) ==
getFormattedDateString(end)
) {
return true;
}
}
return false;
}
function getFormattedDateString(date) {
return Utilities.formatDate(date, "IST", "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
}