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

  ViewVC Help
Powered by ViewVC 1.1.20