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

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

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

revision 1042 by torben, Mon Sep 13 17:57:31 2010 UTC revision 1355 by torben, Wed Apr 20 19:01:33 2011 UTC
# Line 2  package dk.thoerup.traininfoservice.bane Line 2  package dk.thoerup.traininfoservice.bane
2    
3    
4  import java.net.URL;  import java.net.URL;
   
 import java.util.ArrayList;  
 import java.util.List;  
5  import java.util.Map;  import java.util.Map;
6  import java.util.logging.Logger;  import java.util.logging.Logger;
7    
   
8  import org.jsoup.nodes.Document;  import org.jsoup.nodes.Document;
9  import org.jsoup.nodes.Element;  import org.jsoup.nodes.Element;
10  import org.jsoup.select.Elements;  import org.jsoup.select.Elements;
11    
12    import dk.thoerup.android.traininfo.common.MetroBean;
13    import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry;
14    import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
15  import dk.thoerup.circuitbreaker.CircuitBreaker;  import dk.thoerup.circuitbreaker.CircuitBreaker;
16  import dk.thoerup.circuitbreaker.CircuitBreakerManager;  import dk.thoerup.circuitbreaker.CircuitBreakerManager;
17  import dk.thoerup.traininfoservice.StationBean;  import dk.thoerup.genericjavautils.TimeoutMap;
 import dk.thoerup.traininfoservice.StationDAO;  
18  import dk.thoerup.traininfoservice.Statistics;  import dk.thoerup.traininfoservice.Statistics;
19    import dk.thoerup.traininfoservice.db.StationDAO;
20    
21    
22    
23  public class MetroFetcher {  public class MetroFetcher {
         public static class MetroEntry {  
                 String metro;  
                 String destination;  
                 String minutes;          
         }  
           
         public static class MetroBean {  
                 String head;  
                 String operationInfo; //driftsinfo  
                 String plan; // koereplan  
                   
                 List<MetroEntry> entries = new ArrayList<MetroEntry>();  
         }  
24                    
25          Logger logger = Logger.getLogger(MetroFetcher.class.getName());          Logger logger = Logger.getLogger(MetroFetcher.class.getName());
26          StationDAO stationDAO = new StationDAO();          StationDAO stationDAO = new StationDAO();
27                    
28          Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(1000);          Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable
29                    
30          public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception {          public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception {
31                  final String key = "metro:" + stationID;                  final String key = "metro:" + stationID;
# Line 58  public class MetroFetcher { Line 44  public class MetroFetcher {
44    
45          public MetroBean lookupMetroDepartures(int stationID) throws Exception {          public MetroBean lookupMetroDepartures(int stationID) throws Exception {
46                                    
47                  StationBean station = stationDAO.getById(stationID) ;                  StationEntry station = stationDAO.getById(stationID) ;
48                                                    
49                                    
50                  URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");                  URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");
51                    logger.fine("URL:" + url);
52                                    
53              JsoupInvocation wrapper = new JsoupInvocation( url, 3000);              JsoupInvocation wrapper = new JsoupInvocation( url, 3000);
54              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro");              CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro");
# Line 75  public class MetroFetcher { Line 62  public class MetroFetcher {
62                  Element content = doc.getElementsByClass("metro-widget-content").get(0);                  Element content = doc.getElementsByClass("metro-widget-content").get(0);
63                  Elements contentElems = content.children();                  Elements contentElems = content.children();
64                                                                                                                    
65                  Element departureTable = content.getElementsByTag("table").get(0);                  Elements contentTables = content.getElementsByTag("table");
66                    if ( contentTables.size() > 0 ) {
                 Elements rows = departureTable.getElementsByTag("tr");  
   
                 for (int i=1; i<rows.size(); i++) {  
                         Element row = rows.get(i);        
                         Elements fields = row.children();  
67                                                    
68                          String metro = fields.get(0).text();                          Element departureTable = content.getElementsByTag("table").get(0);
69                          String destination = fields.get(1).text();          
70                          Elements minuteFields = fields.get(2).children();                          Elements rows = departureTable.getElementsByTag("tr");
71                          String minutes ="";          
72                                                    for (int i=1; i<rows.size(); i++) {
73                          for (Element elm : minuteFields) {                                  Element row = rows.get(i);      
74                                  minutes = minutes + elm.text() + " ";                                  Elements fields = row.children();
75            
76                                    
77                                    
78                                    Elements metroFields = fields.get(0).children();                        
79                                    String metro ="";                      
80                                    for (Element elm : metroFields) {
81                                            metro = metro + elm.text() + " ";
82                                    }
83                                    
84                                    
85                                    
86                                    String destination = fields.get(1).text();
87                                    
88                                    Elements minuteFields = fields.get(2).children();                      
89                                    String minutes ="";                    
90                                    for (Element elm : minuteFields) {
91                                            minutes = minutes + elm.text() + " ";
92                                    }
93                                    
94                                    MetroEntry entry = new MetroEntry();
95                                    entry.metro = metro.trim();
96                                    entry.destination = destination.trim();
97                                    entry.minutes = minutes.trim();
98                                    
99                                    bean.entries.add(entry);                        
100                          }                          }
                           
                         MetroEntry entry = new MetroEntry();  
                         entry.metro = metro;  
                         entry.destination = destination;  
                         entry.minutes = minutes;  
                           
                         bean.entries.add(entry);                          
101                  }                  }
102                                                                    
103                  bean.operationInfo = contentElems.get(3).text();                  
104                  bean.plan = contentElems.get(5).text();                  for (int i=0; i<contentElems.size(); i++) {
105                            String currentText = contentElems.get(i).text().trim();
106                            if (currentText.equalsIgnoreCase("driftsinformation")) {                                
107                                    bean.operationInfo = contentElems.get(i+1).text();
108                            }
109                            
110                            if (currentText.equalsIgnoreCase("køreplan")) {                                
111                                    bean.plan = contentElems.get(i+1).text();
112                            }
113                    }
114                                    
115    
116    
117                  return bean;                  return bean;
118          }          }
119                    

Legend:
Removed from v.1042  
changed lines
  Added in v.1355

  ViewVC Help
Powered by ViewVC 1.1.20