/[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 975 by torben, Sat Jul 10 08:35:16 2010 UTC revision 1060 by torben, Thu Sep 16 13:32:10 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;
 import java.util.ArrayList;  
 import java.util.List;  
7  import java.util.Map;  import java.util.Map;
8  import java.util.logging.Level;  import java.util.logging.Level;
9  import java.util.logging.Logger;  import java.util.logging.Logger;
10    
11  import com.gargoylesoftware.htmlunit.BrowserVersion;  import org.jsoup.nodes.Document;
12  import com.gargoylesoftware.htmlunit.Page;  import org.jsoup.nodes.Element;
13  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;  
14    
15  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
16  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
# Line 24  import dk.thoerup.traininfoservice.Stati Line 18  import dk.thoerup.traininfoservice.Stati
18  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
19    
20  public class TimetableFetcher {  public class TimetableFetcher {
21            
         class NullRefreshHandler implements RefreshHandler {  
                 public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {                    
                 }  
                   
         }  
22                                    
23          Map<String, List<TimetableBean>> cache;          Map<String, TimetableBean> cache;
24          Map<String, Integer> stationCache;          Map<String, Integer> stationCache;
25    
26          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
# Line 39  public class TimetableFetcher { Line 28  public class TimetableFetcher {
28                    
29          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
30                    
31          private boolean useTempSite;          private boolean useAzureSite;
32            private int replyTimeout;
33                    
34          public TimetableFetcher(boolean tmpSite, int cacheTimeout) {          public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {
35                  useTempSite = tmpSite;                  useAzureSite = azureSite;
36                    this.replyTimeout = replyTimeout;
37                                    
38                  cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);                  cache = new TimeoutMap<String,TimetableBean>(cacheTimeout);
39                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );
40          }          }
41                    
42                    
43          List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {          TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {
44                  String key = trainID+type;                  String key = trainID+type;
45                  List<TimetableBean> list = cache.get(key);                  TimetableBean list = cache.get(key);
46                                    
47                  if (list == null) {                  if (list == null) {
48                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
# Line 63  public class TimetableFetcher { Line 54  public class TimetableFetcher {
54                  return list;                  return list;
55          }          }
56                    
57          List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
58                  if (useTempSite == false ){                  if (useAzureSite == true ){
59                          return lookupTimetableRealSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
60                            
61                  } else {                  } else {
62                          return new ArrayList<TimetableBean>(); // no timetable data on temp site                          return lookupTimetableWwwSite(trainID, type);
63                  }                  }
64          }          }
65                    
# Line 87  public class TimetableFetcher { Line 79  public class TimetableFetcher {
79                  return id;                  return id;
80          }          }
81    
82          List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
83                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
84                                    
                 //String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";  
                 String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                  
85    
86              final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
87              webClient.setTimeout(2500);                  logger.fine("URL:" + url);
             webClient.setJavaScriptEnabled(false);          
             webClient.setRefreshHandler( new NullRefreshHandler() );  
             webClient.setCssEnabled(false);  
               
88                            
89              HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, url);              JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);
90              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
91                            
92              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
93                                            
94                            
95              boolean currentStation = false;              boolean currentStation = false;
96              boolean currentStationSaved = false;              boolean currentStationSaved = false;
97                            
98              List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");              Elements tables = doc.getElementsByClass("Rute");
99                
100              if (tables.size() == 1) {              if (tables.size() == 1) {
101                  HtmlElement timetable = tables.get(0);                  Element timetable = tables.get(0);
102                  DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");                  Elements rows = timetable.getElementsByTag("tr");
103                                    
104                  for (int i=0; i<rows.size(); i++) {                  for (int i=0; i<rows.size(); i++) {
105                          if (i==0) //First row is column headers                          if (i==0) //First row is column headers
106                                  continue;                                  continue;
107                                                    
108                                                    
109                          HtmlElement row = rows.get(i);                          Element row = rows.get(i);
110                          DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");                          Elements fields = row.getElementsByTag("td");
111    
112                                                    
113                          if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {                          if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
114                                  currentStation = true;                                  currentStation = true;
115                                  continue;                                  continue;
116                          }                          }
117                                                    
118                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
119                                                    
120                          String station = fields.get(0).asText() ;                          String station = fields.get(0).text() ;
121                          if (station.equals("København"))                          if (station.equals("København"))
122                                  station = "København H"; //correct inconsistency in naming                                  station = "København H"; //correct inconsistency in naming
123                                                    
124                          bean.setStation( station );                          entry.setStation( station );
125                          bean.setArrival( fields.get(1).asText() );                          entry.setArrival( fields.get(1).text() );
126                          bean.setDeparture( fields.get(2).asText() );                          entry.setDeparture( fields.get(2).text() );
127                                                    
128                          boolean cancelled = fields.get(3).asText().equalsIgnoreCase("aflyst");                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
129                          bean.setCancelled(cancelled);                          entry.setCancelled(cancelled);
130                                                    
131                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
132                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
133                                  currentStationSaved = true;                                  currentStationSaved = true;
134                          }                          }
135                                                    
136                          bean.setStationId( getStationId( station ));                          entry.setStationId( getStationId( station ));
137                                                    
138                          timetableList.add(bean);                          timetableBean.entries.add(entry);
139                  }                  }
140                                    
141                    //TODO: There is an off-by-one error in this cancelled parser thingie
142                  final String cancelledString = "Aflyst";                  final String cancelledString = "Aflyst";
143                  for (int i=0;i<timetableList.size(); i++) { //handle cancelled labels                  for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
144                          final int lastIdx = (timetableList.size() - 1);                          final int lastIdx = (timetableBean.entries.size() - 1);
145                                                    
146                          TimetableBean current = timetableList.get(i);                          TimetableEntry current = timetableBean.entries.get(i);
147                          if (current.isCancelled() && i == 0) {                          if (current.isCancelled()) {
148                                  current.setDeparture(cancelledString);                                  if (i == 0) {
149                          } else if (current.isCancelled() && i == lastIdx) {                                          current.setDeparture(cancelledString);
150                                  current.setArrival(cancelledString);                                  } else if (i == lastIdx) {
151                          } else if (current.isCancelled() && i>0 && i<lastIdx) {                                          current.setArrival(cancelledString);
152                                  TimetableBean next = timetableList.get(i+1);                                  } else if (i>0 && i<lastIdx) {
153                                  TimetableBean prev = timetableList.get(i-1);                                          TimetableEntry next = timetableBean.entries.get(i+1);
154                                                                            TimetableEntry prev = timetableBean.entries.get(i-1);
155                                  if (next.isCancelled())                                          
156                                          current.setDeparture(cancelledString);                                          if (next.isCancelled())
157                                  if (prev.isCancelled())                                                  current.setDeparture(cancelledString);
158                                          current.setArrival(cancelledString);                                          if (prev.isCancelled())
159                                                    current.setArrival(cancelledString);
160                                    }
161                          }                          }
162                  }                  }
163                                    
164              } else {              } else {
165                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);
166              }              }
167              webClient.closeAllWindows();              
168                                    
169                  return timetableList;                  return timetableBean;
170          }          }
171    
172            TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
173                    TimetableBean timetableBean = new TimetableBean();
174                    
175                    String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
176                    logger.fine("URL:" + url);
177    
178                
179                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout);
180                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
181                
182                Document doc = (Document) breaker.invoke(wrapper);
183                        
184                
185                boolean currentStation = false;
186                boolean currentStationSaved = false;
187                
188                Elements tables = doc.getElementsByClass("Rute");
189                
190                if (tables.size() == 1) {
191                    Element timetable = tables.get(0);
192                    Elements rows = timetable.getElementsByTag("tr");
193                    
194                    for (int i=0; i<rows.size(); i++) {
195                            if (i==0) //First row is column headers
196                                    continue;
197                            
198                            
199                            Element row = rows.get(i);
200                            Elements fields = row.getElementsByTag("td");
201    
202                            
203                            if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
204                                    currentStation = true;
205                                    continue;
206                            }
207                            
208                            TimetableEntry entry = new TimetableEntry();
209                            
210                            String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
211                            if (station.equals("København"))
212                                    station = "København H"; //correct inconsistency in naming
213                            
214                            String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
215                            String departure = DepartureFetcher.cleanText( fields.get(2).text() );
216                            
217                            entry.setStation( station );
218                            entry.setArrival( arrival );
219                            entry.setDeparture( departure );
220                    
221                            
222                            if (currentStation == true && currentStationSaved == false ) {
223                                    entry.setCurrent(currentStation);
224                                    currentStationSaved = true;
225                            }
226                            
227                            entry.setStationId( getStationId( station ));
228                            
229                            timetableBean.entries.add(entry);
230                    }              
231                    
232                } else {
233                    logger.warning("No time table found, trainID=" + trainID + " type=" + type);
234                }
235                
236                    
237                    return timetableBean;
238            }      
239                    
240  }  }

Legend:
Removed from v.975  
changed lines
  Added in v.1060

  ViewVC Help
Powered by ViewVC 1.1.20