1 |
torben |
305 |
package dk.thoerup.traininfoservice.banedk;
|
2 |
|
|
|
3 |
torben |
978 |
|
4 |
torben |
992 |
import java.net.URL;
|
5 |
torben |
994 |
import java.net.URLEncoder;
|
6 |
torben |
307 |
import java.util.Collections;
|
7 |
torben |
1562 |
import java.util.Comparator;
|
8 |
torben |
428 |
import java.util.Map;
|
9 |
torben |
348 |
import java.util.logging.Logger;
|
10 |
torben |
305 |
|
11 |
torben |
992 |
import org.jsoup.nodes.Document;
|
12 |
|
|
import org.jsoup.nodes.Element;
|
13 |
|
|
import org.jsoup.select.Elements;
|
14 |
torben |
305 |
|
15 |
torben |
1061 |
import dk.thoerup.android.traininfo.common.DepartureBean;
|
16 |
|
|
import dk.thoerup.android.traininfo.common.DepartureEntry;
|
17 |
torben |
1409 |
import dk.thoerup.android.traininfo.common.StationEntry;
|
18 |
torben |
468 |
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
19 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
20 |
torben |
1366 |
import dk.thoerup.genericjavautils.HttpUtil;
|
21 |
torben |
1355 |
import dk.thoerup.genericjavautils.TimeoutMap;
|
22 |
torben |
711 |
import dk.thoerup.traininfoservice.Statistics;
|
23 |
torben |
1305 |
import dk.thoerup.traininfoservice.TraininfoSettings;
|
24 |
torben |
1255 |
import dk.thoerup.traininfoservice.db.StationDAO;
|
25 |
torben |
307 |
|
26 |
torben |
305 |
public class DepartureFetcher {
|
27 |
torben |
348 |
|
28 |
torben |
972 |
enum TrainType{
|
29 |
|
|
STOG,
|
30 |
|
|
REGIONAL
|
31 |
|
|
}
|
32 |
|
|
|
33 |
torben |
1248 |
enum FetchTrainType {
|
34 |
|
|
STOG,
|
35 |
|
|
REGIONAL,
|
36 |
|
|
BOTH
|
37 |
|
|
}
|
38 |
|
|
|
39 |
torben |
348 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
|
40 |
torben |
387 |
|
41 |
torben |
978 |
Map<String, DepartureBean> cache;
|
42 |
torben |
387 |
|
43 |
torben |
588 |
StationDAO stationDao = new StationDAO();
|
44 |
|
|
|
45 |
torben |
1303 |
|
46 |
|
|
private TraininfoSettings settings;
|
47 |
torben |
580 |
|
48 |
torben |
1562 |
Comparator<DepartureEntry> departureTimeComparator = new Comparator<DepartureEntry>() {
|
49 |
|
|
|
50 |
|
|
@Override
|
51 |
|
|
public int compare(DepartureEntry arg0, DepartureEntry arg1) {
|
52 |
|
|
String timeStr1 = arg0.getTime().replace(":","").trim();
|
53 |
|
|
String timeStr2 = arg1.getTime().replace(":","").trim();
|
54 |
|
|
|
55 |
|
|
int time1 = 0;
|
56 |
|
|
int time2 = 0;
|
57 |
|
|
|
58 |
|
|
if (timeStr1.length() > 0)
|
59 |
|
|
time1 = Integer.parseInt(timeStr1);
|
60 |
|
|
|
61 |
|
|
if (timeStr2.length() > 0)
|
62 |
|
|
time2 = Integer.parseInt(timeStr2);
|
63 |
|
|
|
64 |
|
|
//work correctly when clock wraps around at midnight
|
65 |
|
|
if (Math.abs(time1-time2) < 1200) {
|
66 |
|
|
if (time1 > time2)
|
67 |
|
|
return 1;
|
68 |
|
|
else
|
69 |
|
|
return -1;
|
70 |
|
|
} else {
|
71 |
|
|
if (time1 < time2)
|
72 |
|
|
return 1;
|
73 |
|
|
else
|
74 |
|
|
return -1;
|
75 |
|
|
|
76 |
|
|
}
|
77 |
|
|
|
78 |
|
|
}
|
79 |
|
|
|
80 |
|
|
};
|
81 |
|
|
|
82 |
torben |
1303 |
public DepartureFetcher(TraininfoSettings settings) {
|
83 |
|
|
this.settings = settings;
|
84 |
|
|
cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );
|
85 |
torben |
580 |
}
|
86 |
|
|
|
87 |
|
|
|
88 |
torben |
307 |
|
89 |
torben |
387 |
|
90 |
torben |
1248 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception {
|
91 |
torben |
980 |
|
92 |
torben |
1248 |
final String key = "" + stationID + ":" + arrival + ":" + type.toString();
|
93 |
|
|
|
94 |
torben |
978 |
DepartureBean departureBean = cache.get(key);
|
95 |
torben |
308 |
|
96 |
torben |
387 |
|
97 |
torben |
978 |
if (departureBean == null) {
|
98 |
torben |
1248 |
departureBean = lookupDepartures(stationID, arrival, type);
|
99 |
torben |
978 |
cache.put(key, departureBean);
|
100 |
torben |
387 |
} else {
|
101 |
torben |
711 |
Statistics.getInstance().incrementDepartureCacheHits();
|
102 |
torben |
829 |
logger.info("Departure: Cache hit " + key); //remove before production
|
103 |
torben |
387 |
}
|
104 |
torben |
980 |
return departureBean;
|
105 |
torben |
387 |
}
|
106 |
|
|
|
107 |
|
|
|
108 |
torben |
1248 |
public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception {
|
109 |
torben |
307 |
|
110 |
torben |
978 |
DepartureBean departureBean = new DepartureBean();
|
111 |
|
|
|
112 |
torben |
1060 |
StationEntry station = stationDao.getById(stationID);
|
113 |
torben |
307 |
|
114 |
torben |
1021 |
departureBean.stationName = station.getName();
|
115 |
torben |
1252 |
|
116 |
torben |
1424 |
//TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
|
117 |
torben |
1248 |
if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {
|
118 |
torben |
978 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
|
119 |
torben |
1063 |
departureBean.entries.addAll( tempBean.entries );
|
120 |
torben |
978 |
departureBean.notifications.addAll(tempBean.notifications);
|
121 |
torben |
307 |
}
|
122 |
|
|
|
123 |
torben |
1248 |
if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) {
|
124 |
torben |
978 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
|
125 |
torben |
1063 |
departureBean.entries.addAll( tempBean.entries );
|
126 |
torben |
978 |
departureBean.notifications.addAll(tempBean.notifications);
|
127 |
torben |
588 |
}
|
128 |
|
|
|
129 |
torben |
1063 |
if (departureBean.entries.size() == 0) {
|
130 |
torben |
1037 |
logger.info("No departures found for station " + stationID);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
torben |
1424 |
//TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
|
134 |
torben |
1252 |
if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time
|
135 |
torben |
1562 |
Collections.sort( departureBean.entries, departureTimeComparator);
|
136 |
torben |
1252 |
}
|
137 |
torben |
1832 |
|
138 |
|
|
System.out.println("Trit: " + settings.isTritinfoEnabled() + " " + station.getTritStation() );
|
139 |
|
|
if ( settings.isTritinfoEnabled() && station.getTritStation() != -1) {
|
140 |
|
|
try {
|
141 |
|
|
injectTritinfoData(departureBean, station);
|
142 |
|
|
} catch (Exception ex) { //det er ikke kritisk at vi får perron numre med
|
143 |
|
|
ex.printStackTrace();
|
144 |
|
|
}
|
145 |
|
|
}
|
146 |
torben |
588 |
|
147 |
|
|
|
148 |
torben |
978 |
return departureBean;
|
149 |
torben |
305 |
}
|
150 |
|
|
|
151 |
torben |
978 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
|
152 |
torben |
1372 |
if ( settings.getBackend() == TraininfoSettings.Backend.Azure) {
|
153 |
torben |
1034 |
return lookupDeparturesAzureSite(stationcode, type, arrival);
|
154 |
|
|
} else {
|
155 |
torben |
1330 |
return lookupDeparturesMobileSite(stationcode, type, arrival);
|
156 |
torben |
580 |
}
|
157 |
|
|
}
|
158 |
|
|
|
159 |
torben |
1034 |
private String getTypeStringAzure(TrainType type) {
|
160 |
torben |
972 |
switch (type) {
|
161 |
|
|
case STOG:
|
162 |
|
|
return "S-Tog";
|
163 |
|
|
case REGIONAL:
|
164 |
|
|
return "Fjerntog";
|
165 |
|
|
default:
|
166 |
|
|
return ""; //Can not happen
|
167 |
|
|
}
|
168 |
|
|
}
|
169 |
|
|
|
170 |
torben |
1034 |
private String getTypeStringWww(TrainType type) {
|
171 |
|
|
switch (type) {
|
172 |
|
|
case STOG:
|
173 |
|
|
return "S2";
|
174 |
|
|
case REGIONAL:
|
175 |
|
|
return "FJRN";
|
176 |
|
|
default:
|
177 |
|
|
return ""; //Can not happen
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception {
|
182 |
torben |
305 |
|
183 |
torben |
978 |
DepartureBean departureBean = new DepartureBean();
|
184 |
torben |
992 |
|
185 |
torben |
970 |
|
186 |
torben |
1034 |
String typeString = getTypeStringAzure(type);
|
187 |
torben |
970 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
|
188 |
torben |
994 |
|
189 |
|
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
|
190 |
torben |
970 |
|
191 |
torben |
1034 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";
|
192 |
torben |
994 |
|
193 |
torben |
1048 |
logger.fine("URI: " + uri);
|
194 |
torben |
1303 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
|
195 |
torben |
421 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
196 |
torben |
305 |
|
197 |
torben |
992 |
Document page = (Document) breaker.invoke(wrapper);
|
198 |
torben |
305 |
|
199 |
torben |
829 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
|
200 |
torben |
992 |
Element table = page.getElementById(tableName);
|
201 |
torben |
829 |
|
202 |
torben |
342 |
if (table != null) {
|
203 |
torben |
992 |
Elements tableRows = table.getElementsByTag("tr");
|
204 |
torben |
342 |
|
205 |
torben |
1188 |
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
|
206 |
|
|
//boolean passedTidsstreg = false;
|
207 |
torben |
1020 |
|
208 |
torben |
992 |
for (Element currentRow : tableRows) {
|
209 |
|
|
String rowClass = currentRow.attr("class");
|
210 |
torben |
1188 |
/*
|
211 |
torben |
1020 |
if (tidsstregExists == true && passedTidsstreg == false) {
|
212 |
|
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
|
213 |
|
|
passedTidsstreg = true;
|
214 |
|
|
} else {
|
215 |
|
|
continue;
|
216 |
|
|
}
|
217 |
torben |
1188 |
}*/
|
218 |
torben |
1020 |
|
219 |
torben |
342 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
|
220 |
torben |
1020 |
|
221 |
torben |
992 |
Elements fields = currentRow.getElementsByTag("td");
|
222 |
torben |
342 |
|
223 |
torben |
978 |
DepartureEntry departure = new DepartureEntry();
|
224 |
torben |
342 |
|
225 |
torben |
992 |
String time = fields.get(0).text();
|
226 |
torben |
375 |
if (time.equals(""))
|
227 |
|
|
time = "0:00"; //Bane.dk bug work-around
|
228 |
torben |
342 |
departure.setTime(time);
|
229 |
|
|
|
230 |
|
|
int updated = extractUpdated( fields.get(1) );
|
231 |
|
|
departure.setUpdated(updated);
|
232 |
|
|
|
233 |
torben |
992 |
String trainNumber = fields.get(2).text();
|
234 |
torben |
972 |
if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
|
235 |
torben |
1039 |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));
|
236 |
torben |
342 |
departure.setTrainNumber(trainNumber);
|
237 |
|
|
|
238 |
torben |
992 |
String destination = fields.get(3).text();
|
239 |
torben |
342 |
departure.setDestination(destination);
|
240 |
|
|
|
241 |
torben |
992 |
String origin = fields.get(4).text();
|
242 |
torben |
342 |
departure.setOrigin(origin);
|
243 |
|
|
|
244 |
torben |
992 |
String location = fields.get(5).text();
|
245 |
torben |
342 |
departure.setLocation(location);
|
246 |
|
|
|
247 |
torben |
992 |
String status = fields.get(6).text().trim();
|
248 |
torben |
342 |
departure.setStatus(status);
|
249 |
|
|
|
250 |
|
|
String note = extractNote( fields.get(7) );
|
251 |
|
|
departure.setNote(note);
|
252 |
|
|
|
253 |
torben |
972 |
departure.setType(typeString);
|
254 |
torben |
697 |
|
255 |
torben |
1063 |
departureBean.entries.add( departure );
|
256 |
torben |
342 |
}
|
257 |
|
|
}
|
258 |
torben |
348 |
} else {
|
259 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type);
|
260 |
torben |
305 |
}
|
261 |
torben |
978 |
|
262 |
torben |
992 |
Element notifDiv = page.getElementById("station_planlagte_text");
|
263 |
torben |
978 |
if (notifDiv != null) {
|
264 |
|
|
|
265 |
torben |
992 |
Elements tables = notifDiv.getElementsByTag("table");
|
266 |
|
|
for (Element tab : tables) {
|
267 |
torben |
978 |
|
268 |
torben |
992 |
Elements anchors = tab.getElementsByTag("a");
|
269 |
torben |
978 |
if (anchors.size() == 2) {
|
270 |
torben |
992 |
departureBean.notifications.add( anchors.get(1).text() );
|
271 |
torben |
978 |
}
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
}
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
return departureBean;
|
278 |
torben |
305 |
}
|
279 |
|
|
|
280 |
torben |
1330 |
public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception {
|
281 |
|
|
|
282 |
|
|
DepartureBean departureBean = new DepartureBean();
|
283 |
|
|
|
284 |
|
|
|
285 |
|
|
String typeString = getTypeStringWww(traintype);
|
286 |
|
|
String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst";
|
287 |
|
|
|
288 |
|
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
|
289 |
|
|
|
290 |
torben |
1424 |
|
291 |
torben |
1330 |
String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1";
|
292 |
|
|
logger.fine("URI: " + uri);
|
293 |
|
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
|
294 |
|
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
295 |
|
|
|
296 |
|
|
Document page = (Document) breaker.invoke(wrapper);
|
297 |
|
|
|
298 |
|
|
|
299 |
|
|
Element content = page.getElementsByClass("contentDiv").get(0);
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
if (content != null) {
|
303 |
|
|
Elements tableRows = content.child(0).children();
|
304 |
|
|
|
305 |
|
|
|
306 |
|
|
|
307 |
|
|
for (Element currentRow : tableRows) {
|
308 |
|
|
if (currentRow.tagName().equals("br") ) {
|
309 |
|
|
break;
|
310 |
|
|
}
|
311 |
|
|
|
312 |
|
|
|
313 |
torben |
1355 |
String link = currentRow.child(0).attr("href");
|
314 |
|
|
|
315 |
torben |
1335 |
logger.fine( currentRow.text() );
|
316 |
torben |
1355 |
logger.fine("Href: " + link);
|
317 |
torben |
1330 |
|
318 |
|
|
|
319 |
|
|
String parts[] = currentRow.text().split(",");
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
DepartureEntry departure = new DepartureEntry();
|
323 |
torben |
1355 |
|
324 |
|
|
//if we do these things upfront, then we are allowed to use continue statement when row contains no more data
|
325 |
|
|
departure.setType(typeString);
|
326 |
|
|
departureBean.entries.add( departure );
|
327 |
torben |
1330 |
|
328 |
|
|
/*
|
329 |
|
|
http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=111&webprofil=FJRN&mode=rute&strBemaerkning=Afg%E5r+fra+%C5rhus+H+kl%2E07%3A21++&strRefURL=%2FmobilStation%2Easp%3FartikelID%3D5332%26stat%5Fkode%3DAR%26webprofil%3DFJRN%26beskrivelse%3D%25C5rhus%2BH%26mode%3Dankomstafgang%26ankomstafgang%3Dafgang%26gemstation%3D
|
330 |
|
|
*/
|
331 |
|
|
int offset = 0;
|
332 |
|
|
|
333 |
|
|
String time = parts[offset++];
|
334 |
|
|
if (time.equals(""))
|
335 |
|
|
time = "0:00"; //Bane.dk bug work-around
|
336 |
|
|
departure.setTime(time);
|
337 |
|
|
|
338 |
|
|
int updated = 4; //does not exist on mobile
|
339 |
|
|
departure.setUpdated(updated);
|
340 |
|
|
|
341 |
torben |
1366 |
String trainNumber = extractTrainNumberMobile(link);
|
342 |
torben |
1330 |
/*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber
|
343 |
|
|
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/
|
344 |
|
|
departure.setTrainNumber(trainNumber);
|
345 |
|
|
|
346 |
torben |
1334 |
if (traintype == TrainType.STOG) { //if it is stog the next vield is the "Line" code - this should be used somewhere, but skippint ahead for now
|
347 |
torben |
1366 |
String stogLine = parts[offset++].trim();
|
348 |
|
|
departure.setTrainNumber(stogLine + " " + trainNumber);
|
349 |
torben |
1333 |
}
|
350 |
|
|
|
351 |
torben |
1366 |
String destination = parts[offset++].trim();;
|
352 |
torben |
1330 |
departure.setDestination(destination);
|
353 |
|
|
|
354 |
torben |
1332 |
String origin = "-"; // fields.get(4).text(); does not exist on mobile
|
355 |
torben |
1330 |
departure.setOrigin(origin);
|
356 |
|
|
|
357 |
|
|
String location = ""; // fields.get(5).text(); does not exist on mobile
|
358 |
|
|
departure.setLocation(location);
|
359 |
torben |
1355 |
|
360 |
|
|
if (offset == parts.length) {
|
361 |
|
|
continue;
|
362 |
|
|
}
|
363 |
|
|
|
364 |
|
|
if (parts[offset].trim().equalsIgnoreCase("NB!")) {
|
365 |
|
|
offset++;
|
366 |
|
|
}
|
367 |
|
|
|
368 |
|
|
if (offset == parts.length) {
|
369 |
|
|
continue;
|
370 |
|
|
}
|
371 |
torben |
1330 |
|
372 |
torben |
1366 |
String status = parts[offset++].trim();; //fields.get(6).text().trim(); - extract from url
|
373 |
torben |
1330 |
departure.setStatus(status);
|
374 |
|
|
|
375 |
|
|
String note = ""; //extractNote( fields.get(7) ); - extract from url
|
376 |
|
|
departure.setNote(note);
|
377 |
|
|
|
378 |
|
|
}
|
379 |
|
|
} else {
|
380 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + traintype);
|
381 |
|
|
}
|
382 |
|
|
|
383 |
|
|
return departureBean;
|
384 |
|
|
}
|
385 |
torben |
1038 |
|
386 |
|
|
|
387 |
torben |
1330 |
|
388 |
torben |
1040 |
public static String cleanText(String input) {
|
389 |
|
|
//apparently JSoup translates characters on www.bane.dk to 0xA0
|
390 |
torben |
1038 |
return input.replace((char) 0xA0, (char)0x20).trim();
|
391 |
|
|
}
|
392 |
|
|
|
393 |
torben |
1330 |
|
394 |
|
|
// old www site is not available any more
|
395 |
|
|
@Deprecated
|
396 |
torben |
1034 |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
|
397 |
torben |
580 |
|
398 |
torben |
1034 |
DepartureBean departureBean = new DepartureBean();
|
399 |
torben |
580 |
|
400 |
torben |
1034 |
String type = getTypeStringWww(trainType);
|
401 |
|
|
|
402 |
torben |
1045 |
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1");
|
403 |
|
|
|
404 |
torben |
1034 |
|
405 |
|
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
|
406 |
torben |
1048 |
logger.fine("URI:" + uri);
|
407 |
|
|
|
408 |
torben |
1047 |
|
409 |
torben |
1303 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
|
410 |
torben |
580 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
411 |
|
|
|
412 |
torben |
1034 |
Element page = (Element) breaker.invoke(wrapper);
|
413 |
torben |
580 |
|
414 |
torben |
1034 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
|
415 |
|
|
Element table = page.getElementById(tableName);
|
416 |
torben |
580 |
|
417 |
torben |
1046 |
|
418 |
|
|
|
419 |
torben |
1034 |
if (table != null) {
|
420 |
|
|
Elements tableRows = table.getElementsByTag("tr");
|
421 |
torben |
580 |
|
422 |
torben |
1188 |
//boolean passedTidsstreg = false;
|
423 |
|
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
|
424 |
torben |
1046 |
|
425 |
torben |
1034 |
for (Element currentRow : tableRows) {
|
426 |
|
|
String rowClass = currentRow.attr("class");
|
427 |
torben |
1188 |
/*
|
428 |
torben |
1046 |
if (tidsstregExists == true && passedTidsstreg == false) {
|
429 |
|
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
|
430 |
|
|
passedTidsstreg = true;
|
431 |
|
|
} else {
|
432 |
|
|
continue;
|
433 |
|
|
}
|
434 |
torben |
1188 |
}*/
|
435 |
torben |
1046 |
|
436 |
|
|
|
437 |
torben |
1034 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
|
438 |
|
|
Elements fields = currentRow.getElementsByTag("td");
|
439 |
|
|
|
440 |
|
|
DepartureEntry departure = new DepartureEntry();
|
441 |
|
|
|
442 |
|
|
|
443 |
|
|
|
444 |
torben |
1038 |
String time = cleanText( fields.get(0).getAllElements().get(2).text() );
|
445 |
torben |
1034 |
if (time.equals(""))
|
446 |
|
|
time = "0:00"; //Bane.dk bug work-around
|
447 |
|
|
departure.setTime(time);
|
448 |
|
|
|
449 |
|
|
int updated = extractUpdated( fields.get(1) );
|
450 |
|
|
departure.setUpdated(updated);
|
451 |
|
|
|
452 |
torben |
1038 |
String trainNumber = cleanText( fields.get(2).text() );
|
453 |
torben |
1034 |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
|
454 |
torben |
1039 |
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2));
|
455 |
torben |
1034 |
departure.setTrainNumber(trainNumber);
|
456 |
|
|
|
457 |
torben |
1038 |
String destination = cleanText( fields.get(3).text() );
|
458 |
torben |
1034 |
departure.setDestination(destination);
|
459 |
|
|
|
460 |
torben |
1038 |
String origin = cleanText( fields.get(4).text() );
|
461 |
torben |
1034 |
departure.setOrigin(origin);
|
462 |
|
|
|
463 |
torben |
1038 |
String location = cleanText( fields.get(5).text() );
|
464 |
torben |
1034 |
departure.setLocation(location);
|
465 |
|
|
|
466 |
torben |
1038 |
String status = cleanText( fields.get(6).text() );
|
467 |
torben |
1034 |
departure.setStatus(status);
|
468 |
|
|
|
469 |
torben |
1038 |
String note = cleanText( extractNote( fields.get(7) ) );
|
470 |
torben |
1034 |
departure.setNote(note);
|
471 |
|
|
|
472 |
|
|
departure.setType(type);
|
473 |
|
|
|
474 |
torben |
1063 |
departureBean.entries.add(departure);
|
475 |
torben |
1034 |
|
476 |
|
|
|
477 |
torben |
580 |
}
|
478 |
|
|
}
|
479 |
|
|
} else {
|
480 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type);
|
481 |
|
|
}
|
482 |
|
|
|
483 |
torben |
591 |
|
484 |
torben |
1034 |
return departureBean;
|
485 |
|
|
}
|
486 |
|
|
|
487 |
torben |
580 |
|
488 |
torben |
992 |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
|
489 |
torben |
305 |
int updated = -1;
|
490 |
|
|
|
491 |
torben |
992 |
Elements updatedImgs = updatedTd.getElementsByTag("img");
|
492 |
|
|
String updatedStr = updatedImgs.get(0).attr("src");
|
493 |
torben |
305 |
|
494 |
|
|
if (updatedStr != null) {
|
495 |
|
|
for (int i=0; i<updatedStr.length(); i++) {
|
496 |
|
|
char c = updatedStr.charAt(i);
|
497 |
|
|
if ( Character.isDigit(c)) {
|
498 |
|
|
updated = Character.digit(c, 10);
|
499 |
|
|
break;
|
500 |
|
|
}
|
501 |
|
|
}
|
502 |
|
|
}
|
503 |
|
|
return updated;
|
504 |
|
|
}
|
505 |
|
|
|
506 |
torben |
992 |
private String extractNote(Element noteTd) {
|
507 |
|
|
String note = noteTd.text().trim();
|
508 |
torben |
313 |
|
509 |
torben |
992 |
|
510 |
|
|
Elements elems = noteTd.getElementsByClass("bemtype");
|
511 |
torben |
313 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
|
512 |
|
|
note = note.substring(0,note.length() -1 );
|
513 |
|
|
|
514 |
torben |
1038 |
return note.trim();
|
515 |
torben |
313 |
}
|
516 |
|
|
|
517 |
torben |
1039 |
private String extractTrainNumberAzure(Element trainTd) {
|
518 |
torben |
992 |
Element anchorElement = trainTd.getElementsByTag("a").get(0);
|
519 |
|
|
String href = anchorElement.attr("href");
|
520 |
torben |
349 |
|
521 |
torben |
973 |
int pos = href.lastIndexOf('/');
|
522 |
|
|
String number = href.substring(pos+1);
|
523 |
torben |
970 |
|
524 |
torben |
349 |
return number;
|
525 |
|
|
}
|
526 |
|
|
|
527 |
torben |
1366 |
private String extractTrainNumberMobile(String link) {
|
528 |
|
|
Map<String,String> elements = HttpUtil.decodeParams(link);
|
529 |
|
|
|
530 |
|
|
return elements.get("tognummer");
|
531 |
|
|
}
|
532 |
|
|
|
533 |
torben |
1039 |
private String extractTrainNumberWww(Element trainTd) {
|
534 |
|
|
String number = "";
|
535 |
|
|
Element anchorElement = trainTd.getElementsByTag("a").get(0);
|
536 |
|
|
String href = anchorElement.attr("href");
|
537 |
torben |
1366 |
|
538 |
|
|
String argstring = href.split("?")[1];
|
539 |
|
|
Map<String,String> elements = HttpUtil.decodeParams(argstring);
|
540 |
|
|
number = elements.get("TogNr");
|
541 |
torben |
1039 |
|
542 |
torben |
1366 |
|
543 |
|
|
/*String argstring = href.substring( href.indexOf('?') + 1);
|
544 |
torben |
1039 |
String args[] = argstring.split("&");
|
545 |
|
|
for (String arg : args) {
|
546 |
|
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
|
547 |
|
|
|
548 |
|
|
if (pair[0].equalsIgnoreCase("TogNr"))
|
549 |
|
|
number = pair[1];
|
550 |
torben |
1366 |
}*/
|
551 |
|
|
|
552 |
torben |
1039 |
|
553 |
|
|
return number;
|
554 |
|
|
}
|
555 |
torben |
1832 |
|
556 |
|
|
|
557 |
|
|
private void injectTritinfoData(DepartureBean departureBean, StationEntry station) throws Exception {
|
558 |
|
|
String uri = "http://tritinfo.pallas.dk/webtavle?page=stationcontent&staid=" + station.getTritStation();
|
559 |
|
|
logger.fine("URI:" + uri);
|
560 |
|
|
System.out.println("URI:" + uri);
|
561 |
|
|
|
562 |
torben |
1039 |
|
563 |
torben |
1832 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
|
564 |
|
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("tritinfo");
|
565 |
|
|
|
566 |
|
|
Element page = (Element) breaker.invoke(wrapper);
|
567 |
|
|
|
568 |
|
|
Element table = page.getElementsByClass("passages").get(0);
|
569 |
|
|
|
570 |
|
|
Elements trains = table.getElementsByClass("train");
|
571 |
|
|
|
572 |
|
|
for (int i=0; i<trains.size(); i++) {
|
573 |
|
|
Element train = trains.get(i);
|
574 |
|
|
|
575 |
|
|
//String trainType = train.getElementsByClass("trainType").get(0).text();
|
576 |
|
|
String trainNumber = train.getElementsByClass("trainNumber").get(0).text();
|
577 |
|
|
|
578 |
|
|
Elements trackElems = train.getElementsByClass("plannedTrack");
|
579 |
|
|
if (trackElems.size() == 0) {
|
580 |
|
|
trackElems = train.getElementsByClass("expectedTrack");
|
581 |
|
|
}
|
582 |
|
|
String track = trackElems.get(0).text();
|
583 |
|
|
String trackType = train.getElementsByClass("trackType").get(0).text();
|
584 |
|
|
|
585 |
|
|
|
586 |
|
|
String platform = track + " " + trackType;
|
587 |
|
|
|
588 |
|
|
for (DepartureEntry entry : departureBean.entries) {
|
589 |
|
|
String entryTrainId = entry.getTrainNumber().split(" ")[1];
|
590 |
|
|
if ( entryTrainId.equals(trainNumber)) {
|
591 |
|
|
|
592 |
|
|
entry.setPlatform(platform);
|
593 |
|
|
break;
|
594 |
|
|
}
|
595 |
|
|
}
|
596 |
|
|
}
|
597 |
|
|
}
|
598 |
|
|
|
599 |
torben |
1039 |
|
600 |
torben |
305 |
//test
|
601 |
torben |
580 |
/*
|
602 |
torben |
451 |
public static void main(String args[]) throws Exception {
|
603 |
torben |
305 |
DepartureFetcher f = new DepartureFetcher();
|
604 |
torben |
307 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
|
605 |
torben |
305 |
for(DepartureBean d : deps) {
|
606 |
|
|
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
|
607 |
|
|
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
|
608 |
|
|
}
|
609 |
|
|
|
610 |
|
|
System.out.println("--------------------------");
|
611 |
torben |
580 |
}*/
|
612 |
torben |
305 |
}
|