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.Comparator; |
8 |
import java.util.Map; |
import java.util.Map; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
12 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
13 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
14 |
|
|
15 |
|
import dk.thoerup.android.traininfo.common.DepartureBean; |
16 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
17 |
|
import dk.thoerup.android.traininfo.common.StationEntry; |
18 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
19 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
20 |
import dk.thoerup.traininfoservice.StationBean; |
import dk.thoerup.genericjavautils.HttpUtil; |
21 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.genericjavautils.TimeoutMap; |
22 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
23 |
|
import dk.thoerup.traininfoservice.TraininfoSettings; |
24 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
25 |
|
|
26 |
public class DepartureFetcher { |
public class DepartureFetcher { |
27 |
|
|
30 |
REGIONAL |
REGIONAL |
31 |
} |
} |
32 |
|
|
33 |
|
enum FetchTrainType { |
34 |
|
STOG, |
35 |
|
REGIONAL, |
36 |
|
BOTH |
37 |
|
} |
38 |
|
|
39 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
40 |
|
|
41 |
Map<String, DepartureBean> cache; |
Map<String, DepartureBean> cache; |
42 |
|
|
43 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
44 |
|
TritinfoFetcher tritinfo; |
45 |
|
|
46 |
|
|
47 |
|
private TraininfoSettings settings; |
48 |
|
|
49 |
private boolean useTempSite; |
Comparator<DepartureEntry> departureTimeComparator = new Comparator<DepartureEntry>() { |
50 |
|
|
51 |
|
@Override |
52 |
|
public int compare(DepartureEntry arg0, DepartureEntry arg1) { |
53 |
|
String timeStr1 = arg0.getTime().replace(":","").trim(); |
54 |
|
String timeStr2 = arg1.getTime().replace(":","").trim(); |
55 |
|
|
56 |
|
int time1 = 0; |
57 |
|
int time2 = 0; |
58 |
|
|
59 |
|
if (timeStr1.length() > 0) |
60 |
|
time1 = Integer.parseInt(timeStr1); |
61 |
|
|
62 |
|
if (timeStr2.length() > 0) |
63 |
|
time2 = Integer.parseInt(timeStr2); |
64 |
|
|
65 |
|
//work correctly when clock wraps around at midnight |
66 |
|
if (Math.abs(time1-time2) < 1200) { |
67 |
|
if (time1 > time2) |
68 |
|
return 1; |
69 |
|
else |
70 |
|
return -1; |
71 |
|
} else { |
72 |
|
if (time1 < time2) |
73 |
|
return 1; |
74 |
|
else |
75 |
|
return -1; |
76 |
|
|
77 |
|
} |
78 |
|
|
79 |
|
} |
80 |
|
|
81 |
|
}; |
82 |
|
|
83 |
public DepartureFetcher(boolean tempSite, int cacheTimeout) { |
public DepartureFetcher(TraininfoSettings settings) { |
84 |
useTempSite = tempSite; |
this.settings = settings; |
85 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout); |
cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() ); |
86 |
|
|
87 |
|
tritinfo = new TritinfoFetcher(settings); |
88 |
} |
} |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
93 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
94 |
final String key = "" + stationID + ":" + arrival; |
|
95 |
|
final String key = "" + stationID + ":" + arrival + ":" + type.toString(); |
96 |
|
|
97 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = cache.get(key); |
98 |
|
|
99 |
|
|
100 |
if (departureBean == null) { |
if (departureBean == null) { |
101 |
departureBean = lookupDepartures(stationID,arrival); |
departureBean = lookupDepartures(stationID, arrival, type); |
102 |
cache.put(key, departureBean); |
cache.put(key, departureBean); |
103 |
} else { |
} else { |
104 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
108 |
} |
} |
109 |
|
|
110 |
|
|
111 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { |
112 |
|
|
113 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
114 |
|
|
115 |
StationBean station = stationDao.getById(stationID); |
StationEntry station = stationDao.getById(stationID); |
116 |
|
|
117 |
if (station.getRegional() != null) { |
departureBean.stationName = station.getName(); |
118 |
|
|
119 |
|
//TODO: FetchTraintype.Both should be removed some time after 0.9.5 release |
120 |
|
if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) { |
121 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
122 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
123 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
124 |
} |
} |
125 |
|
|
126 |
if (station.getStrain() != null) { |
if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) { |
127 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
128 |
departureBean.departureEntries.addAll( tempBean.departureEntries ); |
departureBean.entries.addAll( tempBean.entries ); |
129 |
departureBean.notifications.addAll(tempBean.notifications); |
departureBean.notifications.addAll(tempBean.notifications); |
130 |
} |
} |
131 |
|
|
132 |
Collections.sort( departureBean.departureEntries ); |
if (departureBean.entries.size() == 0) { |
133 |
|
logger.info("No departures found for station " + stationID); |
134 |
|
} |
135 |
|
|
136 |
|
//TODO: FetchTraintype.Both should be removed some time after 0.9.5 release |
137 |
|
if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time |
138 |
|
Collections.sort( departureBean.entries, departureTimeComparator); |
139 |
|
} |
140 |
|
|
141 |
|
//System.out.println("Trit: " + settings.isTritinfoEnabled() + " " + station.getTritStation() ); |
142 |
|
if ( settings.isTritinfoEnabled() && station.getTritStation() != -1) { |
143 |
|
try { |
144 |
|
tritinfo.injectTritinfoData(departureBean, station); |
145 |
|
} catch (Exception ex) { //det er ikke kritisk at vi får perron numre med |
146 |
|
logger.warning("tritinfo failed with " + ex.getClass().getName() + ": " + ex.getMessage() ); |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
return departureBean; |
return departureBean; |
152 |
} |
} |
153 |
|
|
154 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
155 |
if (useTempSite == false) { |
if ( settings.getBackend() == TraininfoSettings.Backend.Azure) { |
156 |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
157 |
} else { |
} else { |
158 |
//return lookupDeparturesFromTemporarySite(stationcode, type); |
return lookupDeparturesMobileSite(stationcode, type, arrival); |
|
//TODO: find out what to to if they ever put a temp site up on trafikinfo.bane.dk |
|
|
return null; |
|
159 |
} |
} |
160 |
} |
} |
161 |
|
|
162 |
private String getTypeString(TrainType type) { |
private String getTypeStringAzure(TrainType type) { |
163 |
switch (type) { |
switch (type) { |
164 |
case STOG: |
case STOG: |
165 |
return "S-Tog"; |
return "S-Tog"; |
170 |
} |
} |
171 |
} |
} |
172 |
|
|
173 |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
private String getTypeStringWww(TrainType type) { |
174 |
|
switch (type) { |
175 |
|
case STOG: |
176 |
|
return "S2"; |
177 |
|
case REGIONAL: |
178 |
|
return "FJRN"; |
179 |
|
default: |
180 |
|
return ""; //Can not happen |
181 |
|
} |
182 |
|
} |
183 |
|
|
184 |
|
public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
185 |
|
|
186 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
187 |
|
|
188 |
|
|
189 |
String typeString = getTypeString(type); |
String typeString = getTypeStringAzure(type); |
190 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
191 |
|
|
192 |
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
|
//String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
|
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
|
193 |
|
|
194 |
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
195 |
|
|
196 |
|
logger.fine("URI: " + uri); |
197 |
//logger.info("URI: " + uri); |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), 2500); |
|
198 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
199 |
|
|
200 |
Document page = (Document) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
205 |
if (table != null) { |
if (table != null) { |
206 |
Elements tableRows = table.getElementsByTag("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
207 |
|
|
208 |
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
209 |
|
//boolean passedTidsstreg = false; |
210 |
|
|
211 |
for (Element currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
212 |
String rowClass = currentRow.attr("class"); |
String rowClass = currentRow.attr("class"); |
213 |
|
/* |
214 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
215 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
216 |
|
passedTidsstreg = true; |
217 |
|
} else { |
218 |
|
continue; |
219 |
|
} |
220 |
|
}*/ |
221 |
|
|
222 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
223 |
|
|
224 |
Elements fields = currentRow.getElementsByTag("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
225 |
|
|
226 |
DepartureEntry departure = new DepartureEntry(); |
DepartureEntry departure = new DepartureEntry(); |
235 |
|
|
236 |
String trainNumber = fields.get(2).text(); |
String trainNumber = fields.get(2).text(); |
237 |
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 |
238 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
239 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
240 |
|
|
241 |
String destination = fields.get(3).text(); |
String destination = fields.get(3).text(); |
255 |
|
|
256 |
departure.setType(typeString); |
departure.setType(typeString); |
257 |
|
|
258 |
departureBean.departureEntries.add( departure ); |
departureBean.entries.add( departure ); |
259 |
} |
} |
260 |
} |
} |
261 |
} else { |
} else { |
280 |
return departureBean; |
return departureBean; |
281 |
} |
} |
282 |
|
|
283 |
/* |
public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception { |
|
@Deprecated |
|
|
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
|
|
|
|
|
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
|
284 |
|
|
285 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
DepartureBean departureBean = new DepartureBean(); |
286 |
webClient.setTimeout(2500); |
|
|
webClient.setJavaScriptEnabled(false); |
|
287 |
|
|
288 |
|
String typeString = getTypeStringWww(traintype); |
289 |
|
String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst"; |
290 |
|
|
291 |
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
292 |
|
|
|
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
|
293 |
|
|
294 |
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1"; |
295 |
|
logger.fine("URI: " + uri); |
296 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
297 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
298 |
|
|
299 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
300 |
|
|
301 |
|
|
302 |
|
Element content = page.getElementsByClass("contentDiv").get(0); |
303 |
|
|
|
HtmlElement table = page.getElementById("traf_afgang"); |
|
304 |
|
|
305 |
if (table != null) { |
if (content != null) { |
306 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
Elements tableRows = content.child(0).children(); |
307 |
|
|
308 |
boolean isFirst = true; |
|
309 |
|
|
310 |
for (HtmlElement currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
311 |
if (isFirst == true) { //skip table headers |
if (currentRow.tagName().equals("br") ) { |
312 |
isFirst = false; |
break; |
|
continue; |
|
313 |
} |
} |
314 |
|
|
315 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
|
316 |
|
String link = currentRow.child(0).attr("href"); |
317 |
DepartureBean departure = new DepartureBean(); |
|
318 |
|
logger.fine( currentRow.text() ); |
319 |
|
logger.fine("Href: " + link); |
320 |
|
|
321 |
|
|
322 |
String time = fields.get(0).asText().trim(); |
String parts[] = currentRow.text().split(","); |
323 |
|
|
324 |
|
|
325 |
|
DepartureEntry departure = new DepartureEntry(); |
326 |
|
|
327 |
|
//if we do these things upfront, then we are allowed to use continue statement when row contains no more data |
328 |
|
departure.setType(typeString); |
329 |
|
departureBean.entries.add( departure ); |
330 |
|
|
331 |
|
/* |
332 |
|
http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=111&webprofil=FJRN&mode=rute&strBemaerkning=Afg%E5r+fra+%C5rhus+H+kl%2E07%3A21++&strRefURL=%2FmobilStation%2Easp%3FartikelID%3D5332%26stat%5Fkode%3DAR%26webprofil%3DFJRN%26beskrivelse%3D%25C5rhus%2BH%26mode%3Dankomstafgang%26ankomstafgang%3Dafgang%26gemstation%3D |
333 |
|
*/ |
334 |
|
int offset = 0; |
335 |
|
|
336 |
|
String time = parts[offset++]; |
337 |
if (time.equals("")) |
if (time.equals("")) |
338 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
339 |
departure.setTime(time); |
departure.setTime(time); |
340 |
|
|
341 |
|
int updated = 4; //does not exist on mobile |
342 |
|
departure.setUpdated(updated); |
343 |
|
|
344 |
String trainNumber = fields.get(1).asText(); |
String trainNumber = extractTrainNumberMobile(link); |
345 |
|
/*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber |
346 |
|
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/ |
347 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
348 |
|
|
349 |
String destination = fields.get(2).asText(); |
if (traintype == TrainType.STOG) { //if it is stog the next vield is the "Line" code - this should be used somewhere, but skippint ahead for now |
350 |
|
String stogLine = parts[offset++].trim(); |
351 |
|
departure.setTrainNumber(stogLine + " " + trainNumber); |
352 |
|
} |
353 |
|
|
354 |
|
String destination = parts[offset++].trim();; |
355 |
departure.setDestination(destination); |
departure.setDestination(destination); |
356 |
|
|
357 |
String origin = fields.get(3).asText(); |
String origin = "-"; // fields.get(4).text(); does not exist on mobile |
358 |
departure.setOrigin(origin); |
departure.setOrigin(origin); |
359 |
|
|
360 |
String status = fields.get(4).asText(); |
String location = ""; // fields.get(5).text(); does not exist on mobile |
361 |
|
departure.setLocation(location); |
362 |
|
|
363 |
|
if (offset == parts.length) { |
364 |
|
continue; |
365 |
|
} |
366 |
|
|
367 |
|
if (parts[offset].trim().equalsIgnoreCase("NB!")) { |
368 |
|
offset++; |
369 |
|
} |
370 |
|
|
371 |
|
if (offset == parts.length) { |
372 |
|
continue; |
373 |
|
} |
374 |
|
|
375 |
|
String status = parts[offset++].trim();; //fields.get(6).text().trim(); - extract from url |
376 |
departure.setStatus(status); |
departure.setStatus(status); |
377 |
|
|
378 |
String note = fields.get(5).asText(); |
String note = ""; //extractNote( fields.get(7) ); - extract from url |
379 |
departure.setNote(note); |
departure.setNote(note); |
380 |
|
|
|
departureList.add(departure); |
|
381 |
} |
} |
382 |
} else { |
} else { |
383 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + traintype); |
384 |
} |
} |
|
webClient.closeAllWindows(); |
|
385 |
|
|
386 |
|
return departureBean; |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
public static String cleanText(String input) { |
392 |
|
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
393 |
|
return input.replace((char) 0xA0, (char)0x20).trim(); |
394 |
|
} |
395 |
|
|
396 |
|
|
397 |
|
// old www site is not available any more |
398 |
|
@Deprecated |
399 |
|
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
400 |
|
|
401 |
|
DepartureBean departureBean = new DepartureBean(); |
402 |
|
|
403 |
|
String type = getTypeStringWww(trainType); |
404 |
|
|
405 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
406 |
|
|
407 |
|
|
408 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
409 |
|
logger.fine("URI:" + uri); |
410 |
|
|
|
return departureList; |
|
|
}*/ |
|
411 |
|
|
412 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); |
413 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
414 |
|
|
415 |
|
Element page = (Element) breaker.invoke(wrapper); |
416 |
|
|
417 |
|
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
418 |
|
Element table = page.getElementById(tableName); |
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
if (table != null) { |
423 |
|
Elements tableRows = table.getElementsByTag("tr"); |
424 |
|
|
425 |
|
//boolean passedTidsstreg = false; |
426 |
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
427 |
|
|
428 |
|
for (Element currentRow : tableRows) { |
429 |
|
String rowClass = currentRow.attr("class"); |
430 |
|
/* |
431 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
432 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
433 |
|
passedTidsstreg = true; |
434 |
|
} else { |
435 |
|
continue; |
436 |
|
} |
437 |
|
}*/ |
438 |
|
|
439 |
|
|
440 |
|
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
441 |
|
Elements fields = currentRow.getElementsByTag("td"); |
442 |
|
|
443 |
|
DepartureEntry departure = new DepartureEntry(); |
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
String time = cleanText( fields.get(0).getAllElements().get(2).text() ); |
448 |
|
if (time.equals("")) |
449 |
|
time = "0:00"; //Bane.dk bug work-around |
450 |
|
departure.setTime(time); |
451 |
|
|
452 |
|
int updated = extractUpdated( fields.get(1) ); |
453 |
|
departure.setUpdated(updated); |
454 |
|
|
455 |
|
String trainNumber = cleanText( fields.get(2).text() ); |
456 |
|
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
457 |
|
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
458 |
|
departure.setTrainNumber(trainNumber); |
459 |
|
|
460 |
|
String destination = cleanText( fields.get(3).text() ); |
461 |
|
departure.setDestination(destination); |
462 |
|
|
463 |
|
String origin = cleanText( fields.get(4).text() ); |
464 |
|
departure.setOrigin(origin); |
465 |
|
|
466 |
|
String location = cleanText( fields.get(5).text() ); |
467 |
|
departure.setLocation(location); |
468 |
|
|
469 |
|
String status = cleanText( fields.get(6).text() ); |
470 |
|
departure.setStatus(status); |
471 |
|
|
472 |
|
String note = cleanText( extractNote( fields.get(7) ) ); |
473 |
|
departure.setNote(note); |
474 |
|
|
475 |
|
departure.setType(type); |
476 |
|
|
477 |
|
departureBean.entries.add(departure); |
478 |
|
|
479 |
|
|
480 |
|
} |
481 |
|
} |
482 |
|
} else { |
483 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
484 |
|
} |
485 |
|
|
486 |
|
|
487 |
|
return departureBean; |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
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" |
492 |
int updated = -1; |
int updated = -1; |
514 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
515 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
516 |
|
|
517 |
return note; |
return note.trim(); |
518 |
} |
} |
519 |
|
|
520 |
private String extractTrainNumber(Element trainTd) { |
private String extractTrainNumberAzure(Element trainTd) { |
521 |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
522 |
String href = anchorElement.attr("href"); |
String href = anchorElement.attr("href"); |
523 |
|
|
524 |
int pos = href.lastIndexOf('/'); |
int pos = href.lastIndexOf('='); |
525 |
String number = href.substring(pos+1); |
String number = href.substring(pos+1); |
526 |
|
|
527 |
return number; |
return number; |
528 |
} |
} |
529 |
|
|
530 |
|
private String extractTrainNumberMobile(String link) { |
531 |
|
Map<String,String> elements = HttpUtil.decodeParams(link); |
532 |
|
|
533 |
|
return elements.get("tognummer"); |
534 |
|
} |
535 |
|
|
536 |
|
private String extractTrainNumberWww(Element trainTd) { |
537 |
|
String number = ""; |
538 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
539 |
|
String href = anchorElement.attr("href"); |
540 |
|
|
541 |
|
String argstring = href.split("?")[1]; |
542 |
|
Map<String,String> elements = HttpUtil.decodeParams(argstring); |
543 |
|
number = elements.get("TogNr"); |
544 |
|
|
545 |
|
|
546 |
|
/*String argstring = href.substring( href.indexOf('?') + 1); |
547 |
|
String args[] = argstring.split("&"); |
548 |
|
for (String arg : args) { |
549 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
550 |
|
|
551 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
552 |
|
number = pair[1]; |
553 |
|
}*/ |
554 |
|
|
555 |
|
|
556 |
|
return number; |
557 |
|
} |
558 |
|
|
559 |
|
|
560 |
|
|
561 |
//test |
//test |
562 |
/* |
/* |
563 |
public static void main(String args[]) throws Exception { |
public static void main(String args[]) throws Exception { |