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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1093 - (hide annotations) (download)
Tue Sep 21 20:10:46 2010 UTC (13 years, 8 months ago) by torben
File size: 13278 byte(s)
Code Sync (use jsr107 / memcache for caching)
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 1093 import java.util.HashMap;
8 torben 428 import java.util.Map;
9 torben 1093 import java.util.logging.Level;
10 torben 348 import java.util.logging.Logger;
11 torben 305
12 torben 1093 import net.sf.jsr107cache.Cache;
13     import net.sf.jsr107cache.CacheException;
14     import net.sf.jsr107cache.CacheManager;
15    
16 torben 992 import org.jsoup.nodes.Document;
17     import org.jsoup.nodes.Element;
18     import org.jsoup.select.Elements;
19 torben 305
20 torben 1093 import com.google.appengine.api.memcache.jsr107cache.GCacheFactory;
21    
22 torben 1061 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 torben 468 import dk.thoerup.circuitbreaker.CircuitBreaker;
26     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
27 torben 588 import dk.thoerup.traininfoservice.StationDAO;
28 torben 711 import dk.thoerup.traininfoservice.Statistics;
29 torben 307
30 torben 305 public class DepartureFetcher {
31 torben 348
32 torben 972 enum TrainType{
33     STOG,
34     REGIONAL
35     }
36 torben 1093 Cache cache;
37 torben 972
38 torben 1093 Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
39 torben 387
40 torben 588 StationDAO stationDao = new StationDAO();
41    
42 torben 1034 private boolean useAzureSite;
43 torben 1026 private int replyTimeout;
44 torben 580
45 torben 1093 @SuppressWarnings("unchecked")
46 torben 1034 public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {
47 torben 1026 this.replyTimeout = replyTimeout;
48 torben 1034 useAzureSite = azureSite;
49 torben 1093
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 torben 580 }
60    
61    
62 torben 307
63 torben 387
64 torben 978 public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception {
65 torben 1093 final String key = "departure:" + stationID + ":" + arrival;
66 torben 980
67 torben 1093 DepartureBean departureBean = (DepartureBean) cache.get(key);
68 torben 387
69 torben 978 if (departureBean == null) {
70     departureBean = lookupDepartures(stationID,arrival);
71     cache.put(key, departureBean);
72 torben 1093 logger.info("Departure: Cache miss " + key + " !!! "); //remove before production
73 torben 387 } else {
74 torben 711 Statistics.getInstance().incrementDepartureCacheHits();
75 torben 1093 logger.info("Departure: Cache hit " + key);
76 torben 387 }
77 torben 1093
78 torben 980 return departureBean;
79 torben 387 }
80    
81    
82 torben 978 public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception {
83 torben 307
84 torben 978 DepartureBean departureBean = new DepartureBean();
85    
86 torben 1093 //TODO: debug code
87 torben 1080 StationEntry station = new StationEntry(); // stationDao.getById(stationID);
88     station.setId(82);
89     station.setName("Test Station");
90     station.setRegional("HS");
91 torben 1093 //TODO: end of debug code
92 torben 307
93 torben 1021 departureBean.stationName = station.getName();
94    
95 torben 588 if (station.getRegional() != null) {
96 torben 978 DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
97 torben 1063 departureBean.entries.addAll( tempBean.entries );
98 torben 978 departureBean.notifications.addAll(tempBean.notifications);
99 torben 307 }
100    
101 torben 588 if (station.getStrain() != null) {
102 torben 978 DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
103 torben 1063 departureBean.entries.addAll( tempBean.entries );
104 torben 978 departureBean.notifications.addAll(tempBean.notifications);
105 torben 588 }
106    
107 torben 1063 if (departureBean.entries.size() == 0) {
108 torben 1037 logger.info("No departures found for station " + stationID);
109     }
110    
111 torben 1063 Collections.sort( departureBean.entries );
112 torben 588
113    
114 torben 978 return departureBean;
115 torben 305 }
116    
117 torben 978 public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
118 torben 1034 if (useAzureSite == true) {
119     return lookupDeparturesAzureSite(stationcode, type, arrival);
120     } else {
121     return lookupDeparturesWwwSite(stationcode, type, arrival);
122 torben 580 }
123     }
124    
125 torben 1034 private String getTypeStringAzure(TrainType type) {
126 torben 972 switch (type) {
127     case STOG:
128     return "S-Tog";
129     case REGIONAL:
130     return "Fjerntog";
131     default:
132     return ""; //Can not happen
133     }
134     }
135    
136 torben 1034 private String getTypeStringWww(TrainType type) {
137     switch (type) {
138     case STOG:
139     return "S2";
140     case REGIONAL:
141     return "FJRN";
142     default:
143     return ""; //Can not happen
144     }
145     }
146    
147     public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception {
148 torben 305
149 torben 978 DepartureBean departureBean = new DepartureBean();
150 torben 992
151 torben 970
152 torben 1034 String typeString = getTypeStringAzure(type);
153 torben 970 String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
154 torben 994
155     stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
156 torben 970
157 torben 1034 String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";
158 torben 994
159 torben 1048 logger.fine("URI: " + uri);
160 torben 1026 JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
161 torben 421 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
162 torben 305
163 torben 992 Document page = (Document) breaker.invoke(wrapper);
164 torben 305
165 torben 829 String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
166 torben 992 Element table = page.getElementById(tableName);
167 torben 829
168 torben 342 if (table != null) {
169 torben 992 Elements tableRows = table.getElementsByTag("tr");
170 torben 342
171 torben 1020 boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
172     boolean passedTidsstreg = false;
173    
174 torben 992 for (Element currentRow : tableRows) {
175     String rowClass = currentRow.attr("class");
176 torben 1020
177     if (tidsstregExists == true && passedTidsstreg == false) {
178     if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
179     passedTidsstreg = true;
180     } else {
181     continue;
182     }
183     }
184    
185 torben 342 if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
186 torben 1020
187 torben 992 Elements fields = currentRow.getElementsByTag("td");
188 torben 342
189 torben 978 DepartureEntry departure = new DepartureEntry();
190 torben 342
191 torben 992 String time = fields.get(0).text();
192 torben 375 if (time.equals(""))
193     time = "0:00"; //Bane.dk bug work-around
194 torben 342 departure.setTime(time);
195    
196     int updated = extractUpdated( fields.get(1) );
197     departure.setUpdated(updated);
198    
199 torben 992 String trainNumber = fields.get(2).text();
200 torben 972 if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
201 torben 1039 trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));
202 torben 342 departure.setTrainNumber(trainNumber);
203    
204 torben 992 String destination = fields.get(3).text();
205 torben 342 departure.setDestination(destination);
206    
207 torben 992 String origin = fields.get(4).text();
208 torben 342 departure.setOrigin(origin);
209    
210 torben 992 String location = fields.get(5).text();
211 torben 342 departure.setLocation(location);
212    
213 torben 992 String status = fields.get(6).text().trim();
214 torben 342 departure.setStatus(status);
215    
216     String note = extractNote( fields.get(7) );
217     departure.setNote(note);
218    
219 torben 972 departure.setType(typeString);
220 torben 697
221 torben 1063 departureBean.entries.add( departure );
222 torben 342 }
223     }
224 torben 348 } else {
225     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
226 torben 305 }
227 torben 978
228 torben 992 Element notifDiv = page.getElementById("station_planlagte_text");
229 torben 978 if (notifDiv != null) {
230    
231 torben 992 Elements tables = notifDiv.getElementsByTag("table");
232     for (Element tab : tables) {
233 torben 978
234 torben 992 Elements anchors = tab.getElementsByTag("a");
235 torben 978 if (anchors.size() == 2) {
236 torben 992 departureBean.notifications.add( anchors.get(1).text() );
237 torben 978 }
238     }
239    
240     }
241    
242    
243     return departureBean;
244 torben 305 }
245    
246 torben 1038
247    
248 torben 1040 public static String cleanText(String input) {
249     //apparently JSoup translates   characters on www.bane.dk to 0xA0
250 torben 1038 return input.replace((char) 0xA0, (char)0x20).trim();
251     }
252    
253 torben 1034 public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
254 torben 580
255 torben 1034 DepartureBean departureBean = new DepartureBean();
256 torben 580
257 torben 1034 String type = getTypeStringWww(trainType);
258    
259 torben 1045 stationcode = URLEncoder.encode(stationcode, "ISO-8859-1");
260    
261 torben 1034
262     String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
263 torben 1048 logger.fine("URI:" + uri);
264    
265 torben 1047
266 torben 1034 JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
267 torben 580 CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
268    
269 torben 1034 Element page = (Element) breaker.invoke(wrapper);
270 torben 580
271 torben 1034 String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
272     Element table = page.getElementById(tableName);
273 torben 580
274 torben 1046
275    
276 torben 1034 if (table != null) {
277     Elements tableRows = table.getElementsByTag("tr");
278 torben 580
279 torben 1046 boolean passedTidsstreg = false;
280     boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
281    
282 torben 1034 for (Element currentRow : tableRows) {
283     String rowClass = currentRow.attr("class");
284 torben 1046
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 torben 1034 if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
295     Elements fields = currentRow.getElementsByTag("td");
296    
297     DepartureEntry departure = new DepartureEntry();
298    
299    
300    
301 torben 1038 String time = cleanText( fields.get(0).getAllElements().get(2).text() );
302 torben 1034 if (time.equals(""))
303     time = "0:00"; //Bane.dk bug work-around
304     departure.setTime(time);
305    
306     int updated = extractUpdated( fields.get(1) );
307     departure.setUpdated(updated);
308    
309 torben 1038 String trainNumber = cleanText( fields.get(2).text() );
310 torben 1034 if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
311 torben 1039 trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2));
312 torben 1034 departure.setTrainNumber(trainNumber);
313    
314 torben 1038 String destination = cleanText( fields.get(3).text() );
315 torben 1034 departure.setDestination(destination);
316    
317 torben 1038 String origin = cleanText( fields.get(4).text() );
318 torben 1034 departure.setOrigin(origin);
319    
320 torben 1038 String location = cleanText( fields.get(5).text() );
321 torben 1034 departure.setLocation(location);
322    
323 torben 1038 String status = cleanText( fields.get(6).text() );
324 torben 1034 departure.setStatus(status);
325    
326 torben 1038 String note = cleanText( extractNote( fields.get(7) ) );
327 torben 1034 departure.setNote(note);
328    
329     departure.setType(type);
330    
331 torben 1063 departureBean.entries.add(departure);
332 torben 1034
333    
334 torben 580 }
335     }
336     } else {
337     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
338     }
339    
340 torben 591
341 torben 1034 return departureBean;
342     }
343    
344 torben 580
345 torben 992 private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
346 torben 305 int updated = -1;
347    
348 torben 992 Elements updatedImgs = updatedTd.getElementsByTag("img");
349     String updatedStr = updatedImgs.get(0).attr("src");
350 torben 305
351     if (updatedStr != null) {
352     for (int i=0; i<updatedStr.length(); i++) {
353     char c = updatedStr.charAt(i);
354     if ( Character.isDigit(c)) {
355     updated = Character.digit(c, 10);
356     break;
357     }
358     }
359     }
360     return updated;
361     }
362    
363 torben 992 private String extractNote(Element noteTd) {
364     String note = noteTd.text().trim();
365 torben 313
366 torben 992
367     Elements elems = noteTd.getElementsByClass("bemtype");
368 torben 313 if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
369     note = note.substring(0,note.length() -1 );
370    
371 torben 1038 return note.trim();
372 torben 313 }
373    
374 torben 1039 private String extractTrainNumberAzure(Element trainTd) {
375 torben 992 Element anchorElement = trainTd.getElementsByTag("a").get(0);
376     String href = anchorElement.attr("href");
377 torben 349
378 torben 973 int pos = href.lastIndexOf('/');
379     String number = href.substring(pos+1);
380 torben 970
381 torben 349 return number;
382     }
383    
384 torben 1039 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 torben 305 //test
404 torben 580 /*
405 torben 451 public static void main(String args[]) throws Exception {
406 torben 305 DepartureFetcher f = new DepartureFetcher();
407 torben 307 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
408 torben 305 for(DepartureBean d : deps) {
409     System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
410     d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
411     }
412    
413     System.out.println("--------------------------");
414 torben 580 }*/
415 torben 305 }

  ViewVC Help
Powered by ViewVC 1.1.20