/[projects]/android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java
ViewVC logotype

Annotation of /android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1424 - (hide annotations) (download)
Mon May 2 17:19:30 2011 UTC (13 years, 1 month ago) by torben
File size: 17523 byte(s)
//TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
1 torben 305 package dk.thoerup.traininfoservice.banedk;
2    
3 torben 978
4 torben 992 import java.net.URL;
5 torben 994 import java.net.URLEncoder;
6 torben 307 import java.util.Collections;
7 torben 428 import java.util.Map;
8 torben 348 import java.util.logging.Logger;
9 torben 305
10 torben 992 import org.jsoup.nodes.Document;
11     import org.jsoup.nodes.Element;
12     import org.jsoup.select.Elements;
13 torben 305
14 torben 1061 import dk.thoerup.android.traininfo.common.DepartureBean;
15     import dk.thoerup.android.traininfo.common.DepartureEntry;
16 torben 1409 import dk.thoerup.android.traininfo.common.StationEntry;
17 torben 468 import dk.thoerup.circuitbreaker.CircuitBreaker;
18     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
19 torben 1366 import dk.thoerup.genericjavautils.HttpUtil;
20 torben 1355 import dk.thoerup.genericjavautils.TimeoutMap;
21 torben 711 import dk.thoerup.traininfoservice.Statistics;
22 torben 1305 import dk.thoerup.traininfoservice.TraininfoSettings;
23 torben 1255 import dk.thoerup.traininfoservice.db.StationDAO;
24 torben 307
25 torben 305 public class DepartureFetcher {
26 torben 348
27 torben 972 enum TrainType{
28     STOG,
29     REGIONAL
30     }
31    
32 torben 1248 enum FetchTrainType {
33     STOG,
34     REGIONAL,
35     BOTH
36     }
37    
38 torben 348 Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
39 torben 387
40 torben 978 Map<String, DepartureBean> cache;
41 torben 387
42 torben 588 StationDAO stationDao = new StationDAO();
43    
44 torben 1303
45     private TraininfoSettings settings;
46 torben 580
47 torben 1303 public DepartureFetcher(TraininfoSettings settings) {
48     this.settings = settings;
49     cache = new TimeoutMap<String,DepartureBean>( settings.getCacheTimeout() );
50 torben 580 }
51    
52    
53 torben 307
54 torben 387
55 torben 1248 public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception {
56 torben 980
57 torben 1248 final String key = "" + stationID + ":" + arrival + ":" + type.toString();
58    
59 torben 978 DepartureBean departureBean = cache.get(key);
60 torben 308
61 torben 387
62 torben 978 if (departureBean == null) {
63 torben 1248 departureBean = lookupDepartures(stationID, arrival, type);
64 torben 978 cache.put(key, departureBean);
65 torben 387 } else {
66 torben 711 Statistics.getInstance().incrementDepartureCacheHits();
67 torben 829 logger.info("Departure: Cache hit " + key); //remove before production
68 torben 387 }
69 torben 980 return departureBean;
70 torben 387 }
71    
72    
73 torben 1248 public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception {
74 torben 307
75 torben 978 DepartureBean departureBean = new DepartureBean();
76    
77 torben 1060 StationEntry station = stationDao.getById(stationID);
78 torben 307
79 torben 1021 departureBean.stationName = station.getName();
80 torben 1252
81 torben 1424 //TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
82 torben 1248 if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) {
83 torben 978 DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
84 torben 1063 departureBean.entries.addAll( tempBean.entries );
85 torben 978 departureBean.notifications.addAll(tempBean.notifications);
86 torben 307 }
87    
88 torben 1248 if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) {
89 torben 978 DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
90 torben 1063 departureBean.entries.addAll( tempBean.entries );
91 torben 978 departureBean.notifications.addAll(tempBean.notifications);
92 torben 588 }
93    
94 torben 1063 if (departureBean.entries.size() == 0) {
95 torben 1037 logger.info("No departures found for station " + stationID);
96     }
97    
98 torben 1424 //TODO: FetchTraintype.Both should be removed some time after 0.9.5 release
99 torben 1252 if (type == FetchTrainType.BOTH) { //if we have both S-tog and regional order by departure/arrival time
100     Collections.sort( departureBean.entries );
101     }
102 torben 588
103    
104 torben 978 return departureBean;
105 torben 305 }
106    
107 torben 978 public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
108 torben 1372 if ( settings.getBackend() == TraininfoSettings.Backend.Azure) {
109 torben 1034 return lookupDeparturesAzureSite(stationcode, type, arrival);
110     } else {
111 torben 1330 return lookupDeparturesMobileSite(stationcode, type, arrival);
112 torben 580 }
113     }
114    
115 torben 1034 private String getTypeStringAzure(TrainType type) {
116 torben 972 switch (type) {
117     case STOG:
118     return "S-Tog";
119     case REGIONAL:
120     return "Fjerntog";
121     default:
122     return ""; //Can not happen
123     }
124     }
125    
126 torben 1034 private String getTypeStringWww(TrainType type) {
127     switch (type) {
128     case STOG:
129     return "S2";
130     case REGIONAL:
131     return "FJRN";
132     default:
133     return ""; //Can not happen
134     }
135     }
136    
137     public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception {
138 torben 305
139 torben 978 DepartureBean departureBean = new DepartureBean();
140 torben 992
141 torben 970
142 torben 1034 String typeString = getTypeStringAzure(type);
143 torben 970 String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
144 torben 994
145     stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
146 torben 970
147 torben 1034 String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";
148 torben 994
149 torben 1048 logger.fine("URI: " + uri);
150 torben 1303 JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
151 torben 421 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
152 torben 305
153 torben 992 Document page = (Document) breaker.invoke(wrapper);
154 torben 305
155 torben 829 String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
156 torben 992 Element table = page.getElementById(tableName);
157 torben 829
158 torben 342 if (table != null) {
159 torben 992 Elements tableRows = table.getElementsByTag("tr");
160 torben 342
161 torben 1188 //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
162     //boolean passedTidsstreg = false;
163 torben 1020
164 torben 992 for (Element currentRow : tableRows) {
165     String rowClass = currentRow.attr("class");
166 torben 1188 /*
167 torben 1020 if (tidsstregExists == true && passedTidsstreg == false) {
168     if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
169     passedTidsstreg = true;
170     } else {
171     continue;
172     }
173 torben 1188 }*/
174 torben 1020
175 torben 342 if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
176 torben 1020
177 torben 992 Elements fields = currentRow.getElementsByTag("td");
178 torben 342
179 torben 978 DepartureEntry departure = new DepartureEntry();
180 torben 342
181 torben 992 String time = fields.get(0).text();
182 torben 375 if (time.equals(""))
183     time = "0:00"; //Bane.dk bug work-around
184 torben 342 departure.setTime(time);
185    
186     int updated = extractUpdated( fields.get(1) );
187     departure.setUpdated(updated);
188    
189 torben 992 String trainNumber = fields.get(2).text();
190 torben 972 if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
191 torben 1039 trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));
192 torben 342 departure.setTrainNumber(trainNumber);
193    
194 torben 992 String destination = fields.get(3).text();
195 torben 342 departure.setDestination(destination);
196    
197 torben 992 String origin = fields.get(4).text();
198 torben 342 departure.setOrigin(origin);
199    
200 torben 992 String location = fields.get(5).text();
201 torben 342 departure.setLocation(location);
202    
203 torben 992 String status = fields.get(6).text().trim();
204 torben 342 departure.setStatus(status);
205    
206     String note = extractNote( fields.get(7) );
207     departure.setNote(note);
208    
209 torben 972 departure.setType(typeString);
210 torben 697
211 torben 1063 departureBean.entries.add( departure );
212 torben 342 }
213     }
214 torben 348 } else {
215     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
216 torben 305 }
217 torben 978
218 torben 992 Element notifDiv = page.getElementById("station_planlagte_text");
219 torben 978 if (notifDiv != null) {
220    
221 torben 992 Elements tables = notifDiv.getElementsByTag("table");
222     for (Element tab : tables) {
223 torben 978
224 torben 992 Elements anchors = tab.getElementsByTag("a");
225 torben 978 if (anchors.size() == 2) {
226 torben 992 departureBean.notifications.add( anchors.get(1).text() );
227 torben 978 }
228     }
229    
230     }
231    
232    
233     return departureBean;
234 torben 305 }
235    
236 torben 1330 public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception {
237    
238     DepartureBean departureBean = new DepartureBean();
239    
240    
241     String typeString = getTypeStringWww(traintype);
242     String arrivalDeparture = (arrival==false) ? "afgang" : "ankomst";
243    
244     stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
245    
246 torben 1424
247 torben 1330 String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1";
248     logger.fine("URI: " + uri);
249     JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
250     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
251    
252     Document page = (Document) breaker.invoke(wrapper);
253    
254    
255     Element content = page.getElementsByClass("contentDiv").get(0);
256    
257    
258     if (content != null) {
259     Elements tableRows = content.child(0).children();
260    
261    
262    
263     for (Element currentRow : tableRows) {
264     if (currentRow.tagName().equals("br") ) {
265     break;
266     }
267    
268    
269 torben 1355 String link = currentRow.child(0).attr("href");
270    
271 torben 1335 logger.fine( currentRow.text() );
272 torben 1355 logger.fine("Href: " + link);
273 torben 1330
274    
275     String parts[] = currentRow.text().split(",");
276    
277    
278     DepartureEntry departure = new DepartureEntry();
279 torben 1355
280     //if we do these things upfront, then we are allowed to use continue statement when row contains no more data
281     departure.setType(typeString);
282     departureBean.entries.add( departure );
283 torben 1330
284     /*
285     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
286     */
287     int offset = 0;
288    
289     String time = parts[offset++];
290     if (time.equals(""))
291     time = "0:00"; //Bane.dk bug work-around
292     departure.setTime(time);
293    
294     int updated = 4; //does not exist on mobile
295     departure.setUpdated(updated);
296    
297 torben 1366 String trainNumber = extractTrainNumberMobile(link);
298 torben 1330 /*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber
299     trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/
300     departure.setTrainNumber(trainNumber);
301    
302 torben 1334 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
303 torben 1366 String stogLine = parts[offset++].trim();
304     departure.setTrainNumber(stogLine + " " + trainNumber);
305 torben 1333 }
306    
307 torben 1366 String destination = parts[offset++].trim();;
308 torben 1330 departure.setDestination(destination);
309    
310 torben 1332 String origin = "-"; // fields.get(4).text(); does not exist on mobile
311 torben 1330 departure.setOrigin(origin);
312    
313     String location = ""; // fields.get(5).text(); does not exist on mobile
314     departure.setLocation(location);
315 torben 1355
316     if (offset == parts.length) {
317     continue;
318     }
319    
320     if (parts[offset].trim().equalsIgnoreCase("NB!")) {
321     offset++;
322     }
323    
324     if (offset == parts.length) {
325     continue;
326     }
327 torben 1330
328 torben 1366 String status = parts[offset++].trim();; //fields.get(6).text().trim(); - extract from url
329 torben 1330 departure.setStatus(status);
330    
331     String note = ""; //extractNote( fields.get(7) ); - extract from url
332     departure.setNote(note);
333    
334     }
335     } else {
336     logger.warning("No departures found for station=" + stationcode + ", type=" + traintype);
337     }
338    
339     return departureBean;
340     }
341 torben 1038
342    
343 torben 1330
344 torben 1040 public static String cleanText(String input) {
345     //apparently JSoup translates &nbsp; characters on www.bane.dk to 0xA0
346 torben 1038 return input.replace((char) 0xA0, (char)0x20).trim();
347     }
348    
349 torben 1330
350     // old www site is not available any more
351     @Deprecated
352 torben 1034 public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
353 torben 580
354 torben 1034 DepartureBean departureBean = new DepartureBean();
355 torben 580
356 torben 1034 String type = getTypeStringWww(trainType);
357    
358 torben 1045 stationcode = URLEncoder.encode(stationcode, "ISO-8859-1");
359    
360 torben 1034
361     String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
362 torben 1048 logger.fine("URI:" + uri);
363    
364 torben 1047
365 torben 1303 JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() );
366 torben 580 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
367    
368 torben 1034 Element page = (Element) breaker.invoke(wrapper);
369 torben 580
370 torben 1034 String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
371     Element table = page.getElementById(tableName);
372 torben 580
373 torben 1046
374    
375 torben 1034 if (table != null) {
376     Elements tableRows = table.getElementsByTag("tr");
377 torben 580
378 torben 1188 //boolean passedTidsstreg = false;
379     //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
380 torben 1046
381 torben 1034 for (Element currentRow : tableRows) {
382     String rowClass = currentRow.attr("class");
383 torben 1188 /*
384 torben 1046 if (tidsstregExists == true && passedTidsstreg == false) {
385     if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
386     passedTidsstreg = true;
387     } else {
388     continue;
389     }
390 torben 1188 }*/
391 torben 1046
392    
393 torben 1034 if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
394     Elements fields = currentRow.getElementsByTag("td");
395    
396     DepartureEntry departure = new DepartureEntry();
397    
398    
399    
400 torben 1038 String time = cleanText( fields.get(0).getAllElements().get(2).text() );
401 torben 1034 if (time.equals(""))
402     time = "0:00"; //Bane.dk bug work-around
403     departure.setTime(time);
404    
405     int updated = extractUpdated( fields.get(1) );
406     departure.setUpdated(updated);
407    
408 torben 1038 String trainNumber = cleanText( fields.get(2).text() );
409 torben 1034 if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
410 torben 1039 trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2));
411 torben 1034 departure.setTrainNumber(trainNumber);
412    
413 torben 1038 String destination = cleanText( fields.get(3).text() );
414 torben 1034 departure.setDestination(destination);
415    
416 torben 1038 String origin = cleanText( fields.get(4).text() );
417 torben 1034 departure.setOrigin(origin);
418    
419 torben 1038 String location = cleanText( fields.get(5).text() );
420 torben 1034 departure.setLocation(location);
421    
422 torben 1038 String status = cleanText( fields.get(6).text() );
423 torben 1034 departure.setStatus(status);
424    
425 torben 1038 String note = cleanText( extractNote( fields.get(7) ) );
426 torben 1034 departure.setNote(note);
427    
428     departure.setType(type);
429    
430 torben 1063 departureBean.entries.add(departure);
431 torben 1034
432    
433 torben 580 }
434     }
435     } else {
436     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
437     }
438    
439 torben 591
440 torben 1034 return departureBean;
441     }
442    
443 torben 580
444 torben 992 private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
445 torben 305 int updated = -1;
446    
447 torben 992 Elements updatedImgs = updatedTd.getElementsByTag("img");
448     String updatedStr = updatedImgs.get(0).attr("src");
449 torben 305
450     if (updatedStr != null) {
451     for (int i=0; i<updatedStr.length(); i++) {
452     char c = updatedStr.charAt(i);
453     if ( Character.isDigit(c)) {
454     updated = Character.digit(c, 10);
455     break;
456     }
457     }
458     }
459     return updated;
460     }
461    
462 torben 992 private String extractNote(Element noteTd) {
463     String note = noteTd.text().trim();
464 torben 313
465 torben 992
466     Elements elems = noteTd.getElementsByClass("bemtype");
467 torben 313 if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
468     note = note.substring(0,note.length() -1 );
469    
470 torben 1038 return note.trim();
471 torben 313 }
472    
473 torben 1039 private String extractTrainNumberAzure(Element trainTd) {
474 torben 992 Element anchorElement = trainTd.getElementsByTag("a").get(0);
475     String href = anchorElement.attr("href");
476 torben 349
477 torben 973 int pos = href.lastIndexOf('/');
478     String number = href.substring(pos+1);
479 torben 970
480 torben 349 return number;
481     }
482    
483 torben 1366 private String extractTrainNumberMobile(String link) {
484     Map<String,String> elements = HttpUtil.decodeParams(link);
485    
486     return elements.get("tognummer");
487     }
488    
489 torben 1039 private String extractTrainNumberWww(Element trainTd) {
490     String number = "";
491     Element anchorElement = trainTd.getElementsByTag("a").get(0);
492     String href = anchorElement.attr("href");
493 torben 1366
494     String argstring = href.split("?")[1];
495     Map<String,String> elements = HttpUtil.decodeParams(argstring);
496     number = elements.get("TogNr");
497 torben 1039
498 torben 1366
499     /*String argstring = href.substring( href.indexOf('?') + 1);
500 torben 1039 String args[] = argstring.split("&");
501     for (String arg : args) {
502     String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
503    
504     if (pair[0].equalsIgnoreCase("TogNr"))
505     number = pair[1];
506 torben 1366 }*/
507    
508 torben 1039
509     return number;
510     }
511    
512    
513 torben 305 //test
514 torben 580 /*
515 torben 451 public static void main(String args[]) throws Exception {
516 torben 305 DepartureFetcher f = new DepartureFetcher();
517 torben 307 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
518 torben 305 for(DepartureBean d : deps) {
519     System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
520     d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
521     }
522    
523     System.out.println("--------------------------");
524 torben 580 }*/
525 torben 305 }

  ViewVC Help
Powered by ViewVC 1.1.20