11 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
12 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
13 |
|
|
14 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
15 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
16 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
17 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
18 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
19 |
import dk.thoerup.traininfoservice.StationBean; |
import dk.thoerup.genericjavautils.TimeoutMap; |
|
import dk.thoerup.traininfoservice.StationDAO; |
|
20 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
21 |
|
import dk.thoerup.traininfoservice.TraininfoSettings; |
22 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
23 |
|
|
24 |
public class DepartureFetcher { |
public class DepartureFetcher { |
25 |
|
|
28 |
REGIONAL |
REGIONAL |
29 |
} |
} |
30 |
|
|
31 |
|
enum FetchTrainType { |
32 |
|
STOG, |
33 |
|
REGIONAL, |
34 |
|
BOTH |
35 |
|
} |
36 |
|
|
37 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
38 |
|
|
39 |
Map<String, DepartureBean> cache; |
Map<String, DepartureBean> cache; |
40 |
|
|
41 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
42 |
|
|
43 |
private boolean useAzureSite; |
|
44 |
private int replyTimeout; |
private TraininfoSettings settings; |
45 |
|
|
46 |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
public DepartureFetcher(TraininfoSettings settings) { |
47 |
this.replyTimeout = replyTimeout; |
this.settings = settings; |
48 |
useAzureSite = azureSite; |
cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() ); |
|
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
|
49 |
} |
} |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
55 |
final String key = "" + stationID + ":" + arrival; |
|
56 |
|
final String key = "" + stationID + ":" + arrival + ":" + type.toString(); |
57 |
|
|
58 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = cache.get(key); |
59 |
|
|
60 |
|
|
61 |
if (departureBean == null) { |
if (departureBean == null) { |
62 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID, arrival, type); |
63 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
64 |
} else { |
} else { |
65 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
69 |
} |
} |
70 |
|
|
71 |
|
|
72 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
73 |
|
|
74 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
75 |
|
|
76 |
StationBean station = stationDao.getById(stationID); |
StationEntry station = stationDao.getById(stationID); |
77 |
|
|
78 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
79 |
|
|
80 |
if (station.getRegional() != null) { |
if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) { |
81 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
82 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
83 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
84 |
} |
} |
85 |
|
|
86 |
if (station.getStrain() != null) { |
if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) { |
87 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
88 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
89 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
90 |
} |
} |
91 |
|
|
92 |
if (departureBean.departureEntries.size() == 0) { |
if (departureBean.entries.size() == 0) { |
93 |
logger.info("No departures found for station " + stationID); |
logger.info("No departures found for station " + stationID); |
94 |
} |
} |
95 |
|
|
96 |
Collections.sort( departureBean.departureEntries ); |
if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time |
97 |
|
Collections.sort( departureBean.entries ); |
98 |
|
} |
99 |
|
|
100 |
|
|
101 |
return departureBean; |
return departureBean; |
102 |
} |
} |
103 |
|
|
104 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
105 |
if (useAzureSite == true) { |
if ( settings.getUseAzureSite() == true) { |
106 |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
107 |
} else { |
} else { |
108 |
return lookupDeparturesWwwSite(stationcode, type, arrival); |
return lookupDeparturesMobileSite(stationcode, type, arrival); |
109 |
} |
} |
110 |
} |
} |
111 |
|
|
143 |
|
|
144 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
145 |
|
|
146 |
//logger.info("URI: " + uri); |
logger.fine("URI: " + uri); |
147 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
148 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
149 |
|
|
150 |
Document page = (Document) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
155 |
if (table != null) { |
if (table != null) { |
156 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
157 |
|
|
158 |
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
159 |
boolean passedTidsstreg = false; |
//boolean passedTidsstreg = false; |
160 |
|
|
161 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
162 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
163 |
|
/* |
164 |
if (tidsstregExists == true && passedTidsstreg == false) { |
if (tidsstregExists == true && passedTidsstreg == false) { |
165 |
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
166 |
passedTidsstreg = true; |
passedTidsstreg = true; |
167 |
} else { |
} else { |
168 |
continue; |
continue; |
169 |
} |
} |
170 |
} |
}*/ |
171 |
|
|
172 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
173 |
|
|
185 |
|
|
186 |
String trainNumber = fields.get(2).text(); |
String trainNumber = fields.get(2).text(); |
187 |
if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber |
if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber |
188 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
189 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
190 |
|
|
191 |
String destination = fields.get(3).text(); |
String destination = fields.get(3).text(); |
205 |
|
|
206 |
departure.setType(typeString); |
departure.setType(typeString); |
207 |
|
|
208 |
departureBean.departureEntries.add( departure ); |
departureBean.entries.add( departure ); |
209 |
} |
} |
210 |
} |
} |
211 |
} else { |
} else { |
230 |
return departureBean; |
return departureBean; |
231 |
} |
} |
232 |
|
|
233 |
|
public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception { |
234 |
|
|
235 |
|
DepartureBean departureBean = new DepartureBean(); |
236 |
|
|
237 |
|
|
238 |
|
String typeString = getTypeStringWww(traintype); |
239 |
|
String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst"; |
240 |
|
|
241 |
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
242 |
|
|
243 |
|
//String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
244 |
|
String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1"; |
245 |
|
logger.fine("URI: " + uri); |
246 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
247 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
248 |
|
|
249 |
|
Document page = (Document) breaker.invoke(wrapper); |
250 |
|
|
251 |
|
|
252 |
|
Element content = page.getElementsByClass("contentDiv").get(0); |
253 |
|
|
254 |
|
|
255 |
|
if (content != null) { |
256 |
|
Elements tableRows = content.child(0).children(); |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
for (Element currentRow : tableRows) { |
261 |
|
if (currentRow.tagName().equals("br") ) { |
262 |
|
break; |
263 |
|
} |
264 |
|
|
265 |
|
|
266 |
|
String link = currentRow.child(0).attr("href"); |
267 |
|
|
268 |
|
logger.fine( currentRow.text() ); |
269 |
|
logger.fine("Href: " + link); |
270 |
|
|
271 |
|
|
272 |
|
String parts[] = currentRow.text().split(","); |
273 |
|
|
274 |
|
|
275 |
|
DepartureEntry departure = new DepartureEntry(); |
276 |
|
|
277 |
|
//if we do these things upfront, then we are allowed to use continue statement when row contains no more data |
278 |
|
departure.setType(typeString); |
279 |
|
departureBean.entries.add( departure ); |
280 |
|
|
281 |
|
/* |
282 |
|
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 |
283 |
|
*/ |
284 |
|
int offset = 0; |
285 |
|
|
286 |
|
String time = parts[offset++]; |
287 |
|
if (time.equals("")) |
288 |
|
time = "0:00"; //Bane.dk bug work-around |
289 |
|
departure.setTime(time); |
290 |
|
|
291 |
|
int updated = 4; //does not exist on mobile |
292 |
|
departure.setUpdated(updated); |
293 |
|
|
294 |
|
String trainNumber = "-"; //extractTrainNumberAzure(fields.get(2)); |
295 |
|
/*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber |
296 |
|
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/ |
297 |
|
departure.setTrainNumber(trainNumber); |
298 |
|
|
299 |
|
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 |
300 |
|
offset++; |
301 |
|
} |
302 |
|
|
303 |
|
String destination = parts[offset++]; |
304 |
|
departure.setDestination(destination); |
305 |
|
|
306 |
|
String origin = "-"; // fields.get(4).text(); does not exist on mobile |
307 |
|
departure.setOrigin(origin); |
308 |
|
|
309 |
|
String location = ""; // fields.get(5).text(); does not exist on mobile |
310 |
|
departure.setLocation(location); |
311 |
|
|
312 |
|
if (offset == parts.length) { |
313 |
|
continue; |
314 |
|
} |
315 |
|
|
316 |
|
if (parts[offset].trim().equalsIgnoreCase("NB!")) { |
317 |
|
offset++; |
318 |
|
} |
319 |
|
|
320 |
|
if (offset == parts.length) { |
321 |
|
continue; |
322 |
|
} |
323 |
|
|
324 |
|
String status = parts[offset++]; //fields.get(6).text().trim(); - extract from url |
325 |
|
departure.setStatus(status); |
326 |
|
|
327 |
|
String note = ""; //extractNote( fields.get(7) ); - extract from url |
328 |
|
departure.setNote(note); |
329 |
|
|
330 |
|
} |
331 |
|
} else { |
332 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + traintype); |
333 |
|
} |
334 |
|
|
335 |
|
return departureBean; |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
public static String cleanText(String input) { |
341 |
|
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
342 |
|
return input.replace((char) 0xA0, (char)0x20).trim(); |
343 |
|
} |
344 |
|
|
345 |
|
|
346 |
|
// old www site is not available any more |
347 |
|
@Deprecated |
348 |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
349 |
|
|
350 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
351 |
|
|
352 |
String type = getTypeStringWww(trainType); |
String type = getTypeStringWww(trainType); |
353 |
|
|
354 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
355 |
|
|
356 |
|
|
357 |
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
358 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
logger.fine("URI:" + uri); |
359 |
|
|
360 |
|
|
361 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
362 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
363 |
|
|
364 |
Element page = (Element) breaker.invoke(wrapper); |
Element page = (Element) breaker.invoke(wrapper); |
366 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
367 |
Element table = page.getElementById(tableName); |
Element table = page.getElementById(tableName); |
368 |
|
|
369 |
|
|
370 |
|
|
371 |
if (table != null) { |
if (table != null) { |
372 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
373 |
|
|
374 |
|
//boolean passedTidsstreg = false; |
375 |
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
376 |
|
|
377 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
378 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
379 |
|
/* |
380 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
381 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
382 |
|
passedTidsstreg = true; |
383 |
|
} else { |
384 |
|
continue; |
385 |
|
} |
386 |
|
}*/ |
387 |
|
|
388 |
|
|
389 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
390 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
391 |
|
|
393 |
|
|
394 |
|
|
395 |
|
|
396 |
String time = fields.get(0).getAllElements().get(2).text(); |
String time = cleanText( fields.get(0).getAllElements().get(2).text() ); |
397 |
if (time.equals("")) |
if (time.equals("")) |
398 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
399 |
departure.setTime(time); |
departure.setTime(time); |
401 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
402 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
403 |
|
|
404 |
String trainNumber = fields.get(2).text(); |
String trainNumber = cleanText( fields.get(2).text() ); |
405 |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
406 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
407 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
408 |
|
|
409 |
String destination = fields.get(3).text(); |
String destination = cleanText( fields.get(3).text() ); |
410 |
departure.setDestination(destination); |
departure.setDestination(destination); |
411 |
|
|
412 |
String origin = fields.get(4).text(); |
String origin = cleanText( fields.get(4).text() ); |
413 |
departure.setOrigin(origin); |
departure.setOrigin(origin); |
414 |
|
|
415 |
String location = fields.get(5).text(); |
String location = cleanText( fields.get(5).text() ); |
416 |
departure.setLocation(location); |
departure.setLocation(location); |
417 |
|
|
418 |
String status = fields.get(6).text().trim(); |
String status = cleanText( fields.get(6).text() ); |
419 |
departure.setStatus(status); |
departure.setStatus(status); |
420 |
|
|
421 |
String note = extractNote( fields.get(7) ); |
String note = cleanText( extractNote( fields.get(7) ) ); |
422 |
departure.setNote(note); |
departure.setNote(note); |
423 |
|
|
424 |
departure.setType(type); |
departure.setType(type); |
425 |
|
|
426 |
departureBean.departureEntries.add(departure); |
departureBean.entries.add(departure); |
427 |
|
|
428 |
|
|
429 |
} |
} |
463 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
464 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
465 |
|
|
466 |
return note; |
return note.trim(); |
467 |
} |
} |
468 |
|
|
469 |
private String extractTrainNumber(Element trainTd) { |
private String extractTrainNumberAzure(Element trainTd) { |
470 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
471 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
472 |
|
|
476 |
return number; |
return number; |
477 |
} |
} |
478 |
|
|
479 |
|
private String extractTrainNumberWww(Element trainTd) { |
480 |
|
String number = ""; |
481 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
482 |
|
String href = anchorElement.attr("href"); |
483 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
484 |
|
|
485 |
|
String args[] = argstring.split("&"); |
486 |
|
for (String arg : args) { |
487 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
488 |
|
|
489 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
490 |
|
number = pair[1]; |
491 |
|
} |
492 |
|
|
493 |
|
|
494 |
|
return number; |
495 |
|
} |
496 |
|
|
497 |
|
|
498 |
//test |
//test |
499 |
/* |
/* |
500 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |