2 |
|
|
3 |
|
|
4 |
import java.net.URL; |
import java.net.URL; |
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
5 |
import java.util.Map; |
import java.util.Map; |
6 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
7 |
|
|
|
|
|
8 |
import org.jsoup.nodes.Document; |
import org.jsoup.nodes.Document; |
9 |
import org.jsoup.nodes.Element; |
import org.jsoup.nodes.Element; |
10 |
import org.jsoup.select.Elements; |
import org.jsoup.select.Elements; |
11 |
|
|
12 |
|
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 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
16 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
|
import dk.thoerup.traininfoservice.StationBean; |
|
17 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.traininfoservice.StationDAO; |
18 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
19 |
|
|
20 |
|
|
21 |
|
|
22 |
public class MetroFetcher { |
public class MetroFetcher { |
|
public static class MetroEntry { |
|
|
String metro; |
|
|
String destination; |
|
|
String minutes; |
|
|
} |
|
|
|
|
|
public static class MetroBean { |
|
|
String head; |
|
|
String operationInfo; //driftsinfo |
|
|
String plan; // koereplan |
|
|
|
|
|
List<MetroEntry> entries = new ArrayList<MetroEntry>(); |
|
|
} |
|
23 |
|
|
24 |
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
Logger logger = Logger.getLogger(MetroFetcher.class.getName()); |
25 |
StationDAO stationDAO = new StationDAO(); |
StationDAO stationDAO = new StationDAO(); |
26 |
|
|
27 |
Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(1000); |
Map<String, MetroBean> cache = new TimeoutMap<String,MetroBean>(60000); //TODO: make metro cache timeout configurable |
28 |
|
|
29 |
public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { |
public MetroBean cachedLookupMetroDepartures(int stationID) throws Exception { |
30 |
final String key = "metro:" + stationID; |
final String key = "metro:" + stationID; |
43 |
|
|
44 |
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
public MetroBean lookupMetroDepartures(int stationID) throws Exception { |
45 |
|
|
46 |
StationBean station = stationDAO.getById(stationID) ; |
StationEntry station = stationDAO.getById(stationID) ; |
47 |
|
|
48 |
|
|
49 |
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |
URL url = new URL("http://www.m.dk/layouts/Metro/Widgets/MetroWidget.ashx?StationId=" + station.getMetro() + "&Congestion=true&Application=MyMetro&Expiration=true"); |
50 |
|
logger.fine("URL:" + url); |
51 |
|
|
52 |
JsoupInvocation wrapper = new JsoupInvocation( url, 3000); |
JsoupInvocation wrapper = new JsoupInvocation( url, 3000); |
53 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("metro"); |
68 |
for (int i=1; i<rows.size(); i++) { |
for (int i=1; i<rows.size(); i++) { |
69 |
Element row = rows.get(i); |
Element row = rows.get(i); |
70 |
Elements fields = row.children(); |
Elements fields = row.children(); |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
Elements metroFields = fields.get(0).children(); |
75 |
|
String metro =""; |
76 |
|
for (Element elm : metroFields) { |
77 |
|
metro = metro + elm.text() + " "; |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
|
String metro = fields.get(0).text(); |
|
82 |
String destination = fields.get(1).text(); |
String destination = fields.get(1).text(); |
|
Elements minuteFields = fields.get(2).children(); |
|
|
String minutes =""; |
|
83 |
|
|
84 |
|
Elements minuteFields = fields.get(2).children(); |
85 |
|
String minutes =""; |
86 |
for (Element elm : minuteFields) { |
for (Element elm : minuteFields) { |
87 |
minutes = minutes + elm.text() + " "; |
minutes = minutes + elm.text() + " "; |
88 |
} |
} |
89 |
|
|
90 |
MetroEntry entry = new MetroEntry(); |
MetroEntry entry = new MetroEntry(); |
91 |
entry.metro = metro; |
entry.metro = metro.trim(); |
92 |
entry.destination = destination; |
entry.destination = destination.trim(); |
93 |
entry.minutes = minutes; |
entry.minutes = minutes.trim(); |
94 |
|
|
95 |
bean.entries.add(entry); |
bean.entries.add(entry); |
96 |
} |
} |
97 |
|
|
98 |
bean.operationInfo = contentElems.get(3).text(); |
bean.operationInfo = contentElems.get(3).text(); |
|
bean.plan = contentElems.get(5).text(); |
|
99 |
|
|
100 |
|
for (int i=4; i<contentElems.size(); i++) { |
101 |
|
if (contentElems.get(i).text().trim().equalsIgnoreCase("køreplan")) { |
102 |
|
bean.plan = contentElems.get(i+1).text(); |
103 |
|
} |
104 |
|
} |
105 |
|
|
106 |
return bean; |
return bean; |
107 |
} |
} |
108 |
|
|