-
Notifications
You must be signed in to change notification settings - Fork 1
/
calendar.js
214 lines (179 loc) · 5.81 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
/*----------------------------calendar.js--------------------------------------------------------------
| Author: Patrick Lin, Nicholas Jones, Sami Bajwa |
| Group Name: Some Group Name |
| Emails: pjlin@csu.fullerton.edu, nicholasj898@csu.fullerton.edu, samibajwa@csu.fullerton.edu |
| Class: CPSC 254 |
| Instructor: Professor Chen |
| Purpose: This script parses the events JSON generated by our web scraper and inserts each event |
| into our dedicated Google Calendar instance using the Google Calendar API. |
| Dependencies: Google Calendar API, Selenium API |
| Known Bugs: N/A |
| Licensing Information: You are free to use or extend this project for educational purposes |
| provided that you (1) retain this notice, and (2) provide clear attribution to the developers of |
| the project by adding a link to our Github repository: |
| https://github.com/Arbalest007/CSUF-Events-Extension |
------------------------------------------------------------------------------------------------------*/
const fetch = require("node-fetch");
const { google } = require("googleapis");
require("dotenv").config();
const fs = require("fs");
const CREDENTIALS = JSON.parse(process.env.CREDENTIALS);
const SCOPES = "https://www.googleapis.com/auth/calendar";
const TIMEOFFSET = "-08:00";
const calendarId = process.env.calendarId;
const calendar = google.calendar({
version: "v3",
});
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
const auth = new google.auth.JWT(
CREDENTIALS.client_email,
null,
CREDENTIALS.private_key,
SCOPES
);
const convertNoon = (input) => {
inputArray = input.split(" ");
for (var i = 0; i < inputArray.length; i++) {
if (inputArray[i] == "Noon") {
inputArray[i] = "12";
inputArray.splice(i + 1, 0, "pm");
}
}
return inputArray;
};
const convertTime12to24 = (time12h) => {
const [time, modifier] = time12h.split(" ");
let [hours, minutes] = time.split(":");
if (hours === "12") {
hours = "00";
}
if (minutes == null) {
minutes = "00";
}
if (modifier === "pm") {
hours = parseInt(hours, 10) + 12;
}
return `${hours}:${minutes}`;
};
const convertFullString = (s) => {
let time_array = [];
let l = 0;
for (let r = 1; r < s.length; r++) {
if ((s[r] == "m" && s[r - 1] == "p") || (s[r] == "m" && s[r - 1] == "a")) {
let time_string = s.slice(l, r + 1);
time_array.push(time_string);
l = r;
}
while (isNaN(parseInt(s[l])) && l < s.length) {
l += 1;
}
}
let time1 = convertTime12to24(time_array[0]);
let time2 = convertTime12to24(time_array[1]);
return `${time1} to ${time2}`;
};
const parseTime = (s) => {
inputArray = convertNoon(s);
inputArray = convertFullString(
inputArray.toString().replaceAll(",", " ")
).split(" ");
return inputArray;
};
const insertEvent = async (event) => {
try {
const response = await calendar.events.insert({
auth: auth,
calendarId: calendarId,
resource: event,
});
if (response["status"] == 200 && response["statusText"] === "OK") {
return "Event inserted successfully";
} else {
return "Failed to insert event";
}
} catch (error) {
console.log(`Error at insertEvent --> ${error}`);
return 0;
}
};
const insertNewEvent = async (event) => {
try {
const res = await insertEvent(event);
console.log(res);
} catch (error) {
console.log(error);
}
};
async function getEvents() {
const eventList = await fetch(
"https://raw.githubusercontent.com/Arbalest007/CSUF-Events-Extension/main/event.json"
)
.then((eventList) => eventList.json())
.then((data) => initializeCalendar(data.events));
}
//Only call if event.json has not been initialized yet!
function removeLastCharacter(filename) {
const stat = fs.statSync(filename);
const fileSize = stat.size;
fs.truncate(filename, fileSize - 2, function () {
console.log("Last 2 Deleted!");
});
fs.appendFile("event.json", "\n]}", (err) => {
if (err) {
console.error(err);
return;
}
});
}
async function initializeCalendar(data) {
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
let event = {
summary: `This is the summary`,
description: `This is the description.`,
start: {
dateTime: "Empty",
timeZone: "America/Los_Angeles",
},
end: {
dateTime: "Empty",
timeZone: "America/Los_Angeles",
},
};
for (i = 0; i < data.length; i++) {
event["summary"] = data[i].event_title;
event["description"] = data[i].link.replaceAll("Share URL:\n", "");
timeString = data[i].event_time;
cleanString = convertNoon(timeString);
finalTime = parseTime(timeString);
month = 1 + months.indexOf(data[i].event_month);
year = 0;
if (month < 11) {
year = 2023;
} else {
year = 2022;
}
day = data[i].event_day;
startTime = finalTime[0];
endTime = finalTime[2];
startTimeFinal = `${year}-${month}-${day}T${startTime}:00.000${TIMEOFFSET}`;
endTimeFinal = `${year}-${month}-${day}T${endTime}:00.000${TIMEOFFSET}`;
event["start"].dateTime = startTimeFinal;
event["end"].dateTime = endTimeFinal;
insertNewEvent(event);
await timer(3000);
}
}
getEvents();