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

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

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

revision 939 by torben, Mon Jun 28 12:35:35 2010 UTC revision 1026 by torben, Thu Sep 2 18:37:49 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.traininfoservice.banedk;  package dk.thoerup.traininfoservice.banedk;
2    
3    
4  import java.io.IOException;  
5  import java.net.URL;  import java.net.URL;
6  import java.sql.SQLException;  import java.sql.SQLException;
7  import java.util.ArrayList;  import java.util.ArrayList;
# Line 10  import java.util.Map; Line 10  import java.util.Map;
10  import java.util.logging.Level;  import java.util.logging.Level;
11  import java.util.logging.Logger;  import java.util.logging.Logger;
12    
13  import com.gargoylesoftware.htmlunit.BrowserVersion;  import org.jsoup.nodes.Document;
14  import com.gargoylesoftware.htmlunit.Page;  import org.jsoup.nodes.Element;
15  import com.gargoylesoftware.htmlunit.RefreshHandler;  import org.jsoup.select.Elements;
 import com.gargoylesoftware.htmlunit.WebClient;  
 import com.gargoylesoftware.htmlunit.html.DomNodeList;  
 import com.gargoylesoftware.htmlunit.html.HtmlElement;  
 import com.gargoylesoftware.htmlunit.html.HtmlPage;  
16    
17  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
18  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
# Line 24  import dk.thoerup.traininfoservice.Stati Line 20  import dk.thoerup.traininfoservice.Stati
20  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
21    
22  public class TimetableFetcher {  public class TimetableFetcher {
23            
         class NullRefreshHandler implements RefreshHandler {  
                 public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {                    
                 }  
                   
         }  
24                                    
25          Map<String, List<TimetableBean>> cache;          Map<String, List<TimetableBean>> cache;
26          Map<String, Integer> stationCache;          Map<String, Integer> stationCache;
# Line 40  public class TimetableFetcher { Line 31  public class TimetableFetcher {
31          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
32                    
33          private boolean useTempSite;          private boolean useTempSite;
34            private int replyTimeout;
35                    
36          public TimetableFetcher(boolean tmpSite, int cacheTimeout) {          public TimetableFetcher(boolean tmpSite, int cacheTimeout, int replyTimeout) {
37                  useTempSite = tmpSite;                  useTempSite = tmpSite;
38                    this.replyTimeout = replyTimeout;
39                                    
40                  cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);                  cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);
41                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
# Line 90  public class TimetableFetcher { Line 83  public class TimetableFetcher {
83          List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {                      List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {            
84                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();
85                                    
86                  String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";                  //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
87                                                    String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
88    
             final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);  
             webClient.setTimeout(2500);  
             webClient.setJavaScriptEnabled(false);          
             webClient.setRefreshHandler( new NullRefreshHandler() );  
             webClient.setCssEnabled(false);  
89                            
90                            JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);
             HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, url);  
91              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
92                            
93              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
94                                            
95                            
96              boolean currentStation = false;              boolean currentStation = false;
97              boolean currentStationSaved = false;              boolean currentStationSaved = false;
98                            
99              List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");              Elements tables = doc.getElementsByClass("Rute");
100                
101              if (tables.size() == 1) {              if (tables.size() == 1) {
102                  HtmlElement timetable = tables.get(0);                  Element timetable = tables.get(0);
103                  DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");                  Elements rows = timetable.getElementsByTag("tr");
104                                    
105                  for (int i=0; i<rows.size(); i++) {                  for (int i=0; i<rows.size(); i++) {
106                          if (i==0) //First row is column headers                          if (i==0) //First row is column headers
107                                  continue;                                  continue;
108                                                    
109                                                    
110                          HtmlElement row = rows.get(i);                          Element row = rows.get(i);
111                          DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");                          Elements fields = row.getElementsByTag("td");
112    
113                                                    
114                          if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {                          if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
115                                  currentStation = true;                                  currentStation = true;
116                                  continue;                                  continue;
117                          }                          }
118                                                    
119                          TimetableBean bean = new TimetableBean();                          TimetableBean bean = new TimetableBean();
120                                                    
121                          String station = fields.get(0).asText() ;                          String station = fields.get(0).text() ;
122                          if (station.equals("København"))                          if (station.equals("København"))
123                                  station = "København H"; //correct inconsistency in naming                                  station = "København H"; //correct inconsistency in naming
124                                                    
125                          bean.setStation( station );                          bean.setStation( station );
126                          bean.setArrival( fields.get(1).asText() );                          bean.setArrival( fields.get(1).text() );
127                          bean.setDeparture( fields.get(2).asText() );                          bean.setDeparture( fields.get(2).text() );
128                            
129                            boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
130                            bean.setCancelled(cancelled);
131                                                    
132                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
133                                  bean.setCurrent(currentStation);                                  bean.setCurrent(currentStation);
# Line 145  public class TimetableFetcher { Line 137  public class TimetableFetcher {
137                          bean.setStationId( getStationId( station ));                          bean.setStationId( getStationId( station ));
138                                                    
139                          timetableList.add(bean);                          timetableList.add(bean);
140                    }
141                    
142                    //TODO: There is an off-by-one error in this cancelled parser thingie
143                    final String cancelledString = "Aflyst";
144                    for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels
145                            final int lastIdx = (timetableList.size() - 1);
146                            
147                            TimetableBean current = timetableList.get(i);
148                            if (current.isCancelled()) {
149                                    if (i == 0) {
150                                            current.setDeparture(cancelledString);
151                                    } else if (i == lastIdx) {
152                                            current.setArrival(cancelledString);
153                                    } else if (i>0 && i<lastIdx) {
154                                            TimetableBean next = timetableList.get(i+1);
155                                            TimetableBean prev = timetableList.get(i-1);
156                                            
157                                            if (next.isCancelled())
158                                                    current.setDeparture(cancelledString);
159                                            if (prev.isCancelled())
160                                                    current.setArrival(cancelledString);
161                                    }
162                            }
163                  }                  }
164                                    
165              } else {              } else {
166                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);
167              }              }
168              webClient.closeAllWindows();              
169                                    
170                  return timetableList;                  return timetableList;
171          }          }

Legend:
Removed from v.939  
changed lines
  Added in v.1026

  ViewVC Help
Powered by ViewVC 1.1.20