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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1043 - (hide annotations) (download)
Mon Sep 13 18:53:30 2010 UTC (13 years, 8 months ago) by torben
File size: 3232 byte(s)
Make metro plan extraction works in all cases ... and increase cache timeout to 1 minute
1 torben 1042 package dk.thoerup.traininfoservice.banedk;
2    
3    
4     import java.net.URL;
5    
6     import java.util.ArrayList;
7     import java.util.List;
8     import java.util.Map;
9     import java.util.logging.Logger;
10    
11    
12     import org.jsoup.nodes.Document;
13     import org.jsoup.nodes.Element;
14     import org.jsoup.select.Elements;
15    
16     import dk.thoerup.circuitbreaker.CircuitBreaker;
17     import dk.thoerup.circuitbreaker.CircuitBreakerManager;
18     import dk.thoerup.traininfoservice.StationBean;
19     import dk.thoerup.traininfoservice.StationDAO;
20     import dk.thoerup.traininfoservice.Statistics;
21    
22    
23    
24     public class MetroFetcher {
25     public static class MetroEntry {
26     String metro;
27     String destination;
28     String minutes;
29     }
30    
31     public static class MetroBean {
32     String head;
33     String operationInfo; //driftsinfo
34     String plan; // koereplan
35    
36     List<MetroEntry> entries = new ArrayList<MetroEntry>();
37     }
38    
39     Logger logger = Logger.getLogger(MetroFetcher.class.getName());
40     StationDAO stationDAO = new StationDAO();
41    
42 torben 1043 Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable
43 torben 1042
44     public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception {
45     final String key = "metro:" + stationID;
46    
47     MetroBean metroBean = cache.get(key);
48    
49     if (metroBean == null) {
50     metroBean = lookupMetroDepartures(stationID);
51     cache.put(key, metroBean);
52     } else {
53     Statistics.getInstance().incrementDepartureCacheHits();
54     logger.info("Metro: Cache hit " + key); //remove before production
55     }
56     return metroBean;
57     }
58    
59     public MetroBean lookupMetroDepartures(int stationID) throws Exception {
60    
61     StationBean station = stationDAO.getById(stationID) ;
62    
63    
64     URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true");
65    
66     JsoupInvocation wrapper = new JsoupInvocation( url, 3000);
67     CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro");
68    
69     Document doc = (Document) breaker.invoke(wrapper);
70    
71     MetroBean bean = new MetroBean();
72    
73     bean.head = doc.getElementsByClass("metro-widget-headline").get(0).text();
74    
75     Element content = doc.getElementsByClass("metro-widget-content").get(0);
76     Elements contentElems = content.children();
77    
78     Element departureTable = content.getElementsByTag("table").get(0);
79    
80     Elements rows = departureTable.getElementsByTag("tr");
81    
82     for (int i=1; i<rows.size(); i++) {
83     Element row = rows.get(i);
84     Elements fields = row.children();
85    
86     String metro = fields.get(0).text();
87     String destination = fields.get(1).text();
88     Elements minuteFields = fields.get(2).children();
89     String minutes ="";
90    
91     for (Element elm : minuteFields) {
92     minutes = minutes + elm.text() + " ";
93     }
94    
95     MetroEntry entry = new MetroEntry();
96     entry.metro = metro;
97     entry.destination = destination;
98     entry.minutes = minutes;
99    
100     bean.entries.add(entry);
101     }
102    
103 torben 1043 bean.operationInfo = contentElems.get(3).text();
104 torben 1042
105 torben 1043 int planIndex;
106     if (contentElems.get(4).text().trim().equalsIgnoreCase("stationsinformation")) {
107     planIndex = 7;
108     } else {
109     planIndex = 5;
110     }
111    
112     bean.plan = contentElems.get(planIndex).text();
113    
114 torben 1042 return bean;
115     }
116    
117     }

  ViewVC Help
Powered by ViewVC 1.1.20