1 |
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 |
Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable |
43 |
|
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 |
logger.fine("URL:" + url); |
66 |
|
67 |
JsoupInvocation wrapper = new JsoupInvocation( url, 3000); |
68 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro"); |
69 |
|
70 |
Document doc = (Document) breaker.invoke(wrapper); |
71 |
|
72 |
MetroBean bean = new MetroBean(); |
73 |
|
74 |
bean.head = doc.getElementsByClass("metro-widget-headline").get(0).text(); |
75 |
|
76 |
Element content = doc.getElementsByClass("metro-widget-content").get(0); |
77 |
Elements contentElems = content.children(); |
78 |
|
79 |
Element departureTable = content.getElementsByTag("table").get(0); |
80 |
|
81 |
Elements rows = departureTable.getElementsByTag("tr"); |
82 |
|
83 |
for (int i=1; i<rows.size(); i++) { |
84 |
Element row = rows.get(i); |
85 |
Elements fields = row.children(); |
86 |
|
87 |
String metro = fields.get(0).text(); |
88 |
String destination = fields.get(1).text(); |
89 |
Elements minuteFields = fields.get(2).children(); |
90 |
String minutes =""; |
91 |
|
92 |
for (Element elm : minuteFields) { |
93 |
minutes = minutes + elm.text() + " "; |
94 |
} |
95 |
|
96 |
MetroEntry entry = new MetroEntry(); |
97 |
entry.metro = metro; |
98 |
entry.destination = destination; |
99 |
entry.minutes = minutes; |
100 |
|
101 |
bean.entries.add(entry); |
102 |
} |
103 |
|
104 |
bean.operationInfo = contentElems.get(3).text(); |
105 |
|
106 |
for (int i=0; i<contentElems.size(); i++) { |
107 |
if (contentElems.get(i).text().trim().equalsIgnoreCase("køreplan")) { |
108 |
bean.plan = contentElems.get(i+1).text(); |
109 |
} |
110 |
} |
111 |
|
112 |
return bean; |
113 |
} |
114 |
|
115 |
} |