Scraping code and basic view done

This commit is contained in:
Claudio Maggioni 2019-09-26 13:58:02 +02:00
commit eab591188e
2 changed files with 121 additions and 0 deletions

31
index.html Normal file
View File

@ -0,0 +1,31 @@
<!DOCTYPE html>
<!-- vim: set ts=2 sw=2 et tw=80: -->
<html>
<head>
<meta charset='utf-8'>
<title>USI INF room checker</title>
</head>
<body>
<h1>USI INF room checker</h1>
<h4>By maggicl</h4>
<section class="times">
</section>
<template id="room">
<h2 class="room-title"></h2>
<ul class="list"></ul>
</template>
<template id="time-free">
<li><strong>Free all day</strong></li>
</template>
<template id="time-slot">
<li>
<span class="title"></span>
(<span class="start"></span>-<span class="end"></span>)
</li>
</template>
<script src='index.js'></script>
</body>
</html>

90
index.js Normal file
View File

@ -0,0 +1,90 @@
// vim: set ts=2 sw=2 et tw=80:
function roomStatus(room, callback) {
return new Promise((resolve, _) => {
const xhr = new XMLHttpRequest();
xhr.addEventListener('load', () => {
const parser = new DOMParser();
const doc = parser.parseFromString(xhr.responseText, 'text/html');
const lessons =
doc.querySelectorAll('table.rsContentTable div.rsAptSimple');
const parsed = [];
for (let lesson of lessons) {
const time = lesson.querySelector('span[id$=lblOrario]');
const start = new Date();
start.setHours(parseInt(time.innerHTML.substring(1,3)));
start.setMinutes(parseInt(time.innerHTML.substring(4,6)));
start.setSeconds(0);
const end = new Date();
end.setHours(parseInt(time.innerHTML.substring(7,9)));
end.setMinutes(parseInt(time.innerHTML.substring(10,12)));
end.setSeconds(0);
parsed.push({
title: lesson.getAttribute('title'),
start: start,
end: end
});
}
resolve(parsed);
});
xhr.open('GET', 'https://aule.usi.ch/aule/View.aspx?name=' + room);
xhr.send();
});
}
const ROOMS = [
'SI-004',
'SI-005',
'SI-006',
'SI-007',
'SI-008',
'SI-013',
'SI-015'
];
const ROOM_LIST = document.querySelector(".times");
const ROOM_TEMPLATE = document.getElementById("room");
const SLOT_TEMPLATE = document.getElementById("time-slot");
const FREE_SLOT_TEMPLATE = document.getElementById("time-free");
function formatTime(date) {
return (date.getHours() < 10 ? '0' : '') + date.getHours() + ':' +
(date.getMinutes() < 10 ? '0' : '') + date.getMinutes();
}
async function buildRoomMarkup(roomTitle) {
const data = await roomStatus(roomTitle);
const room = document.importNode(ROOM_TEMPLATE.content, true);
const title = room.querySelector('.room-title');
title.innerHTML = roomTitle;
const list = room.querySelector('.list');
for (const d of data) {
const slot = document.importNode(SLOT_TEMPLATE.content, true);
const title = slot.querySelector('.title');
document.getElementById("room");title.innerHTML = d.title;
const start = slot.querySelector('.start');
start.innerHTML = formatTime(d.start);
const end = slot.querySelector('.end');
end.innerHTML = formatTime(d.end);
list.appendChild(slot);
}
if (data.length == 0) {
list.appendChild(document.importNode(FREE_SLOT_TEMPLATE.content, true));
}
ROOM_LIST.appendChild(room);
}
(async function() {
for (const room of ROOMS) {
await buildRoomMarkup(room);
}
})()