79 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
80 |
} |
} |
81 |
|
|
82 |
|
if (departureBean.departureEntries.size() == 0) { |
83 |
|
logger.info("No departures found for station " + stationID); |
84 |
|
} |
85 |
|
|
86 |
Collections.sort( departureBean.departureEntries ); |
Collections.sort( departureBean.departureEntries ); |
87 |
|
|
88 |
|
|
131 |
|
|
132 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
133 |
|
|
134 |
//logger.info("URI: " + uri); |
logger.fine("URI: " + uri); |
135 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
136 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
137 |
|
|
173 |
|
|
174 |
String trainNumber = fields.get(2).text(); |
String trainNumber = fields.get(2).text(); |
175 |
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 |
176 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
177 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
178 |
|
|
179 |
String destination = fields.get(3).text(); |
String destination = fields.get(3).text(); |
218 |
return departureBean; |
return departureBean; |
219 |
} |
} |
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
public static String cleanText(String input) { |
224 |
|
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
225 |
|
return input.replace((char) 0xA0, (char)0x20).trim(); |
226 |
|
} |
227 |
|
|
228 |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
229 |
|
|
230 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
231 |
|
|
232 |
String type = getTypeStringWww(trainType); |
String type = getTypeStringWww(trainType); |
233 |
|
|
234 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
235 |
|
|
236 |
|
|
237 |
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; |
238 |
|
logger.fine("URI:" + uri); |
239 |
|
|
240 |
|
|
241 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
242 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
243 |
|
|
246 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
247 |
Element table = page.getElementById(tableName); |
Element table = page.getElementById(tableName); |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
if (table != null) { |
if (table != null) { |
252 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
253 |
|
|
254 |
|
boolean passedTidsstreg = false; |
255 |
|
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
256 |
|
|
257 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
258 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
259 |
|
|
260 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
261 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
262 |
|
passedTidsstreg = true; |
263 |
|
} else { |
264 |
|
continue; |
265 |
|
} |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
270 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
271 |
|
|
273 |
|
|
274 |
|
|
275 |
|
|
276 |
String time = fields.get(0).getAllElements().get(2).text(); |
String time = cleanText( fields.get(0).getAllElements().get(2).text() ); |
277 |
if (time.equals("")) |
if (time.equals("")) |
278 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
279 |
departure.setTime(time); |
departure.setTime(time); |
|
logger.info("Time: " + time);// TODO: remove |
|
280 |
|
|
281 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
282 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
|
logger.info("Updated: " + updated);// TODO: remove |
|
283 |
|
|
284 |
String trainNumber = fields.get(2).text(); |
String trainNumber = cleanText( fields.get(2).text() ); |
285 |
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 |
286 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
287 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
|
logger.info("TrainNumber: " + trainNumber);// TODO: remove |
|
288 |
|
|
289 |
String destination = fields.get(3).text(); |
String destination = cleanText( fields.get(3).text() ); |
290 |
departure.setDestination(destination); |
departure.setDestination(destination); |
291 |
|
|
292 |
String origin = fields.get(4).text(); |
String origin = cleanText( fields.get(4).text() ); |
293 |
departure.setOrigin(origin); |
departure.setOrigin(origin); |
294 |
|
|
295 |
String location = fields.get(5).text(); |
String location = cleanText( fields.get(5).text() ); |
296 |
departure.setLocation(location); |
departure.setLocation(location); |
297 |
|
|
298 |
String status = fields.get(6).text().trim(); |
String status = cleanText( fields.get(6).text() ); |
299 |
departure.setStatus(status); |
departure.setStatus(status); |
300 |
|
|
301 |
String note = extractNote( fields.get(7) ); |
String note = cleanText( extractNote( fields.get(7) ) ); |
302 |
departure.setNote(note); |
departure.setNote(note); |
303 |
|
|
304 |
departure.setType(type); |
departure.setType(type); |
343 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
344 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
345 |
|
|
346 |
return note; |
return note.trim(); |
347 |
} |
} |
348 |
|
|
349 |
private String extractTrainNumber(Element trainTd) { |
private String extractTrainNumberAzure(Element trainTd) { |
350 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
351 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
352 |
|
|
356 |
return number; |
return number; |
357 |
} |
} |
358 |
|
|
359 |
|
private String extractTrainNumberWww(Element trainTd) { |
360 |
|
String number = ""; |
361 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
362 |
|
String href = anchorElement.attr("href"); |
363 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
364 |
|
|
365 |
|
String args[] = argstring.split("&"); |
366 |
|
for (String arg : args) { |
367 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
368 |
|
|
369 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
370 |
|
number = pair[1]; |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
|
return number; |
375 |
|
} |
376 |
|
|
377 |
|
|
378 |
//test |
//test |
379 |
/* |
/* |
380 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |