-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
56 lines (49 loc) · 2.28 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Holidays API</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this API.</noscript>
<script>
(function () {
const now = new Date();
const year = now.getFullYear();
const params = new URLSearchParams(window.location.search);
const from = params.get("from") || `${year - 1}-01-01T00:00:00Z`;
const to = params.get("to") || `${year}-01-01T00:00:00Z`;
const country = params.get("country") || "indian";
const format = params.get("format") || "csv";
window.onload = function () {
fetch(`https://www.googleapis.com/calendar/v3/calendars/en.${country}%23holiday%40group.v.calendar.google.com/events?key=AIzaSyAEwhpXgJGK7A2SCHtIhzje7-z7KNqODfU&timeMin=${from}&timeMax=${to}`).then(res => res.json()).then(res => {
const arrData = res.items.map(el => [el.start.date, el.summary]);
let content;
if (format === "json") {
const jsonStr = JSON.stringify(arrData.map(el => ({
"date": el[0],
"event": el[1]
})));
content = "data:text/json;charset=utf-8," + jsonStr;
} else {
content = "data:text/csv;charset=utf-8,"
+ "date,event\n"
+ arrData.map(e => e.join(",")).join("\n");
}
const link = document.createElement("a");
link.setAttribute("href", encodeURI(content));
link.setAttribute("download", `holidays.${format}`);
document.body.appendChild(link); // Required for FF
link.click();
window.close();
}).catch(error => {
console.log(error);
document.body.innerHTML += "<h1>somthing went wrong. Please try again later!</h1>"
})
}
})();
</script>
</body>
</html>