forked from inevitable-team/compiled-mcr-events
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogleCalendar.js
More file actions
70 lines (63 loc) · 2.25 KB
/
googleCalendar.js
File metadata and controls
70 lines (63 loc) · 2.25 KB
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
const fetch = require("node-fetch"),
group = require("./templates/group"),
event = require("./templates/event");
class googleCalendar {
constructor(calendarId, urlLink, name, calendarKey) {
this.url = `https://www.googleapis.com/calendar/v3/calendars/${ calendarId }/events?key=${ calendarKey }&maxResults=9999&singleEvents=true&orderBy=starttime&timeMin=${ this.ISODateString(new Date()) }&timeMax=${ this.ISODateString(new Date((new Date().valueOf()) + 31540000000)) }`;
this.link = urlLink || "";
this.name = name || "N/A";
// Converters
this.eventClass = event;
this.event = (event) => new this.eventClass(
event.summary,
event.htmlLink,
event.location,
this.removeHTML(event.description || ""),
event.start.dateTime,
event.end.dateTime || event.start.dateTime,
null,
null,
null,
null,
this.name,
this.link,
this.name,
false,
this.chkOnline(event.location),
! this.chkOnline(event.location),
[],
[]
);
}
chkOnline(location) {
if(location == undefined) {
location = "";
}
return !!(location.includes("Online") || location.includes("online"));
}
async getData() {
return new Promise(resolve => {
this.getEvents().then(events => {
let filteredEvents = events.filter(e => e.hasOwnProperty("summary")).map(this.event);
resolve([[], filteredEvents]);
});
});
}
async getEvents() {
return new Promise(resolve => {
fetch(this.url).then((res) => {
return res.json()
}).then((jsonData) => {
resolve(jsonData.items)
})
});
}
ISODateString(d) {
let pad = (n) => n < 10 ? '0' + n : n;
return d.getUTCFullYear() + '-' + pad(d.getUTCMonth() + 1) + '-' + pad(d.getUTCDate()) + 'T' + pad(d.getUTCHours()) + ':' + pad(d.getUTCMinutes()) + ':' + pad(d.getUTCSeconds()) + 'Z';
}
removeHTML(html) {
return html.replace(/(<([^>]+)>)/ig, "");
}
}
module.exports = googleCalendar;