2 |
|
|
3 |
|
|
4 |
import java.net.URL; |
import java.net.URL; |
5 |
|
import java.net.URLEncoder; |
6 |
import java.util.Collections; |
import java.util.Collections; |
7 |
import java.util.Map; |
import java.util.Map; |
8 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
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; |
|
|
import dk.thoerup.traininfoservice.StationDAO; |
|
19 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
20 |
|
import dk.thoerup.traininfoservice.TraininfoSettings; |
21 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
22 |
|
|
23 |
public class DepartureFetcher { |
public class DepartureFetcher { |
24 |
|
|
27 |
REGIONAL |
REGIONAL |
28 |
} |
} |
29 |
|
|
30 |
|
enum FetchTrainType { |
31 |
|
STOG, |
32 |
|
REGIONAL, |
33 |
|
BOTH |
34 |
|
} |
35 |
|
|
36 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
37 |
|
|
38 |
Map<String, DepartureBean> cache; |
Map<String, DepartureBean> cache; |
39 |
|
|
40 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
41 |
|
|
42 |
private boolean useTempSite; |
|
43 |
|
private TraininfoSettings settings; |
44 |
|
|
45 |
public DepartureFetcher(boolean tempSite, int cacheTimeout) { |
public DepartureFetcher(TraininfoSettings settings) { |
46 |
useTempSite = tempSite; |
this.settings = settings; |
47 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() ); |
48 |
} |
} |
49 |
|
|
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 |
if (station.getRegional() != null) { |
departureBean.stationName = station.getName(); |
78 |
|
|
79 |
|
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 |
|
if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time |
96 |
|
Collections.sort( departureBean.entries ); |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
return departureBean; |
return departureBean; |
101 |
} |
} |
102 |
|
|
103 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
104 |
if (useTempSite == false) { |
if ( settings.getUseAzureSite() == true) { |
105 |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
106 |
} else { |
} else { |
107 |
//return lookupDeparturesFromTemporarySite(stationcode, type); |
return lookupDeparturesWwwSite(stationcode, type, arrival); |
|
//TODO: find out what to to if they ever put a temp site up on trafikinfo.bane.dk |
|
|
return null; |
|
108 |
} |
} |
109 |
} |
} |
110 |
|
|
111 |
private String getTypeString(TrainType type) { |
private String getTypeStringAzure(TrainType type) { |
112 |
switch (type) { |
switch (type) { |
113 |
case STOG: |
case STOG: |
114 |
return "S-Tog"; |
return "S-Tog"; |
119 |
} |
} |
120 |
} |
} |
121 |
|
|
122 |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
private String getTypeStringWww(TrainType type) { |
123 |
|
switch (type) { |
124 |
|
case STOG: |
125 |
|
return "S2"; |
126 |
|
case REGIONAL: |
127 |
|
return "FJRN"; |
128 |
|
default: |
129 |
|
return ""; //Can not happen |
130 |
|
} |
131 |
|
} |
132 |
|
|
133 |
|
public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
134 |
|
|
135 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
136 |
|
|
137 |
|
|
138 |
String typeString = getTypeString(type); |
String typeString = getTypeStringAzure(type); |
139 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
140 |
|
|
141 |
//String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
|
142 |
|
|
143 |
//logger.info("URI: " + uri); |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
144 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), 2500); |
|
145 |
|
logger.fine("URI: " + uri); |
146 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
147 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
148 |
|
|
149 |
Document page = (Document) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
154 |
if (table != null) { |
if (table != null) { |
155 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
156 |
|
|
157 |
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
158 |
|
//boolean passedTidsstreg = false; |
159 |
|
|
160 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
161 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
162 |
|
/* |
163 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
164 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
165 |
|
passedTidsstreg = true; |
166 |
|
} else { |
167 |
|
continue; |
168 |
|
} |
169 |
|
}*/ |
170 |
|
|
171 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
172 |
|
|
173 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
174 |
|
|
175 |
DepartureEntry departure = new DepartureEntry(); |
DepartureEntry departure = new DepartureEntry(); |
184 |
|
|
185 |
String trainNumber = fields.get(2).text(); |
String trainNumber = fields.get(2).text(); |
186 |
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 |
187 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
188 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
189 |
|
|
190 |
String destination = fields.get(3).text(); |
String destination = fields.get(3).text(); |
204 |
|
|
205 |
departure.setType(typeString); |
departure.setType(typeString); |
206 |
|
|
207 |
departureBean.departureEntries.add( departure ); |
departureBean.entries.add( departure ); |
208 |
} |
} |
209 |
} |
} |
210 |
} else { |
} else { |
229 |
return departureBean; |
return departureBean; |
230 |
} |
} |
231 |
|
|
232 |
/* |
|
233 |
@Deprecated |
|
234 |
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
public static String cleanText(String input) { |
235 |
|
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
236 |
|
return input.replace((char) 0xA0, (char)0x20).trim(); |
237 |
|
} |
238 |
|
|
239 |
|
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
240 |
|
|
241 |
|
DepartureBean departureBean = new DepartureBean(); |
242 |
|
|
243 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
String type = getTypeStringWww(trainType); |
244 |
|
|
245 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
246 |
webClient.setTimeout(2500); |
|
247 |
webClient.setJavaScriptEnabled(false); |
|
248 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
249 |
|
logger.fine("URI:" + uri); |
250 |
|
|
251 |
|
|
252 |
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
|
|
|
|
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
|
253 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
254 |
|
|
255 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Element page = (Element) breaker.invoke(wrapper); |
256 |
|
|
257 |
|
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
258 |
|
Element table = page.getElementById(tableName); |
259 |
|
|
260 |
HtmlElement table = page.getElementById("traf_afgang"); |
|
261 |
|
|
262 |
if (table != null) { |
if (table != null) { |
263 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
264 |
|
|
265 |
boolean isFirst = true; |
//boolean passedTidsstreg = false; |
266 |
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
267 |
|
|
268 |
for (HtmlElement currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
269 |
if (isFirst == true) { //skip table headers |
String rowClass = currentRow.attr("class"); |
270 |
isFirst = false; |
/* |
271 |
continue; |
if (tidsstregExists == true && passedTidsstreg == false) { |
272 |
} |
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
273 |
|
passedTidsstreg = true; |
274 |
|
} else { |
275 |
|
continue; |
276 |
|
} |
277 |
|
}*/ |
278 |
|
|
279 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
|
280 |
|
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
281 |
DepartureBean departure = new DepartureBean(); |
Elements fields = currentRow.getElementsByTag("td"); |
282 |
|
|
283 |
String time = fields.get(0).asText().trim(); |
DepartureEntry departure = new DepartureEntry(); |
284 |
|
|
|
if (time.equals("")) |
|
|
time = "0:00"; //Bane.dk bug work-around |
|
|
departure.setTime(time); |
|
|
|
|
|
|
|
|
String trainNumber = fields.get(1).asText(); |
|
|
departure.setTrainNumber(trainNumber); |
|
|
|
|
|
String destination = fields.get(2).asText(); |
|
|
departure.setDestination(destination); |
|
|
|
|
|
String origin = fields.get(3).asText(); |
|
|
departure.setOrigin(origin); |
|
|
|
|
|
String status = fields.get(4).asText(); |
|
|
departure.setStatus(status); |
|
|
|
|
|
String note = fields.get(5).asText(); |
|
|
departure.setNote(note); |
|
285 |
|
|
286 |
departureList.add(departure); |
|
287 |
|
String time = cleanText( fields.get(0).getAllElements().get(2).text() ); |
288 |
|
if (time.equals("")) |
289 |
|
time = "0:00"; //Bane.dk bug work-around |
290 |
|
departure.setTime(time); |
291 |
|
|
292 |
|
int updated = extractUpdated( fields.get(1) ); |
293 |
|
departure.setUpdated(updated); |
294 |
|
|
295 |
|
String trainNumber = cleanText( fields.get(2).text() ); |
296 |
|
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
297 |
|
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
298 |
|
departure.setTrainNumber(trainNumber); |
299 |
|
|
300 |
|
String destination = cleanText( fields.get(3).text() ); |
301 |
|
departure.setDestination(destination); |
302 |
|
|
303 |
|
String origin = cleanText( fields.get(4).text() ); |
304 |
|
departure.setOrigin(origin); |
305 |
|
|
306 |
|
String location = cleanText( fields.get(5).text() ); |
307 |
|
departure.setLocation(location); |
308 |
|
|
309 |
|
String status = cleanText( fields.get(6).text() ); |
310 |
|
departure.setStatus(status); |
311 |
|
|
312 |
|
String note = cleanText( extractNote( fields.get(7) ) ); |
313 |
|
departure.setNote(note); |
314 |
|
|
315 |
|
departure.setType(type); |
316 |
|
|
317 |
|
departureBean.entries.add(departure); |
318 |
|
|
319 |
|
|
320 |
|
} |
321 |
} |
} |
322 |
} else { |
} else { |
323 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
324 |
} |
} |
|
webClient.closeAllWindows(); |
|
325 |
|
|
326 |
|
|
327 |
return departureList; |
return departureBean; |
328 |
}*/ |
} |
329 |
|
|
330 |
|
|
331 |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
332 |
int updated = -1; |
int updated = -1; |
354 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
355 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
356 |
|
|
357 |
return note; |
return note.trim(); |
358 |
} |
} |
359 |
|
|
360 |
private String extractTrainNumber(Element trainTd) { |
private String extractTrainNumberAzure(Element trainTd) { |
361 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
362 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
363 |
|
|
367 |
return number; |
return number; |
368 |
} |
} |
369 |
|
|
370 |
|
private String extractTrainNumberWww(Element trainTd) { |
371 |
|
String number = ""; |
372 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
373 |
|
String href = anchorElement.attr("href"); |
374 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
375 |
|
|
376 |
|
String args[] = argstring.split("&"); |
377 |
|
for (String arg : args) { |
378 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
379 |
|
|
380 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
381 |
|
number = pair[1]; |
382 |
|
} |
383 |
|
|
384 |
|
|
385 |
|
return number; |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
//test |
//test |
390 |
/* |
/* |
391 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |