-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
267 lines (209 loc) · 7.5 KB
/
main.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const request = require('request'); // To get website data
const fs = require('fs'); // To output files
var cheerio = require('cheerio'); // To work with html data
var cheerioTableparser = require('cheerio-tableparser'); // The most useful tool to parse those pesky tables.
var he = require('he'); // To decode HTML entities to utf-8 strings
var beautify = require("json-beautify"); // To output pretty-printed JSON.
if (!fs.existsSync('./json')){
console.log("Creating directory: ./json")
fs.mkdirSync('./json');
}
getEverything();
function getPlan(kategorie){
// Open timetable website of class
request('https://www.h-ab.de/fileadmin/dokumente/stundenplan/klassisch/'+kategorie+'.html', function (error, response, body) {
// If website is offline
if(error){
console.log("Error fetching website "+kategorie);
return;
}
$ = cheerio.load(body);
// Remove footnotes
$( ".fn" ).remove();
$( ".fnft" ).remove();
$('span').remove();
var plan = {};
var vl = [];
var tblNumber = 0;
// For each week:
$('table').each(function () {
tblNumber += 1;
// Parse week
var parsed = parseWeek(this);
plan[tblNumber] = parsed[0];
vl.push(parsed[1]);
})
// Done
var currTime = new Date();
planFinal = {timeCreated: currTime, plan: plan};
vlFinal = {timeCreated: currTime, vl: vl};
makePlainLists(kategorie, vlFinal.vl);
/*********************************
* THE MAIN PARSING HAPPENS HERE *
********************************/
function parseWeek(week) {
$w = cheerio.load(week);
cheerioTableparser($w);
var data = $w("table").parsetable(true, false, false);
// Read more about the cheerio tableparser here:
// https://www.npmjs.com/package/cheerio-tableparser
var woche= [];
for(var i=3;i<data.length;i++){
// Get rid of empty or meaningless cells (There are a lot of them)
var col = data[i].filter(function (el) {
return el != " "&& el != "";
});
if(col.length>1){
woche.push(col);
}
}
// Remove time column
woche.splice(-1,1);
// Sort "double lectures" (that happen at the same time at the same day simultaneously) into correct day array
for(var i=1;i<woche.length;i++){
if (woche[i][0] === woche[i-1][0]){
for (var j=1;j<woche[i].length;j++){
woche[i-1].push(woche[i][j])
}
woche[i]="";
}
}
// Remove empty elements
var woche = woche.filter(function (el) {
return el != "";
});
// We now have a nice array (woche) for that week.
// This needs to me converted into a well-readable object
var weekObject = {};
var vorlesungArray = [];
var weekNum = 0;
woche.forEach(function(tag){
var vorlesungen = tag.slice(1);
var vorlesungObject = {};
vorlesungen.forEach(function(vorlesung){
// This checks if there is enough information. Some courses don't provide much information, this helps mitigate this problem.
if(vorlesung.split("<br>").length<4){
uhrzeit = returnLastItem(vorlesung.split("<br>")[0].split(">"));
ende = returnLastItem(vorlesung.split("<br>")[0].split(">")).split(" - ")[1].split(" ")[0];
name = he.decode(vorlesung.split("<br>")[1]);
gruppe = he.decode(returnLastItem(vorlesung.split("<br>")).split("</a>")[0].split("<")[0]);
prof = "";
raum = "";
// This is the normal behavior:
} else {
uhrzeit = returnLastItem(vorlesung.split("<br>")[0].split(">"));
ende = returnLastItem(vorlesung.split("<br>")[0].split(">")).split(" - ")[1].split(" ")[0]
name = he.decode(vorlesung.split("<br>")[1]);
prof = he.decode(vorlesung.split("<br>")[2]);
raum = he.decode(vorlesung.split("<br>")[3].split("<")[0]);
gruppe = he.decode(returnLastItem(vorlesung.split("<br>")).split("</a>")[0].split("<")[0]);
}
vorlesungObject = {
vorlesung: name,
//datum: tag[0], // These are obsolete. Use the timestamps (timestamp, ende) instead
//zeit: uhrzeit,
dozent: prof,
raum: raum,
gruppe: gruppe,
timestamp: parseDate(tag[0],uhrzeit),
ende: parseDate(tag[0],ende)
}
vorlesungArray.push(vorlesungObject)
})
weekObject[weekNum] = vorlesungObject;
weekNum++;
});
// Returns the last item of an array without modifying it
function returnLastItem(array){
return array[array.length-1];
}
// Parses the date and time from strings and returns a timestamp
function parseDate(tag,uhrzeit){
var origStr = tag;
var origTime = uhrzeit;
//var oDay = parseInt(origStr.split(",")[0]); // This just returns the name of the day. Useless.
var oDate = parseInt(origStr.split(", ")[1].split(".")[0]);
var oMonth = parseInt(origStr.split(", ")[1].split(".")[1])-1;
var oTimeStart = origTime.split(" - ")[0];
var oTimeStartHours = parseInt(oTimeStart.split(":")[0]);
var oTimeStartMinutes = parseInt(oTimeStart.split(":")[1]);
var parsedDate = new Date();
parsedDate.setMonth(oMonth);
parsedDate.setDate(oDate);
parsedDate.setHours(oTimeStartHours);
parsedDate.setMinutes(oTimeStartMinutes);
parsedDate.setSeconds(0);
parsedDate.setMilliseconds(0);
parsedDate.setFullYear(fixYear(parsedDate));
// Timezones are confusing. On my Raspberry, these are showing local time, on my PC they are UTC.
// Since years are not provided in the table, we need to determine which year it is talking about.
// Usually is the current year, but can also be next year.
function fixYear(parsedDate){
var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var difference = currentDate - parsedDate;
// If the script assumes it's too far in the past, then it must be in the future (i.e. next year)
if(difference > 12960000000){
correctYear = currentYear + 1;
} else {
correctYear = currentYear;
}
return correctYear;
}
return parsedDate;
}
return [weekObject,vorlesungArray];
}
});
}
// Check which courses exist.
function getEverything(){
// This site has a handy list of all currently running classes:
request('https://www.h-ab.de/fileadmin/dokumente/stundenplan/klassisch/index.html', function (error, response, body) {
$ = cheerio.load(body);
var list = [];
// A prototype method to remove an element from an array by specifying a string
Array.prototype.remove = function() {
var what, a = arguments, L = a.length, ax;
while (L && this.length) {
what = a[--L];
while ((ax = this.indexOf(what)) !== -1) {
this.splice(ax, 1);
}
}
return this;
};
// Collect the short names of the classes (the significant part of the respective URLs)
$('a').each(function(){
var link = $(this).attr("href").split(".html")[0];
list.push(link);
})
// This gets parsed too, but since it's not a class, we don't want this.
list.remove("http://www.sked.de");
// Output the list to a file.
fs.writeFileSync("json/CLASSLIST.json", JSON.stringify(list));
// Now do some work
list.forEach(function(kat){
getPlan(kat,list);
})
})
}
// The last step: Cleaning up.
function makePlainLists(kategorie, all){
var upcoming = [];
// Get rid of the weeks
all.forEach(function(woche){
woche.forEach(function(vl){
upcoming.push(vl);
})
})
// Sort the whole thing by date
upcoming.sort(function(a,b){
return new Date(a.timestamp) - new Date(b.timestamp);
})
// Output to file
if (upcoming.length > 0) {
fs.writeFileSync("json/"+kategorie+".json", beautify(upcoming,null,2,80));
console.log("Generated successfully: "+kategorie+".json");
}
}