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; |
|
import dk.thoerup.traininfoservice.StationBean; |
|
19 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.traininfoservice.StationDAO; |
20 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
21 |
|
|
65 |
|
|
66 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
67 |
|
|
68 |
StationBean station = stationDao.getById(stationID); |
StationEntry station = new StationEntry(); // stationDao.getById(stationID); |
69 |
|
station.setId(82); |
70 |
|
station.setName("Test Station"); |
71 |
|
station.setRegional("HS"); |
72 |
|
|
73 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
74 |
|
|
75 |
if (station.getRegional() != null) { |
if (station.getRegional() != null) { |
76 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
77 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
78 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
79 |
} |
} |
80 |
|
|
81 |
if (station.getStrain() != null) { |
if (station.getStrain() != null) { |
82 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
83 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
84 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
85 |
} |
} |
86 |
|
|
87 |
Collections.sort( departureBean.departureEntries ); |
if (departureBean.entries.size() == 0) { |
88 |
|
logger.info("No departures found for station " + stationID); |
89 |
|
} |
90 |
|
|
91 |
|
Collections.sort( departureBean.entries ); |
92 |
|
|
93 |
|
|
94 |
return departureBean; |
return departureBean; |
136 |
|
|
137 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
138 |
|
|
139 |
//logger.info("URI: " + uri); |
logger.fine("URI: " + uri); |
140 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
141 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
142 |
|
|
178 |
|
|
179 |
String trainNumber = fields.get(2).text(); |
String trainNumber = fields.get(2).text(); |
180 |
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 |
181 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
182 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
183 |
|
|
184 |
String destination = fields.get(3).text(); |
String destination = fields.get(3).text(); |
198 |
|
|
199 |
departure.setType(typeString); |
departure.setType(typeString); |
200 |
|
|
201 |
departureBean.departureEntries.add( departure ); |
departureBean.entries.add( departure ); |
202 |
} |
} |
203 |
} |
} |
204 |
} else { |
} else { |
223 |
return departureBean; |
return departureBean; |
224 |
} |
} |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
public static String cleanText(String input) { |
229 |
|
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
230 |
|
return input.replace((char) 0xA0, (char)0x20).trim(); |
231 |
|
} |
232 |
|
|
233 |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
234 |
|
|
235 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
236 |
|
|
237 |
String type = getTypeStringWww(trainType); |
String type = getTypeStringWww(trainType); |
238 |
|
|
239 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
240 |
|
|
241 |
|
|
242 |
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; |
243 |
|
logger.fine("URI:" + uri); |
244 |
|
|
245 |
|
|
246 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
247 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
248 |
|
|
251 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
252 |
Element table = page.getElementById(tableName); |
Element table = page.getElementById(tableName); |
253 |
|
|
254 |
|
|
255 |
|
|
256 |
if (table != null) { |
if (table != null) { |
257 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
258 |
|
|
259 |
|
boolean passedTidsstreg = false; |
260 |
|
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
261 |
|
|
262 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
263 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
264 |
|
|
265 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
266 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
267 |
|
passedTidsstreg = true; |
268 |
|
} else { |
269 |
|
continue; |
270 |
|
} |
271 |
|
} |
272 |
|
|
273 |
|
|
274 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
275 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
276 |
|
|
278 |
|
|
279 |
|
|
280 |
|
|
281 |
String time = fields.get(0).getAllElements().get(2).text(); |
String time = cleanText( fields.get(0).getAllElements().get(2).text() ); |
282 |
if (time.equals("")) |
if (time.equals("")) |
283 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
284 |
departure.setTime(time); |
departure.setTime(time); |
286 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
287 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
288 |
|
|
289 |
String trainNumber = fields.get(2).text(); |
String trainNumber = cleanText( fields.get(2).text() ); |
290 |
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 |
291 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
292 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
293 |
|
|
294 |
String destination = fields.get(3).text(); |
String destination = cleanText( fields.get(3).text() ); |
295 |
departure.setDestination(destination); |
departure.setDestination(destination); |
296 |
|
|
297 |
String origin = fields.get(4).text(); |
String origin = cleanText( fields.get(4).text() ); |
298 |
departure.setOrigin(origin); |
departure.setOrigin(origin); |
299 |
|
|
300 |
String location = fields.get(5).text(); |
String location = cleanText( fields.get(5).text() ); |
301 |
departure.setLocation(location); |
departure.setLocation(location); |
302 |
|
|
303 |
String status = fields.get(6).text().trim(); |
String status = cleanText( fields.get(6).text() ); |
304 |
departure.setStatus(status); |
departure.setStatus(status); |
305 |
|
|
306 |
String note = extractNote( fields.get(7) ); |
String note = cleanText( extractNote( fields.get(7) ) ); |
307 |
departure.setNote(note); |
departure.setNote(note); |
308 |
|
|
309 |
departure.setType(type); |
departure.setType(type); |
310 |
|
|
311 |
departureBean.departureEntries.add(departure); |
departureBean.entries.add(departure); |
312 |
|
|
313 |
|
|
314 |
} |
} |
348 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
349 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
350 |
|
|
351 |
return note; |
return note.trim(); |
352 |
} |
} |
353 |
|
|
354 |
private String extractTrainNumber(Element trainTd) { |
private String extractTrainNumberAzure(Element trainTd) { |
355 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
356 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
357 |
|
|
361 |
return number; |
return number; |
362 |
} |
} |
363 |
|
|
364 |
|
private String extractTrainNumberWww(Element trainTd) { |
365 |
|
String number = ""; |
366 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
367 |
|
String href = anchorElement.attr("href"); |
368 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
369 |
|
|
370 |
|
String args[] = argstring.split("&"); |
371 |
|
for (String arg : args) { |
372 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
373 |
|
|
374 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
375 |
|
number = pair[1]; |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
|
return number; |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
//test |
//test |
384 |
/* |
/* |
385 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |