-
Notifications
You must be signed in to change notification settings - Fork 0
/
webtest.js
144 lines (132 loc) · 6.82 KB
/
webtest.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
const cheerio = require('cheerio');
const fs = require('fs')
const util = require('util')
const package = require('./package.json');
const os = require('os');
const request = require("request");
//let Gdata = "";
const customFetch = request.defaults({
headers: { 'User-Agent': `OpenVGN/${package.version} (NodeJS_${process.env.NODE_VERSION}) ${os.platform()} (${os.arch()}) NodeJS Wrapper` }
})
// Enable this to load a old webpage with all possible elements.
/*
fs.readFile('./test/VAGHtmlTestTemplates/AZandUbahn.txt', 'utf8', (err, data) => {
if (err) { console.log(err) }
Gdata = data
askURL();
})
*/
const askURL = function () {
return new Promise(function (resolve, reject) {
let Time_Started = new Date().getTime();
customFetch("https://www.vag.de/fahrplan/fahrplanaenderungen-stoerungen", { json: false }, (err, res, body) => {
if (err) { reject(err); }
//body = Gdata
let Time_Started_parse = new Date().getTime();
const $ = cheerio.load(body);
let schedule_changes = {};
let disturbance_list = {};
let disturbance = {};
let Key = "";
$('div[class=linieninfo_list]').children().each(function (i, e) {
if ($(this).find('stoer_list_content')) {
//console.log($(this).text())
}
//Aktuelle Störungen
if ($(this).html().includes("Aktuelle Störungen")) {
disturbance["S"] = []
Key = "S"
}
//Aktuelle Fahrplanänderungen
if ($(this).html().includes("Aktuelle Fahrplanänderungen")) {
disturbance["F"] = []
Key = "F"
}
//Skip Headlines
if ($(this).text().includes("Aktuelle Störungen") || $(this).text().includes("Aktuelle Fahrplanänderungen")) {
} else {
//console.log($(this).text())
disturbance[Key].push($(this).text())
}
});
//out.interruption
let tracker = "";
// Parse Aktuelle Störungen
if(disturbance.hasOwnProperty("S")){
//console.log(disturbance)
disturbance["S"].map((Key, i) => {
Key = Key.replace(/\r?\n|\r/g, "")
Key = Key.replace(/\s\s+/g, ' ').split(" ")
if (!(i & 1)) {
tracker = Key[0].trim().split(" ")[0];
disturbance_list[tracker] = [];
} else {
for (let i = 1; i < Key.length; i = i + 3) {
if (Key[i] === "") { continue; }
const UpdatedString = `${Key[i + 2].split(":")[1]}:${Key[i + 2].split(":")[2]}`
const UpdatedStringDate = UpdatedString.split("um")[0].trim().split(".")
const UpdatedStringTIme = UpdatedString.split("um")[1]
//console.log(Key[i])
const UpdatedStamp = new Date(`${[UpdatedStringDate[2], UpdatedStringDate[1], UpdatedStringDate[0]].join("-")} ${UpdatedStringTIme.replace("Uhr", "").trim()}`);
// Create Object if Aufzugsstörungen
if(tracker === "Aufzugsstörungen"){
disturbance_list[tracker].push({
"Where": Key[i + 1],
"Station": Key[i].split(",")[0],
"What": Key[i].split(",")[1],
"Updated": UpdatedStamp
})
}
// Create Object if U-Bahn
if(tracker === "U-Bahn" || tracker === "Bus" || tracker === "Tram"){
disturbance_list[tracker].push({
"Until": Key[i + 1].replace("Uhr", "").trim().slice(8),
"Line": Key[i].split(":")[0].slice(6, Key[i].length),
"What": Key[i].split(":")[1],
"Updated": UpdatedStamp
})
}
}
}
})
}
// Parse Aktuelle Fahrplanänderungen
if(disturbance.hasOwnProperty("F")){
disturbance["F"].map((Key, i) => {
Key = Key.replace(/\r?\n|\r/g, "")
Key = Key.replace(/\s\s+/g, ' ').split(" ")
if (!(i & 1)) {
tracker = Key[0].trim().split(" ")[0];
schedule_changes[tracker] = [];
} else {
for (let i = 1; i < Key.length; i = i + 3) {
if (Key[i] === "") { continue; }
if (Key[i].toLowerCase().startsWith("linie")) {
const UpdatedString = `${Key[i + 2].split(":")[1]}:${Key[i + 2].split(":")[2]}`
const UpdatedStringDate = UpdatedString.split("um")[0].trim().split(".")
const UpdatedStringTIme = UpdatedString.split("um")[1]
const UpdatedStamp = new Date(`${[UpdatedStringDate[2], UpdatedStringDate[1], UpdatedStringDate[0]].join("-")} ${UpdatedStringTIme.replace("Uhr", "").trim()}`);
// Create Object
schedule_changes[tracker].push({
"Line": Key[i].split(":")[0].slice(6, Key[i].length),
"What": Key[i].split(":")[1],
"Start": (Key[i + 1].split(" ")[1] != "auf") ? Key[i + 1].split(" ")[1] : "Now",
"End": (Key[i + 1].split(" ")[3] != null) ? Key[i + 1].split(" ")[3] : "Later",
"Updated": UpdatedStamp
})
}
}
}
})
}
console.log(console.log(util.inspect({schedule_changes: schedule_changes, disturbances: disturbance_list}, false, null, true /* enable colors */)))
/*console.log(JSON.stringify({schedule_changes: schedule_changes, disturbances: disturbance_list, Meta: {
Timestamp: new Date,
RequestTime: new Date().getTime() - Time_Started,
ParseTime: new Date().getTime() - Time_Started_parse,
URL: 'https://www.vag.de/fahrplan/fahrplanaenderungen-stoerungen'
}}))*/
});
});
};
askURL();