1 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
3 |
|
4 |
import java.net.URL; |
5 |
import java.util.HashMap; |
6 |
import java.util.Map; |
7 |
import java.util.logging.Level; |
8 |
import java.util.logging.Logger; |
9 |
|
10 |
import net.sf.jsr107cache.Cache; |
11 |
import net.sf.jsr107cache.CacheException; |
12 |
import net.sf.jsr107cache.CacheManager; |
13 |
|
14 |
import org.jsoup.nodes.Document; |
15 |
import org.jsoup.nodes.Element; |
16 |
import org.jsoup.select.Elements; |
17 |
|
18 |
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; |
19 |
|
20 |
import dk.thoerup.android.traininfo.common.MetroBean; |
21 |
import dk.thoerup.android.traininfo.common.MetroBean.MetroEntry; |
22 |
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
23 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
24 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
25 |
import dk.thoerup.traininfoservice.StationDAO; |
26 |
import dk.thoerup.traininfoservice.Statistics; |
27 |
|
28 |
|
29 |
|
30 |
public class MetroFetcher { |
31 |
|
32 |
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
33 |
StationDAO stationDAO = new StationDAO(); |
34 |
|
35 |
//Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable |
36 |
Cache cache; |
37 |
|
38 |
public MetroFetcher() { |
39 |
Map props = new HashMap(); |
40 |
props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, 60000); |
41 |
|
42 |
try { |
43 |
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
44 |
} catch (CacheException e) { |
45 |
logger.log(Level.WARNING, "error creating cache", e); |
46 |
} |
47 |
} |
48 |
|
49 |
public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { |
50 |
final String key = "metro:" + stationID; |
51 |
|
52 |
MetroBean metroBean = (MetroBean) cache.get(key); |
53 |
|
54 |
if (metroBean == null) { |
55 |
metroBean = lookupMetroDepartures(stationID); |
56 |
cache.put(key, metroBean); |
57 |
} else { |
58 |
Statistics.getInstance().incrementDepartureCacheHits(); |
59 |
logger.info("Metro: Cache hit " + key); //remove before production |
60 |
} |
61 |
return metroBean; |
62 |
} |
63 |
|
64 |
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
65 |
|
66 |
StationEntry station = stationDAO.getById(stationID) ; |
67 |
|
68 |
|
69 |
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |
70 |
logger.fine("URL:" + url); |
71 |
|
72 |
JsoupInvocation wrapper = new JsoupInvocation( url, 3000); |
73 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro"); |
74 |
|
75 |
Document doc = (Document) breaker.invoke(wrapper); |
76 |
|
77 |
MetroBean bean = new MetroBean(); |
78 |
|
79 |
bean.head = doc.getElementsByClass("metro-widget-headline").get(0).text(); |
80 |
|
81 |
Element content = doc.getElementsByClass("metro-widget-content").get(0); |
82 |
Elements contentElems = content.children(); |
83 |
|
84 |
Element departureTable = content.getElementsByTag("table").get(0); |
85 |
|
86 |
Elements rows = departureTable.getElementsByTag("tr"); |
87 |
|
88 |
for (int i=1; i<rows.size(); i++) { |
89 |
Element row = rows.get(i); |
90 |
Elements fields = row.children(); |
91 |
|
92 |
|
93 |
|
94 |
Elements metroFields = fields.get(0).children(); |
95 |
String metro =""; |
96 |
for (Element elm : metroFields) { |
97 |
metro = metro + elm.text() + " "; |
98 |
} |
99 |
|
100 |
|
101 |
|
102 |
String destination = fields.get(1).text(); |
103 |
|
104 |
Elements minuteFields = fields.get(2).children(); |
105 |
String minutes =""; |
106 |
for (Element elm : minuteFields) { |
107 |
minutes = minutes + elm.text() + " "; |
108 |
} |
109 |
|
110 |
MetroEntry entry = new MetroEntry(); |
111 |
entry.metro = metro.trim(); |
112 |
entry.destination = destination.trim(); |
113 |
entry.minutes = minutes.trim(); |
114 |
|
115 |
bean.entries.add(entry); |
116 |
} |
117 |
|
118 |
bean.operationInfo = contentElems.get(3).text(); |
119 |
|
120 |
for (int i=4; i<contentElems.size(); i++) { |
121 |
if (contentElems.get(i).text().trim().equalsIgnoreCase("køreplan")) { |
122 |
bean.plan = contentElems.get(i+1).text(); |
123 |
} |
124 |
} |
125 |
|
126 |
return bean; |
127 |
} |
128 |
|
129 |
} |