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