-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
69 lines (64 loc) · 2.17 KB
/
index.js
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
'use strict'
const cheerio = require('cheerio');
const axios = require('axios');
const env = require('./environment.json');
const replaceall = require('replaceall');
function sanitize(data) {
return replaceall("\t", "", replaceall("\n", "", data));
}
function isTime(data) {
const regex = /[-\.]+/g;
let m;
return regex.test(data);
}
axios.get(env.url).then(response => {
let $ = cheerio.load(response.data);
let schedules = [];
let meta = {};
let rowNum = 0, cellNum = 0, day = "";
let metaErase = false;
$('table').each((i, elm) => {
rowNum = 0;
metaErase = true;
//console.log(elm, $(elm));
$(elm).children().each((i, body) => {
$(body).children().each((j, row) => {
cellNum = 0;
$(row).children().each((k, cell) => {
let rowspan = cell.attribs.rowspan;
if (rowspan && rowspan > 1) {
day = sanitize($(cell).text());
}
if (rowNum == 0) {
if (cellNum > 0) {
let time = sanitize($(cell).text());
if(isTime(time)){
if(metaErase){
meta = {};
metaErase = false;
}
meta[cellNum] = time;
}
}
} else {
let data = sanitize($(cell).text());
if (data.startsWith(env.course)) {
schedules.push({
day: day,
time: meta[cellNum + 1],
course: data
})
}
}
cellNum++;
})
rowNum++;
})
});
})
return schedules;
})
.then(rows => {
console.log(rows);
console.log(rows.length);
})