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

  ViewVC Help
Powered by ViewVC 1.1.20