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

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

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

revision 842 by torben, Fri Jun 11 20:50:40 2010 UTC revision 2081 by torben, Sat Nov 23 12:08:25 2013 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.android.traininfo.common.StationEntry;
16    import dk.thoerup.android.traininfo.common.TimetableBean;
17    import dk.thoerup.android.traininfo.common.TimetableEntry;
18  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
19  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
20  import dk.thoerup.traininfoservice.StationDAO;  import dk.thoerup.genericjavautils.TimeoutMap;
21  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
22    import dk.thoerup.traininfoservice.TraininfoSettings;
23    import dk.thoerup.traininfoservice.db.StationDAO;
24    
25  public class TimetableFetcher {  public class TimetableFetcher {
26            
         class NullRefreshHandler implements RefreshHandler {  
                 public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException {                    
                 }  
                   
         }  
27                                    
28          Map<String, List<TimetableBean>> cache;          Map<String, TimetableBean> cache;
29          Map<String, Integer> stationCache;          Map<String, StationEntry> stationCache;
30    
31          StationDAO stationDao = new StationDAO();          StationDAO stationDao = new StationDAO();
32                    
33                    
34          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());          Logger logger = Logger.getLogger(TimetableFetcher.class.getName());
35    
36            TraininfoSettings settings;    
37                    
38          private boolean useTempSite;          public TimetableFetcher(TraininfoSettings settings) {
39                            this.settings = settings;
         public TimetableFetcher(boolean tmpSite, int cacheTimeout) {  
                 useTempSite = tmpSite;  
40                                    
41                  cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout);                  cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() );
42                  stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 );                  stationCache = new TimeoutMap<String,StationEntry>( 3*60*60*1000 );
43          }          }
44                    
45                    
46          List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception {          TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception {
47                  String key = trainID+type;                  String key = trainID+type;
48                  List<TimetableBean> list = cache.get(key);                  TimetableBean list = cache.get(key);
49                                    
50                  if (list == null) {                  if (list == null) {
51                          list = lookupTimetable(trainID,type);                          list = lookupTimetable(trainID,type);
# Line 63  public class TimetableFetcher { Line 57  public class TimetableFetcher {
57                  return list;                  return list;
58          }          }
59                    
60          List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception {          TimetableBean lookupTimetable(String trainID, String type) throws Exception {
61                  if (useTempSite == false ){                  if (settings.getBackend() == TraininfoSettings.Backend.Azure ){
62                          return lookupTimetableRealSite(trainID, type);                          return lookupTimetableAzureSite(trainID, type);
63                            
64                  } else {                  } else {
65                          return new ArrayList<TimetableBean>(); // no timetable data on temp site                          return lookupTimetableMobileSite(trainID, type);
66                  }                  }
67          }          }
68                    
69          int getStationId(String name) {          StationEntry getStation(String name) {
70                  Integer id = stationCache.get(name);                  StationEntry station = stationCache.get(name);
71                                    
72                  if (id == null) {                  if (station == null) {
73                          try {                          try {
74                                  id = stationDao.getIdByName(name);                                  station = stationDao.getSimpleByName(name);
75                                  stationCache.put(name, id);                                  if (station != null) {
76                                            stationCache.put(name,station);
77                                    }
78                          } catch (SQLException e) {                          } catch (SQLException e) {
79                                  logger.log(Level.SEVERE, "getStationId failed", e);                                  logger.log(Level.SEVERE, "getStationId failed", e);
                                 id = -1;  
80                          }                          }
81                  }                  }
82    
83                  return id;                  return station;
84            }
85            
86            String correctStationName(String name) {
87                    if (name.equals("København"))
88                            name = "København H"; //correct inconsistency in naming
89                    
90                    return name;            
91          }          }
92    
93          List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception {                      TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception {          
94                  List<TimetableBean> timetableList = new ArrayList<TimetableBean>();                  TimetableBean timetableBean = new TimetableBean();
95                                    
                 String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";  
                                   
96    
97              final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);                  String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID;                
98              webClient.setTimeout(2500);                  logger.fine("URL:" + url);
             webClient.setJavaScriptEnabled(false);          
             webClient.setRefreshHandler( new NullRefreshHandler() );  
             webClient.setCssEnabled(false);  
99                            
100                            JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
             BanedkInvocation wrapper = new BanedkInvocation(webClient, url);  
101              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
102                            
103              HtmlPage page = (HtmlPage) breaker.invoke(wrapper);              Document doc = (Document) breaker.invoke(wrapper);
104                                            
105                            
106              boolean currentStation = false;              boolean currentStation = false;
107              boolean currentStationSaved = false;              boolean currentStationSaved = false;
108                            
109              List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute");              Elements tables = doc.getElementsByClass("Rute");
110                
111              if (tables.size() == 1) {              if (tables.size() == 1) {
112                  HtmlElement timetable = tables.get(0);                  Element timetable = tables.get(0);
113                  DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr");                  Elements rows = timetable.getElementsByTag("tr");
114                                    
115                  for (int i=0; i<rows.size(); i++) {                  for (int i=0; i<rows.size(); i++) {
116                          if (i==0) //First row is column headers                          if (i==0) //First row is column headers
117                                  continue;                                  continue;
118                                                    
119                                                    
120                          HtmlElement row = rows.get(i);                          Element row = rows.get(i);
121                          DomNodeList<HtmlElement> fields = row.getElementsByTagName("td");                          Elements fields = row.getElementsByTag("td");
122    
123                                                    
124                          if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) {                          if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
125                                  currentStation = true;                                  currentStation = true;
126                                  continue;                                  continue;
127                          }                          }
128                                                    
129                          TimetableBean bean = new TimetableBean();                          TimetableEntry entry = new TimetableEntry();
130                                                    
131                          String station = fields.get(0).asText() ;                          String station = correctStationName( fields.get(0).text() );
132                          if (station.equals("København"))                          
133                                  station = "København H"; //correct inconsistency in naming                          entry.setStation( station );
134                                                    entry.setArrival( fields.get(1).text() );
135                          bean.setStation( station );                          entry.setDeparture( fields.get(2).text() );
136                          bean.setArrival( fields.get(1).asText() );                          
137                          bean.setDeparture( fields.get(2).asText() );                          boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst");
138                            entry.setCancelled(cancelled);
139                                                    
140                          if (currentStation == true && currentStationSaved == false ) {                          if (currentStation == true && currentStationSaved == false ) {
141                                  bean.setCurrent(currentStation);                                  entry.setCurrent(currentStation);
142                                  currentStationSaved = true;                                  currentStationSaved = true;
143                          }                          }
144                                                    
145                          bean.setStationId( getStationId( station ));                          entry.setStationEntry( getStation( station ));
146                                                    
147                          timetableList.add(bean);                          timetableBean.entries.add(entry);
148                    }
149                    
150                    //TODO: There is an off-by-one error in this cancelled parser thingie
151                    final String cancelledString = "Aflyst";
152                    for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels
153                            final int lastIdx = (timetableBean.entries.size() - 1);
154                            
155                            TimetableEntry current = timetableBean.entries.get(i);
156                            if (current.isCancelled()) {
157                                    if (i == 0) {
158                                            current.setDeparture(cancelledString);
159                                    } else if (i == lastIdx) {
160                                            current.setArrival(cancelledString);
161                                    } else if (i>0 && i<lastIdx) {
162                                            TimetableEntry next = timetableBean.entries.get(i+1);
163                                            TimetableEntry prev = timetableBean.entries.get(i-1);
164                                            
165                                            if (next.isCancelled())
166                                                    current.setDeparture(cancelledString);
167                                            if (prev.isCancelled())
168                                                    current.setArrival(cancelledString);
169                                    }
170                            }
171                  }                  }
172                                    
173              } else {              } else {
174                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);                  logger.warning("No time table found, trainID=" + trainID + " type=" + type);
175              }              }
176              webClient.closeAllWindows();              
177                                    
178                  return timetableList;                  return timetableBean;
179            }
180    
181            TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception {
182                    TimetableBean timetableBean = new TimetableBean();
183                    
184                    String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute";
185                    logger.fine("URL:" + url);
186    
187                
188                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
189                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
190                
191                Document doc = (Document) breaker.invoke(wrapper);
192    
193                Element content = doc.getElementsByClass("contentDiv").get(1);
194                Element dlist = content.child(0);
195    
196                
197                Elements rows = dlist.getElementsByTag("dt");
198    
199                for (int i=0; i<rows.size(); i++) {
200    
201                    Element row = rows.get(i);
202                    
203                    logger.fine( row.text() );
204                    
205                    String parts[] = row.text().split(",");
206                    
207                    TimetableEntry entry = new TimetableEntry();
208    
209                    String station = DepartureFetcher.cleanText( parts[0] ) ;
210                    station = correctStationName(station);
211    
212    
213                    String arrival = DepartureFetcher.cleanText( parts[1] );
214                    String departure = DepartureFetcher.cleanText( "" );
215    
216                    entry.setStation( station );
217                    entry.setArrival( arrival );
218                    entry.setDeparture( departure );
219    
220    
221                    entry.setStationEntry( getStation( station ));
222    
223                    timetableBean.entries.add(entry);
224                }          
225    
226    
227                return timetableBean;
228    
229          }          }
230                    
231            @Deprecated
232            TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception {            
233                    TimetableBean timetableBean = new TimetableBean();
234                    
235                    String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276";
236                    logger.fine("URL:" + url);
237    
238                
239                JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() );
240                CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
241                
242                Document doc = (Document) breaker.invoke(wrapper);
243                        
244                
245                boolean currentStation = false;
246                boolean currentStationSaved = false;
247                
248                Elements tables = doc.getElementsByClass("Rute");
249                
250                if (tables.size() == 1) {
251                    Element timetable = tables.get(0);
252                    Elements rows = timetable.getElementsByTag("tr");
253                    
254                    for (int i=0; i<rows.size(); i++) {
255                            if (i==0) //First row is column headers
256                                    continue;
257                            
258                            
259                            Element row = rows.get(i);
260                            Elements fields = row.getElementsByTag("td");
261    
262                            
263                            if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) {
264                                    currentStation = true;
265                                    continue;
266                            }
267                            
268                            TimetableEntry entry = new TimetableEntry();
269                            
270                            String station = DepartureFetcher.cleanText( fields.get(0).text() ) ;
271                            station = correctStationName(station);
272    
273                            
274                            String arrival = DepartureFetcher.cleanText( fields.get(1).text() );
275                            String departure = DepartureFetcher.cleanText( fields.get(2).text() );
276                            
277                            entry.setStation( station );
278                            entry.setArrival( arrival );
279                            entry.setDeparture( departure );
280                    
281                            
282                            if (currentStation == true && currentStationSaved == false ) {
283                                    entry.setCurrent(currentStation);
284                                    currentStationSaved = true;
285                            }
286                            
287                            entry.setStationEntry( getStation( station ));
288                            
289                            timetableBean.entries.add(entry);
290                    }              
291                    
292                } else {
293                    logger.warning("No time table found, trainID=" + trainID + " type=" + type);
294                }
295                
296                    
297                    return timetableBean;
298            }      
299            
300  }  }

Legend:
Removed from v.842  
changed lines
  Added in v.2081

  ViewVC Help
Powered by ViewVC 1.1.20