-
Notifications
You must be signed in to change notification settings - Fork 8
/
gwcc_export.html
96 lines (95 loc) · 3.28 KB
/
gwcc_export.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
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
---
permalink: /gwcc_export
layout: null
---
<html>
<head>
</head>
<body>
<a id="download">export it</a>
</body>
<script>
const srcfile = "https://sessionize.com/api/v2/g415clv7/view/GridSmart";
const csvData = [
["Event","Start","End","Room"]
];
fetch(srcfile,{
method: "GET",
mode: "cors",
cache: "no-cache",
credentials: "omit",
redirect: "follow",
referrerPolicy: "origin-when-cross-origin"})
.then(
resp => {
//console.log(['resp', resp])
return resp.text()
}
)
.then(
data => {
//console.log(["data text", data]);
return data? JSON.parse(data) : []
}
)
.then(
process
).catch(
error => console.log(["fail", error])
)
function process( days ) {
if (days.length == 0) {
throw new Error("empty event data");
}
//console.log(["days", days])
days.map( d => {
//console.log(["rooms", d["rooms"]])
return d["rooms"]
})
.map(rooms => {
console.log(["r", rooms])
return rooms.flatMap(r => r["sessions"]);
})
.forEach(e => {
console.log(["sessions.mapped", e])
e.forEach( s => {
const row = [];
const room = formatRoom(s["room"])
if(!room) return;
row.push('"' + s["title"] + '"')
row.push('"' + formatDate(s["startsAt"]) + '"')
row.push('"' + formatDate(s["endsAt"]) + '"')
row.push('"' + room + '"')
//console.log([row, s])
csvData.push(row.join(","))
})
});
if (csvData.length > 1) {
downloadObjectAsJson(csvData.join("\r\n"));
} else {
console.log("Nothing processed");
}
}
function formatDate(datestr) {
const date = new Date(datestr);
const datePart = date.toLocaleString("en-us", {month: "numeric",day: "numeric",year: "numeric"});
const timePart = date.toLocaleString("en-us", {hour12: true, hour: "2-digit",minute: "2-digit"});
return datePart+ " " +timePart
}
function formatRoom(roomStr) {
if(roomStr.includes('Sydney Marcus')) return 'Sydney Marcus';
const number = roomStr.match(/\d+/g);
if(number) return "A" + number[0];
else return null;
}
function downloadObjectAsJson(exportObj, exportName) {
var dataStr = "data:text/plain;charset=utf-8," + encodeURIComponent(exportObj);
var downloadAnchorNode = document.createElement("a")
downloadAnchorNode.setAttribute("href", dataStr);
downloadAnchorNode.setAttribute("download", "sessions_export.csv");
document.body.appendChild(downloadAnchorNode); // required for firefox
downloadAnchorNode.click();
downloadAnchorNode.remove();
}
</script>
</html>