This repository was archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
85 lines (75 loc) · 2.72 KB
/
Copy pathindex.ts
File metadata and controls
85 lines (75 loc) · 2.72 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
function doGet(e) {
if (e.pathInfo === "disable") {
const properties = PropertiesService.getUserProperties();
const trigger = ScriptApp.getProjectTriggers().find(
(t) => t.getUniqueId() === properties.getProperty("TRIGGER_ID")
);
if (trigger) ScriptApp.deleteTrigger(trigger);
CalendarApp.getCalendarById(
properties.getProperty("CALENDAR_ID")
).deleteCalendar();
properties.deleteAllProperties();
return HtmlService.createHtmlOutput(
"Расписание отключено, спасибо за использование"
);
}
return HtmlService.createTemplateFromFile("web/app").evaluate();
}
function include(filename: string) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
function init(login: string, password: string, educationSpace = "4") {
const properties = PropertiesService.getUserProperties();
const trigger = ScriptApp.getProjectTriggers().find(
(t) => t.getUniqueId() === properties.getProperty("TRIGGER_ID")
);
if (trigger) ScriptApp.deleteTrigger(trigger);
if (login && password) {
properties.setProperty("USERNAME", login);
properties.setProperty("PASSWORD", password);
properties.setProperty("EDUCATION_SPACE", educationSpace);
} else if (!properties.getProperty("AUTH_TOKEN")) {
throw new Error("Не указаны логин и пароль");
}
const edu = new Wrapper();
if (!edu.logIn()) throw new Error("Неверный логин или пароль");
const triggerId = ScriptApp.newTrigger("triggerManager")
.timeBased()
.everyMinutes(5)
.create()
.getUniqueId();
properties.setProperty("TRIGGER_ID", triggerId);
}
function needAuth() {
return !PropertiesService.getUserProperties().getProperty("AUTH_TOKEN");
}
function triggerManager() {
const now = new Date();
const properties = PropertiesService.getUserProperties();
let lastUpdate = new Date(properties.getProperty("LAST_UPDATE"));
if (!lastUpdate.getTime()) lastUpdate = new Date(0);
if (
(now.getHours() >= 6 && now.getHours() < 18) ||
now.getTime() - lastUpdate.getTime() >= 1.8e6
) {
parseSchedule();
properties.setProperty("LAST_UPDATE", now.toJSON());
}
}
function parseSchedule() {
const edu = new Wrapper();
const calendar = new VCalendar();
const schedule = edu
.schedule()
.filter(
({ start }) =>
new Date(start).getTime() >= VCalendar.range().start.getTime()
)
.map(VCalendar.format);
const allEvents = calendar.all();
const allIds = schedule.map((data) => VCalendar.getHash(data).id.toString());
allEvents.forEach((item) => {
if (!item.id || !allIds.includes(item.id)) item.event.deleteEvent();
});
schedule.forEach((data) => calendar.write(data));
}