/[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 591 - (hide annotations) (download)
Wed Feb 10 14:07:04 2010 UTC (14 years, 3 months ago) by torben
File size: 8873 byte(s)
Act as a Firefox 3 browser, and cleanup HtmlUnit WebClient instance when done
1 torben 305 package dk.thoerup.traininfoservice.banedk;
2    
3     import java.util.ArrayList;
4 torben 307 import java.util.Collections;
5 torben 305 import java.util.List;
6 torben 428 import java.util.Map;
7 torben 348 import java.util.logging.Logger;
8 torben 305
9 torben 591 import com.gargoylesoftware.htmlunit.BrowserVersion;
10 torben 305 import com.gargoylesoftware.htmlunit.WebClient;
11     import com.gargoylesoftware.htmlunit.html.DomNodeList;
12     import com.gargoylesoftware.htmlunit.html.HtmlElement;
13     import com.gargoylesoftware.htmlunit.html.HtmlPage;
14    
15 torben 468 import dk.thoerup.circuitbreaker.CircuitBreaker;
16     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17 torben 588 import dk.thoerup.traininfoservice.StationBean;
18     import dk.thoerup.traininfoservice.StationDAO;
19 torben 307
20 torben 305 public class DepartureFetcher {
21 torben 348
22     Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
23 torben 387
24 torben 584 Map<Integer, List<DepartureBean>> cache;
25 torben 387
26 torben 588 StationDAO stationDao = new StationDAO();
27    
28 torben 580 private boolean useTempSite;
29    
30 torben 584 public DepartureFetcher(boolean tempSite, int cacheTimeout) {
31 torben 580 useTempSite = tempSite;
32 torben 584 cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout);
33 torben 580 }
34    
35    
36 torben 307
37 torben 387
38 torben 451 public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception {
39 torben 308
40 torben 387 List<DepartureBean> list = cache.get(stationID);
41    
42     if (list == null) {
43     list = lookupDepartures(stationID);
44     cache.put(stationID, list);
45     } else {
46 torben 389 logger.info("Departure: Cache hit " + stationID); //remove before production
47 torben 387 }
48     return list;
49     }
50    
51    
52 torben 451 public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
53 torben 307 List<DepartureBean> departureList = new ArrayList<DepartureBean>();
54    
55 torben 588 StationBean station = stationDao.getById(stationID);
56 torben 307
57 torben 588 if (station.getRegional() != null) {
58     List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN");
59     departureList.addAll(list);
60 torben 307 }
61    
62 torben 588 if (station.getStrain() != null) {
63     List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2");
64     departureList.addAll(list);
65     }
66    
67     Collections.sort( departureList );
68    
69    
70 torben 307 return departureList;
71 torben 305 }
72    
73 torben 451 public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
74 torben 580 if (useTempSite == false) {
75     return lookupDeparturesNormalSite(stationcode, type);
76     } else {
77     return lookupDeparturesFromTemporarySite(stationcode, type);
78     }
79     }
80    
81     public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception {
82 torben 305
83     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
84    
85 torben 591 final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 );
86 torben 386 webClient.setTimeout(2500);
87 torben 313 webClient.setJavaScriptEnabled(false);
88    
89 torben 421 String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
90     BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
91     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
92 torben 305
93 torben 421 HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
94 torben 305
95     HtmlElement table = page.getElementById("afgangtabel");
96    
97 torben 342 if (table != null) {
98     DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
99    
100     for (HtmlElement currentRow : tableRows) {
101     String rowClass = currentRow.getAttribute("class");
102     if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
103     DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
104    
105     DepartureBean departure = new DepartureBean();
106    
107     String time = fields.get(0).asText();
108 torben 375 if (time.equals(""))
109     time = "0:00"; //Bane.dk bug work-around
110 torben 342 departure.setTime(time);
111    
112     int updated = extractUpdated( fields.get(1) );
113     departure.setUpdated(updated);
114    
115     String trainNumber = fields.get(2).asText();
116 torben 410 if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
117 torben 349 trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
118 torben 342 departure.setTrainNumber(trainNumber);
119    
120     String destination = fields.get(3).asText();
121     departure.setDestination(destination);
122    
123     String origin = fields.get(4).asText();
124     departure.setOrigin(origin);
125    
126     String location = fields.get(5).asText();
127     departure.setLocation(location);
128    
129     String status = fields.get(6).asText();
130     departure.setStatus(status);
131    
132     String note = extractNote( fields.get(7) );
133     departure.setNote(note);
134    
135     departureList.add(departure);
136     }
137     }
138 torben 348 } else {
139     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
140 torben 305 }
141 torben 591 webClient.closeAllWindows();
142 torben 348
143 torben 305 return departureList;
144     }
145    
146 torben 580 public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
147    
148     List<DepartureBean> departureList = new ArrayList<DepartureBean>();
149    
150 torben 591 final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
151 torben 580 webClient.setTimeout(2500);
152     webClient.setJavaScriptEnabled(false);
153    
154    
155     String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
156    
157     BanedkInvocation wrapper = new BanedkInvocation(webClient, uri);
158     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
159    
160     HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
161    
162     HtmlElement table = page.getElementById("traf_afgang");
163    
164     if (table != null) {
165     DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
166    
167     boolean isFirst = true;
168    
169     for (HtmlElement currentRow : tableRows) {
170     if (isFirst == true) { //skip table headers
171     isFirst = false;
172     continue;
173     }
174    
175     DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
176    
177     DepartureBean departure = new DepartureBean();
178    
179     String time = fields.get(0).asText().trim();
180 torben 583
181 torben 580 if (time.equals(""))
182     time = "0:00"; //Bane.dk bug work-around
183     departure.setTime(time);
184    
185    
186     String trainNumber = fields.get(1).asText();
187     departure.setTrainNumber(trainNumber);
188    
189     String destination = fields.get(2).asText();
190     departure.setDestination(destination);
191    
192     String origin = fields.get(3).asText();
193     departure.setOrigin(origin);
194    
195     String status = fields.get(4).asText();
196     departure.setStatus(status);
197    
198 torben 582 String note = fields.get(5).asText();
199 torben 580 departure.setNote(note);
200    
201     departureList.add(departure);
202     }
203     } else {
204     logger.warning("No departures found for station=" + stationcode + ", type=" + type);
205     }
206 torben 591 webClient.closeAllWindows();
207 torben 580
208 torben 591
209 torben 580 return departureList;
210     }
211    
212    
213 torben 305 private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
214     int updated = -1;
215    
216     DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
217     String updatedStr = updatedImgs.get(0).getAttribute("src");
218    
219     if (updatedStr != null) {
220     for (int i=0; i<updatedStr.length(); i++) {
221     char c = updatedStr.charAt(i);
222     if ( Character.isDigit(c)) {
223     updated = Character.digit(c, 10);
224     break;
225     }
226     }
227     }
228     return updated;
229     }
230    
231 torben 313 private String extractNote(HtmlElement noteTd) {
232     String note = noteTd.asText().trim();
233    
234     List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype");
235     if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
236     note = note.substring(0,note.length() -1 );
237    
238     return note;
239     }
240    
241 torben 349 private String extractTrainNumber(HtmlElement trainTd) {
242     String number = "";
243     HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0);
244     String href = anchorElement.getAttribute("href");
245     String argstring = href.substring( href.indexOf('?') + 1);
246    
247     String args[] = argstring.split("&");
248     for (String arg : args) {
249     String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
250    
251     if (pair[0].equalsIgnoreCase("TogNr"))
252     number = pair[1];
253     }
254    
255    
256     return number;
257     }
258    
259 torben 305 //test
260 torben 580 /*
261 torben 451 public static void main(String args[]) throws Exception {
262 torben 305 DepartureFetcher f = new DepartureFetcher();
263 torben 307 List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
264 torben 305 for(DepartureBean d : deps) {
265     System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
266     d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
267     }
268    
269     System.out.println("--------------------------");
270 torben 580 }*/
271 torben 305 }

  ViewVC Help
Powered by ViewVC 1.1.20