1 |
torben |
1042 |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
3 |
|
|
|
4 |
|
|
import java.net.URL; |
5 |
|
|
import java.util.Map; |
6 |
|
|
import java.util.logging.Logger; |
7 |
|
|
|
8 |
|
|
import org.jsoup.nodes.Document; |
9 |
|
|
import org.jsoup.nodes.Element; |
10 |
|
|
import org.jsoup.select.Elements; |
11 |
|
|
|
12 |
torben |
1061 |
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 |
torben |
1042 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
16 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
17 |
torben |
1355 |
import dk.thoerup.genericjavautils.TimeoutMap; |
18 |
torben |
1042 |
import dk.thoerup.traininfoservice.Statistics; |
19 |
torben |
1255 |
import dk.thoerup.traininfoservice.db.StationDAO; |
20 |
torben |
1042 |
|
21 |
|
|
|
22 |
|
|
|
23 |
|
|
public class MetroFetcher { |
24 |
|
|
|
25 |
|
|
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
26 |
|
|
StationDAO stationDAO = new StationDAO(); |
27 |
|
|
|
28 |
torben |
1043 |
Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable |
29 |
torben |
1042 |
|
30 |
|
|
public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { |
31 |
|
|
final String key = "metro:" + stationID; |
32 |
|
|
|
33 |
|
|
MetroBean metroBean = cache.get(key); |
34 |
|
|
|
35 |
|
|
if (metroBean == null) { |
36 |
|
|
metroBean = lookupMetroDepartures(stationID); |
37 |
|
|
cache.put(key, metroBean); |
38 |
|
|
} else { |
39 |
|
|
Statistics.getInstance().incrementDepartureCacheHits(); |
40 |
|
|
logger.info("Metro: Cache hit " + key); //remove before production |
41 |
|
|
} |
42 |
|
|
return metroBean; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
46 |
|
|
|
47 |
torben |
1060 |
StationEntry station = stationDAO.getById(stationID) ; |
48 |
torben |
1042 |
|
49 |
|
|
|
50 |
|
|
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |
51 |
torben |
1048 |
logger.fine("URL:" + url); |
52 |
torben |
1042 |
|
53 |
|
|
JsoupInvocation wrapper = new JsoupInvocation( url, 3000); |
54 |
|
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro"); |
55 |
|
|
|
56 |
|
|
Document doc = (Document) breaker.invoke(wrapper); |
57 |
|
|
|
58 |
|
|
MetroBean bean = new MetroBean(); |
59 |
|
|
|
60 |
|
|
bean.head = doc.getElementsByClass("metro-widget-headline").get(0).text(); |
61 |
|
|
|
62 |
|
|
Element content = doc.getElementsByClass("metro-widget-content").get(0); |
63 |
|
|
Elements contentElems = content.children(); |
64 |
|
|
|
65 |
torben |
1147 |
Elements contentTables = content.getElementsByTag("table"); |
66 |
|
|
if ( contentTables.size() > 0 ) { |
67 |
torben |
1042 |
|
68 |
torben |
1147 |
Element departureTable = content.getElementsByTag("table").get(0); |
69 |
|
|
|
70 |
|
|
Elements rows = departureTable.getElementsByTag("tr"); |
71 |
|
|
|
72 |
|
|
for (int i=1; i<rows.size(); i++) { |
73 |
|
|
Element row = rows.get(i); |
74 |
|
|
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 |
torben |
1056 |
} |
101 |
torben |
1147 |
} |
102 |
|
|
|
103 |
|
|
|
104 |
|
|
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 |
torben |
1042 |
} |
109 |
|
|
|
110 |
torben |
1147 |
if (currentText.equalsIgnoreCase("køreplan")) { |
111 |
torben |
1050 |
bean.plan = contentElems.get(i+1).text(); |
112 |
|
|
} |
113 |
torben |
1043 |
} |
114 |
torben |
1147 |
|
115 |
torben |
1050 |
|
116 |
torben |
1147 |
|
117 |
torben |
1042 |
return bean; |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
} |