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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.970  
changed lines
  Added in v.1080

  ViewVC Help
Powered by ViewVC 1.1.20