4 |
import java.net.URL; |
import java.net.URL; |
5 |
import java.net.URLEncoder; |
import java.net.URLEncoder; |
6 |
import java.util.Collections; |
import java.util.Collections; |
7 |
|
import java.util.HashMap; |
8 |
import java.util.Map; |
import java.util.Map; |
9 |
|
import java.util.logging.Level; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
12 |
|
import net.sf.jsr107cache.Cache; |
13 |
|
import net.sf.jsr107cache.CacheException; |
14 |
|
import net.sf.jsr107cache.CacheManager; |
15 |
|
|
16 |
import org.jsoup.nodes.Document; |
import org.jsoup.nodes.Document; |
17 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
18 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
19 |
|
|
20 |
|
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; |
21 |
|
|
22 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
23 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
24 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
25 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
26 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
|
import dk.thoerup.traininfoservice.StationBean; |
|
27 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.traininfoservice.StationDAO; |
28 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
29 |
|
|
33 |
STOG, |
STOG, |
34 |
REGIONAL |
REGIONAL |
35 |
} |
} |
36 |
|
Cache cache; |
37 |
|
|
38 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
|
|
|
|
Map<String, DepartureBean> cache; |
|
39 |
|
|
40 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
41 |
|
|
42 |
private boolean useAzureSite; |
private boolean useAzureSite; |
43 |
private int replyTimeout; |
private int replyTimeout; |
44 |
|
|
45 |
|
@SuppressWarnings("unchecked") |
46 |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
47 |
this.replyTimeout = replyTimeout; |
this.replyTimeout = replyTimeout; |
48 |
useAzureSite = azureSite; |
useAzureSite = azureSite; |
49 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
|
50 |
|
Map props = new HashMap(); |
51 |
|
props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, cacheTimeout); |
52 |
|
|
53 |
|
try { |
54 |
|
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
55 |
|
} catch (CacheException e) { |
56 |
|
logger.log(Level.WARNING, "error creating cache", e); |
57 |
|
} |
58 |
|
|
59 |
} |
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
65 |
final String key = "" + stationID + ":" + arrival; |
final String key = "departure:" + stationID + ":" + arrival; |
66 |
|
|
67 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = (DepartureBean) cache.get(key); |
|
|
|
68 |
|
|
69 |
if (departureBean == null) { |
if (departureBean == null) { |
70 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID,arrival); |
71 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
72 |
|
logger.info("Departure: Cache miss " + key + " !!! "); //remove before production |
73 |
} else { |
} else { |
74 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
75 |
logger.info("Departure: Cache hit " + key); //remove before production |
logger.info("Departure: Cache hit " + key); |
76 |
} |
} |
77 |
|
|
78 |
return departureBean; |
return departureBean; |
79 |
} |
} |
80 |
|
|
83 |
|
|
84 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
85 |
|
|
86 |
StationBean station = stationDao.getById(stationID); |
//TODO: debug code |
87 |
|
StationEntry station = new StationEntry(); // stationDao.getById(stationID); |
88 |
|
station.setId(82); |
89 |
|
station.setName("Test Station"); |
90 |
|
station.setRegional("HS"); |
91 |
|
//TODO: end of debug code |
92 |
|
|
93 |
departureBean.stationName = station.getName(); |
departureBean.stationName = station.getName(); |
94 |
|
|
95 |
if (station.getRegional() != null) { |
if (station.getRegional() != null) { |
96 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
97 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
98 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
99 |
} |
} |
100 |
|
|
101 |
if (station.getStrain() != null) { |
if (station.getStrain() != null) { |
102 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
103 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
104 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
105 |
} |
} |
106 |
|
|
107 |
if (departureBean.departureEntries.size() == 0) { |
if (departureBean.entries.size() == 0) { |
108 |
logger.info("No departures found for station " + stationID); |
logger.info("No departures found for station " + stationID); |
109 |
} |
} |
110 |
|
|
111 |
Collections.sort( departureBean.departureEntries ); |
Collections.sort( departureBean.entries ); |
112 |
|
|
113 |
|
|
114 |
return departureBean; |
return departureBean; |
156 |
|
|
157 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
158 |
|
|
159 |
//logger.info("URI: " + uri); |
logger.fine("URI: " + uri); |
160 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
161 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
162 |
|
|
198 |
|
|
199 |
String trainNumber = fields.get(2).text(); |
String trainNumber = fields.get(2).text(); |
200 |
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 |
201 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
202 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
203 |
|
|
204 |
String destination = fields.get(3).text(); |
String destination = fields.get(3).text(); |
218 |
|
|
219 |
departure.setType(typeString); |
departure.setType(typeString); |
220 |
|
|
221 |
departureBean.departureEntries.add( departure ); |
departureBean.entries.add( departure ); |
222 |
} |
} |
223 |
} |
} |
224 |
} else { |
} else { |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
String cleanText(String input) { |
public static String cleanText(String input) { |
249 |
//apparently JSoup interprets some of space characters on www.bane.dk as 0xA0 |
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
250 |
return input.replace((char) 0xA0, (char)0x20).trim(); |
return input.replace((char) 0xA0, (char)0x20).trim(); |
251 |
} |
} |
252 |
|
|
256 |
|
|
257 |
String type = getTypeStringWww(trainType); |
String type = getTypeStringWww(trainType); |
258 |
|
|
259 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
260 |
|
|
261 |
|
|
262 |
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; |
263 |
|
logger.fine("URI:" + uri); |
264 |
|
|
265 |
|
|
266 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
267 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
268 |
|
|
271 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
272 |
Element table = page.getElementById(tableName); |
Element table = page.getElementById(tableName); |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
if (table != null) { |
if (table != null) { |
277 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
278 |
|
|
279 |
|
boolean passedTidsstreg = false; |
280 |
|
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
281 |
|
|
282 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
283 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
284 |
|
|
285 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
286 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
287 |
|
passedTidsstreg = true; |
288 |
|
} else { |
289 |
|
continue; |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
295 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
296 |
|
|
308 |
|
|
309 |
String trainNumber = cleanText( fields.get(2).text() ); |
String trainNumber = cleanText( fields.get(2).text() ); |
310 |
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 |
311 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
312 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
313 |
|
|
314 |
String destination = cleanText( fields.get(3).text() ); |
String destination = cleanText( fields.get(3).text() ); |
328 |
|
|
329 |
departure.setType(type); |
departure.setType(type); |
330 |
|
|
331 |
departureBean.departureEntries.add(departure); |
departureBean.entries.add(departure); |
332 |
|
|
333 |
|
|
334 |
} |
} |
371 |
return note.trim(); |
return note.trim(); |
372 |
} |
} |
373 |
|
|
374 |
private String extractTrainNumber(Element trainTd) { |
private String extractTrainNumberAzure(Element trainTd) { |
375 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
376 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
377 |
|
|
381 |
return number; |
return number; |
382 |
} |
} |
383 |
|
|
384 |
|
private String extractTrainNumberWww(Element trainTd) { |
385 |
|
String number = ""; |
386 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
387 |
|
String href = anchorElement.attr("href"); |
388 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
389 |
|
|
390 |
|
String args[] = argstring.split("&"); |
391 |
|
for (String arg : args) { |
392 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
393 |
|
|
394 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
395 |
|
number = pair[1]; |
396 |
|
} |
397 |
|
|
398 |
|
|
399 |
|
return number; |
400 |
|
} |
401 |
|
|
402 |
|
|
403 |
//test |
//test |
404 |
/* |
/* |
405 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |