-
Notifications
You must be signed in to change notification settings - Fork 10
/
node_helper.js
executable file
·124 lines (118 loc) · 3.8 KB
/
node_helper.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
/*
* Magic Mirror
* Node Helper: mvgmunich
*
* By Simon Crnko
* MIT Licensed
*
*/
const NodeHelper = require("node_helper");
const request = require("request");
const urlencode = require("urlencode");
const globals = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"X-MVG-Authorization-Key": "5af1beca494712ed38d313714d4caff6",
"Referer": "https://www.mvg.de/dienste/abfahrtszeiten.html",
"Accept-Encoding": "gzip",
"Accept-Language": "en-US,en;q=0.9,de;q=0.8"
};
const mvgAPI = "https://www.mvg.de/";
const apiBase = mvgAPI + "api/fahrinfo/departure/";
const stationQuery = mvgAPI + "api/fahrinfo/location/queryWeb?q=";
const interruptionsURL = mvgAPI + ".rest/betriebsaenderungen/api/interruptions?_=";
module.exports = NodeHelper.create({
socketNotificationReceived: function (notification, payload) {
const self = this;
switch (notification) {
case "GET_STATION_INFO":
self.getStationInfo(payload);
self.getInterruptionsInfo();
break;
case "GET_DEPARTURE_DATA":
self.getDepartureInfo(payload);
break;
default:
console.error("Switch item {} is missing", notification);
}
},
getDepartureInfo: function (payload) {
const self = this;
request({
headers: globals,
uri: apiBase + payload.haltestelleId + "?footway=" + payload.timeToWalk,
method: "GET",
gzip: true
}, function (error, response, body) {
if (error) {
// Error while reading departure data ...
console.error("Error while reading departure info", error);
self.sendSocketNotification("ERROR", "Error while reading data: " + error.message);
} else {
// body is the decompressed response body
try {
payload.transport = JSON.parse(body);
self.sendSocketNotification("UPDATE_DEPARTURE_INFO", payload);
} catch (e) {
console.error("Error while parsing and sending departure info", e);
self.sendSocketNotification("ERROR_NO_DEPARTURE_DATA", "");
}
}
});
},
getStationInfo: function (payload) {
const self = this;
request({
headers: globals,
uri: stationQuery + urlencode(payload.haltestelle),
method: "GET",
gzip: true
}, function (error, response, body) {
if (error) {
// Error while reading station data ...
console.error("Error while reading station data", error);
self.sendSocketNotification("ERROR", "Error while reading data: " + error.message);
} else {
// body is the decompressed response body
try {
const jsonObject = JSON.parse(body);
if (jsonObject.locations[0].id === undefined) {
self.sendSocketNotification("ERROR_NO_STATION", "");
} else {
payload.haltestelleId = jsonObject.locations[0].id;
payload.haltestelleName = jsonObject.locations[0].name;
self.sendSocketNotification("UPDATE_STATION", payload);
}
} catch (e) {
console.error("Error while parsing and sending station info", e);
self.sendSocketNotification("ERROR_NO_STATION", "");
}
}
});
},
getInterruptionsInfo: function () {
const self = this;
request({
headers: globals,
uri: interruptionsURL + new Date().getMilliseconds(),
method: "GET",
gzip: true
}, function (error, response, body) {
if (error) {
// Error while reading interruptions data ...
console.error("Error while reading interruptions data", error);
} else {
// body is the decompressed response body
try {
let jsonObject = JSON.parse(body);
self.sendSocketNotification("UPDATE_INTERRUPTION_DATA", jsonObject);
} catch (e) {
// Error while reading interruptions data ...
console.error("Error while parsing interruptions data", e);
}
}
});
},
});