-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
168 lines (140 loc) · 4.45 KB
/
utils.js
File metadata and controls
168 lines (140 loc) · 4.45 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
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
export function getCurrentYearMonth() {
const today = new Date();
return {
year: today.getFullYear(),
month: today.getMonth() + 1,
day: today.getDate(),
};
}
export function formatDateForICS(date) {
return date.toISOString().replace(/[-:]/g, "").split(".")[0] + "Z";
}
export function downloadICS(events) {
let icsContent = `BEGIN:VCALENDAR
VERSION:2.0
CALSCALE:GREGORIAN
PRODID:-//Moodle to Calendar//EN
`;
for (const event of events) {
const { title, description, startDate, endDate } = event;
const format = formatDateForICS;
icsContent += `BEGIN:VEVENT
SUMMARY:${title}
DESCRIPTION:${description || ""}
DTSTART:${format(startDate)}
DTEND:${format(endDate)}
END:VEVENT
`;
}
icsContent += "END:VCALENDAR";
const blob = new Blob([icsContent], { type: "text/calendar" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "moodle-homework.ics";
a.click();
URL.revokeObjectURL(url);
}
// Google Calendar API add Event
export async function addOneEventToCalendar(token, event) {
// Google Calendar API call
// Docs: https://developers.google.com/calendar/api/v3/reference/events/insert
const res = await fetch("https://www.googleapis.com/calendar/v3/calendars/primary/events", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
summary: event.title,
description: event.description,
start: {
dateTime: event.startDate.toISOString(),
timeZone: "Asia/Taipei",
},
end: {
dateTime: event.endDate.toISOString(),
timeZone: "Asia/Taipei",
},
}),
});
if (!res.ok) {
const errText = await res.text();
throw new Error(`API Error: ${res.status}, ${errText}`);
}
return res.json();
}
function normalizeBatchText(raw) {
return raw.replace(/\r?\n/g, "\r\n");
}
function generateBatchRequestBody(events, boundary = "batch_boundary") {
const CRLF = "\r\n";
let body = "";
events.forEach((event, i) => {
body += `--${boundary}${CRLF}`;
body += `Content-Type: application/http${CRLF}`;
body += `Content-ID: <item-${i + 1}>${CRLF}${CRLF}`;
body += `POST /calendar/v3/calendars/primary/events HTTP/1.1${CRLF}`;
body += `Content-Type: application/json${CRLF}${CRLF}`;
const eventPayload = {
summary: event.title,
description: event.description,
start: {
dateTime: event.startDate.toISOString(),
timeZone: "Asia/Taipei",
},
end: {
dateTime: event.endDate.toISOString(),
timeZone: "Asia/Taipei",
},
};
body += JSON.stringify(eventPayload) + CRLF + CRLF;
});
body += `--${boundary}--${CRLF}`;
return body;
}
function parseGoogleBatchResponse(responseText) {
const boundary = responseText.match(/--batch_[^\r\n-]+/)?.[0]?.slice(2);
const parts = responseText.split(`--${boundary}`);
const results = [];
for (const part of parts) {
const trimmed = part.trim();
if (!trimmed || !trimmed.includes("HTTP/1.1")) continue;
const statusMatch = trimmed.match(/HTTP\/1.1 (\d{3}) (.+)/);
const status = statusMatch ? parseInt(statusMatch[1], 10) : null;
const statusText = statusMatch ? statusMatch[2] : "";
const sections = trimmed.split(/\n{2,}|\r\n{2,}/);
const rawBody = sections[sections.length - 1];
const jsonStart = rawBody.indexOf("{");
const jsonEnd = rawBody.lastIndexOf("}");
let body = null;
if (jsonStart !== -1 && jsonEnd !== -1 && jsonEnd > jsonStart) {
const jsonStr = rawBody.slice(jsonStart, jsonEnd + 1);
try {
body = JSON.parse(jsonStr);
} catch (e) {
console.warn("JSON parse failed:", e);
}
}
results.push({ status, ok: status >= 200 && status < 300, statusText, body });
}
return results;
}
// Google Calendar API batch add Event
export async function addBatchEventsToCalendar(token, events) {
const boundary = "batch_boundary_" + Date.now();
const body = generateBatchRequestBody(events, boundary);
const res = await fetch("https://www.googleapis.com/batch/calendar/v3", {
method: "POST",
headers: {
Authorization: `Bearer ${token}`,
"Content-Type": `multipart/mixed; boundary=${boundary}`,
},
body,
});
const text = await res.text();
if (!res.ok) {
throw new Error(`Batch API Error: ${res.status}\n${text}`);
}
return parseGoogleBatchResponse(normalizeBatchText(text));
}