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 |
|
|
26 |
REGIONAL |
REGIONAL |
27 |
} |
} |
28 |
|
|
29 |
|
enum FetchTrainType { |
30 |
|
STOG, |
31 |
|
REGIONAL, |
32 |
|
BOTH |
33 |
|
} |
34 |
|
|
35 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
36 |
|
|
37 |
Map<String, DepartureBean> cache; |
Map<String, DepartureBean> cache; |
50 |
|
|
51 |
|
|
52 |
|
|
53 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
54 |
final String key = "" + stationID + ":" + arrival; |
|
55 |
|
final String key = "" + stationID + ":" + arrival + ":" + type.toString(); |
56 |
|
|
57 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = cache.get(key); |
58 |
|
|
59 |
|
|
60 |
if (departureBean == null) { |
if (departureBean == null) { |
61 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID, arrival, type); |
62 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
63 |
} else { |
} else { |
64 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
68 |
} |
} |
69 |
|
|
70 |
|
|
71 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
72 |
|
|
73 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
74 |
|
|
75 |
StationBean station = stationDao.getById(stationID); |
StationEntry station = stationDao.getById(stationID); |
76 |
|
|
77 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
78 |
|
|
79 |
if (station.getRegional() != null) { |
if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) { |
80 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
81 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
82 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
83 |
} |
} |
84 |
|
|
85 |
if (station.getStrain() != null) { |
if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) { |
86 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
87 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
88 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
89 |
} |
} |
90 |
|
|
91 |
Collections.sort( departureBean.departureEntries ); |
if (departureBean.entries.size() == 0) { |
92 |
|
logger.info("No departures found for station " + stationID); |
93 |
|
} |
94 |
|
|
95 |
|
Collections.sort( departureBean.entries ); |
96 |
|
|
97 |
|
|
98 |
return departureBean; |
return departureBean; |
140 |
|
|
141 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
142 |
|
|
143 |
//logger.info("URI: " + uri); |
logger.fine("URI: " + uri); |
144 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
145 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
146 |
|
|
152 |
if (table != null) { |
if (table != null) { |
153 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
154 |
|
|
155 |
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
156 |
boolean passedTidsstreg = false; |
//boolean passedTidsstreg = false; |
157 |
|
|
158 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
159 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
160 |
|
/* |
161 |
if (tidsstregExists == true && passedTidsstreg == false) { |
if (tidsstregExists == true && passedTidsstreg == false) { |
162 |
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
163 |
passedTidsstreg = true; |
passedTidsstreg = true; |
164 |
} else { |
} else { |
165 |
continue; |
continue; |
166 |
} |
} |
167 |
} |
}*/ |
168 |
|
|
169 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
170 |
|
|
182 |
|
|
183 |
String trainNumber = fields.get(2).text(); |
String trainNumber = fields.get(2).text(); |
184 |
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 |
185 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
186 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
187 |
|
|
188 |
String destination = fields.get(3).text(); |
String destination = fields.get(3).text(); |
202 |
|
|
203 |
departure.setType(typeString); |
departure.setType(typeString); |
204 |
|
|
205 |
departureBean.departureEntries.add( departure ); |
departureBean.entries.add( departure ); |
206 |
} |
} |
207 |
} |
} |
208 |
} else { |
} else { |
227 |
return departureBean; |
return departureBean; |
228 |
} |
} |
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
public static String cleanText(String input) { |
233 |
|
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
234 |
|
return input.replace((char) 0xA0, (char)0x20).trim(); |
235 |
|
} |
236 |
|
|
237 |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
238 |
|
|
239 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
240 |
|
|
241 |
String type = getTypeStringWww(trainType); |
String type = getTypeStringWww(trainType); |
242 |
|
|
243 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
244 |
|
|
245 |
|
|
246 |
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; |
247 |
|
logger.fine("URI:" + uri); |
248 |
|
|
249 |
|
|
250 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
251 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
252 |
|
|
255 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
256 |
Element table = page.getElementById(tableName); |
Element table = page.getElementById(tableName); |
257 |
|
|
258 |
|
|
259 |
|
|
260 |
if (table != null) { |
if (table != null) { |
261 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
262 |
|
|
263 |
|
//boolean passedTidsstreg = false; |
264 |
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
265 |
|
|
266 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
267 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
268 |
|
/* |
269 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
270 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
271 |
|
passedTidsstreg = true; |
272 |
|
} else { |
273 |
|
continue; |
274 |
|
} |
275 |
|
}*/ |
276 |
|
|
277 |
|
|
278 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
279 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
280 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
String time = fields.get(0).getAllElements().get(2).text(); |
String time = cleanText( fields.get(0).getAllElements().get(2).text() ); |
286 |
if (time.equals("")) |
if (time.equals("")) |
287 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
288 |
departure.setTime(time); |
departure.setTime(time); |
290 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
291 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
292 |
|
|
293 |
String trainNumber = fields.get(2).text(); |
String trainNumber = cleanText( fields.get(2).text() ); |
294 |
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 |
295 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
296 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
297 |
|
|
298 |
String destination = fields.get(3).text(); |
String destination = cleanText( fields.get(3).text() ); |
299 |
departure.setDestination(destination); |
departure.setDestination(destination); |
300 |
|
|
301 |
String origin = fields.get(4).text(); |
String origin = cleanText( fields.get(4).text() ); |
302 |
departure.setOrigin(origin); |
departure.setOrigin(origin); |
303 |
|
|
304 |
String location = fields.get(5).text(); |
String location = cleanText( fields.get(5).text() ); |
305 |
departure.setLocation(location); |
departure.setLocation(location); |
306 |
|
|
307 |
String status = fields.get(6).text().trim(); |
String status = cleanText( fields.get(6).text() ); |
308 |
departure.setStatus(status); |
departure.setStatus(status); |
309 |
|
|
310 |
String note = extractNote( fields.get(7) ); |
String note = cleanText( extractNote( fields.get(7) ) ); |
311 |
departure.setNote(note); |
departure.setNote(note); |
312 |
|
|
313 |
departure.setType(type); |
departure.setType(type); |
314 |
|
|
315 |
departureBean.departureEntries.add(departure); |
departureBean.entries.add(departure); |
316 |
|
|
317 |
|
|
318 |
} |
} |
352 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
353 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
354 |
|
|
355 |
return note; |
return note.trim(); |
356 |
} |
} |
357 |
|
|
358 |
private String extractTrainNumber(Element trainTd) { |
private String extractTrainNumberAzure(Element trainTd) { |
359 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
360 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
361 |
|
|
365 |
return number; |
return number; |
366 |
} |
} |
367 |
|
|
368 |
|
private String extractTrainNumberWww(Element trainTd) { |
369 |
|
String number = ""; |
370 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
371 |
|
String href = anchorElement.attr("href"); |
372 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
373 |
|
|
374 |
|
String args[] = argstring.split("&"); |
375 |
|
for (String arg : args) { |
376 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
377 |
|
|
378 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
379 |
|
number = pair[1]; |
380 |
|
} |
381 |
|
|
382 |
|
|
383 |
|
return number; |
384 |
|
} |
385 |
|
|
386 |
|
|
387 |
//test |
//test |
388 |
/* |
/* |
389 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |