forked from archerydwd/DWD-RealtimeIrishRail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
irishRailFetcher.js
130 lines (120 loc) · 4.54 KB
/
irishRailFetcher.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
const xml2js= require("xml2js");
const request = require("request");
//returns the Irish rail xml data converted to json, copied from python
const build_trains = function(train){
t = {}
t['destination'] = train['Destination']
t['origin'] = train['Origin']
t['eta'] = train['Exparrival']
t['due_in'] = train['Duein']
t['scheduled_arrival'] = train['Scharrival']
t['location'] = train['Lastlocation']
return t
}
//given the js data, will process to an array of objects
const processTrainObjs = function(data){
//data json will only have objStationData key if some trains are running
if(data.hasOwnProperty("objStationData")){
//read the objStationData
data = data["objStationData"];
//convert single to array for uniformity
if(!Array.isArray(data)){
data = [data];
}
//process array
let train_data = data.map(build_trains);
return train_data;
}
else{
return [];
// console.log("no data");
}
}
const convertxml = function(data){
let parser = new xml2js.Parser({explicitArray : false});
let res = "";
parser.parseString(data, function(err, result) {
res = result["ArrayOfObjStationData"];
});
return res;
}
//returns the Irish rail xml data converted to json
const IrishRailFetcher = function(url){
let parser = new xml2js.Parser({explicitArray : false});
let headers = {
"User-Agent": "Mozilla/5.0 MagicMirror/" + global.version
};
let result;
//use fetch in future to speed up
// let fetcher = fetch(url, {headers: headers}
//let result = processTrainObjs(convertxml(fetcher.text()));
/* let result = fetcher.then((response) => response.text())
.then((data) => convertxml(data))
.then((data) => processTrainObjs(data));
*/
request({url: url, method: "GET"},function(error, response, body){
if(!error && response.statusCode == 200){
result=processTrainObjs(convertxml(body));
}
});
//return here doesn't work, need to do async version...
return result;
}
let xml_string = `<?xml version="1.0" encoding="utf-8"?>
<ArrayOfObjStationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://api.irishrail.ie/realtime/">
<objStationData>
<Servertime>2022-05-19T13:14:14.757</Servertime>
<Traincode>A511 </Traincode>
<Stationfullname>Carlow</Stationfullname>
<Stationcode>CRLOW</Stationcode>
<Querytime>13:14:14</Querytime>
<Traindate>19 May 2022</Traindate>
<Origin>Waterford</Origin>
<Destination>Dublin Heuston</Destination>
<Origintime>13:05</Origintime>
<Destinationtime>15:21</Destinationtime>
<Status>No Information</Status>
<Lastlocation />
<Duein>60</Duein>
<Late>0</Late>
<Exparrival>14:12</Exparrival>
<Expdepart>14:14</Expdepart>
<Scharrival>14:12</Scharrival>
<Schdepart>14:14</Schdepart>
<Direction>To Dublin Heuston</Direction>
<Traintype>Train</Traintype>
<Locationtype>S</Locationtype>
</objStationData>
<objStationData>
<Servertime>2022-05-19T13:14:14.757</Servertime>
<Traincode>A506 </Traincode>
<Stationfullname>Carlow</Stationfullname>
<Stationcode>CRLOW</Stationcode>
<Querytime>13:14:14</Querytime>
<Traindate>19 May 2022</Traindate>
<Origin>Dublin Heuston</Origin>
<Destination>Waterford</Destination>
<Origintime>13:15</Origintime>
<Destinationtime>15:30</Destinationtime>
<Status>En Route</Status>
<Lastlocation>Arrived Inchicore Advance Starter</Lastlocation>
<Duein>51</Duein>
<Late>-9</Late>
<Exparrival>14:00</Exparrival>
<Expdepart>14:05</Expdepart>
<Scharrival>14:09</Scharrival>
<Schdepart>14:14</Schdepart>
<Direction>To Waterford</Direction>
<Traintype>Train</Traintype>
<Locationtype>S</Locationtype>
</objStationData>
</ArrayOfObjStationData>
`
/*
let res = IrishRailFetcher("http://api.irishrail.ie/realtime/realtime.asmx/getStationDataByCodeXML_WithNumMins?StationCode=HSTON&NumMins=90", console.log);
*/
console.log(processTrainObjs(convertxml(xml_string)));
exports.IrishRailFetcher = IrishRailFetcher;
exports.build_trains = build_trains;
exports.processTrainObjs = processTrainObjs;
exports.convertxml = convertxml;