1 |
torben |
305 |
package dk.thoerup.traininfoservice.banedk;
|
2 |
|
|
|
3 |
torben |
978 |
|
4 |
torben |
992 |
import java.net.URL;
|
5 |
torben |
994 |
import java.net.URLEncoder;
|
6 |
torben |
307 |
import java.util.Collections;
|
7 |
torben |
428 |
import java.util.Map;
|
8 |
torben |
348 |
import java.util.logging.Logger;
|
9 |
torben |
305 |
|
10 |
torben |
992 |
import org.jsoup.nodes.Document;
|
11 |
|
|
import org.jsoup.nodes.Element;
|
12 |
|
|
import org.jsoup.select.Elements;
|
13 |
torben |
305 |
|
14 |
torben |
468 |
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
15 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
16 |
torben |
588 |
import dk.thoerup.traininfoservice.StationBean;
|
17 |
|
|
import dk.thoerup.traininfoservice.StationDAO;
|
18 |
torben |
711 |
import dk.thoerup.traininfoservice.Statistics;
|
19 |
torben |
307 |
|
20 |
torben |
305 |
public class DepartureFetcher {
|
21 |
torben |
348 |
|
22 |
torben |
972 |
enum TrainType{
|
23 |
|
|
STOG,
|
24 |
|
|
REGIONAL
|
25 |
|
|
}
|
26 |
|
|
|
27 |
torben |
348 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
|
28 |
torben |
387 |
|
29 |
torben |
978 |
Map<String, DepartureBean> cache;
|
30 |
torben |
387 |
|
31 |
torben |
588 |
StationDAO stationDao = new StationDAO();
|
32 |
|
|
|
33 |
torben |
580 |
private boolean useTempSite;
|
34 |
torben |
1026 |
private int replyTimeout;
|
35 |
torben |
580 |
|
36 |
torben |
1026 |
public DepartureFetcher(boolean tempSite, int cacheTimeout, int replyTimeout) {
|
37 |
|
|
this.replyTimeout = replyTimeout;
|
38 |
torben |
580 |
useTempSite = tempSite;
|
39 |
torben |
978 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout);
|
40 |
torben |
580 |
}
|
41 |
|
|
|
42 |
|
|
|
43 |
torben |
307 |
|
44 |
torben |
387 |
|
45 |
torben |
978 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception {
|
46 |
torben |
829 |
final String key = "" + stationID + ":" + arrival;
|
47 |
torben |
980 |
|
48 |
torben |
978 |
DepartureBean departureBean = cache.get(key);
|
49 |
torben |
308 |
|
50 |
torben |
387 |
|
51 |
torben |
978 |
if (departureBean == null) {
|
52 |
|
|
departureBean = lookupDepartures(stationID,arrival);
|
53 |
|
|
cache.put(key, departureBean);
|
54 |
torben |
387 |
} else {
|
55 |
torben |
711 |
Statistics.getInstance().incrementDepartureCacheHits();
|
56 |
torben |
829 |
logger.info("Departure: Cache hit " + key); //remove before production
|
57 |
torben |
387 |
}
|
58 |
torben |
980 |
return departureBean;
|
59 |
torben |
387 |
}
|
60 |
|
|
|
61 |
|
|
|
62 |
torben |
978 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception {
|
63 |
torben |
307 |
|
64 |
torben |
978 |
DepartureBean departureBean = new DepartureBean();
|
65 |
|
|
|
66 |
torben |
588 |
StationBean station = stationDao.getById(stationID);
|
67 |
torben |
307 |
|
68 |
torben |
1021 |
departureBean.stationName = station.getName();
|
69 |
|
|
|
70 |
torben |
588 |
if (station.getRegional() != null) {
|
71 |
torben |
978 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
|
72 |
|
|
departureBean.departureEntries.addAll( tempBean.departureEntries );
|
73 |
|
|
departureBean.notifications.addAll(tempBean.notifications);
|
74 |
torben |
307 |
}
|
75 |
|
|
|
76 |
torben |
588 |
if (station.getStrain() != null) {
|
77 |
torben |
978 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
|
78 |
|
|
departureBean.departureEntries.addAll( tempBean.departureEntries );
|
79 |
|
|
departureBean.notifications.addAll(tempBean.notifications);
|
80 |
torben |
588 |
}
|
81 |
|
|
|
82 |
torben |
978 |
Collections.sort( departureBean.departureEntries );
|
83 |
torben |
588 |
|
84 |
|
|
|
85 |
torben |
978 |
return departureBean;
|
86 |
torben |
305 |
}
|
87 |
|
|
|
88 |
torben |
978 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
|
89 |
torben |
580 |
if (useTempSite == false) {
|
90 |
torben |
829 |
return lookupDeparturesNormalSite(stationcode, type, arrival);
|
91 |
torben |
580 |
} else {
|
92 |
torben |
972 |
//return lookupDeparturesFromTemporarySite(stationcode, type);
|
93 |
|
|
//TODO: find out what to to if they ever put a temp site up on trafikinfo.bane.dk
|
94 |
|
|
return null;
|
95 |
torben |
580 |
}
|
96 |
|
|
}
|
97 |
|
|
|
98 |
torben |
972 |
private String getTypeString(TrainType type) {
|
99 |
|
|
switch (type) {
|
100 |
|
|
case STOG:
|
101 |
|
|
return "S-Tog";
|
102 |
|
|
case REGIONAL:
|
103 |
|
|
return "Fjerntog";
|
104 |
|
|
default:
|
105 |
|
|
return ""; //Can not happen
|
106 |
|
|
}
|
107 |
|
|
}
|
108 |
|
|
|
109 |
torben |
978 |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception {
|
110 |
torben |
305 |
|
111 |
torben |
978 |
DepartureBean departureBean = new DepartureBean();
|
112 |
torben |
992 |
|
113 |
torben |
970 |
|
114 |
torben |
972 |
String typeString = getTypeString(type);
|
115 |
torben |
970 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
|
116 |
torben |
994 |
|
117 |
|
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
|
118 |
torben |
970 |
//String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
|
119 |
torben |
972 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";
|
120 |
torben |
970 |
|
121 |
torben |
994 |
|
122 |
|
|
|
123 |
torben |
992 |
//logger.info("URI: " + uri);
|
124 |
torben |
1026 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
|
125 |
torben |
421 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
126 |
torben |
305 |
|
127 |
torben |
992 |
Document page = (Document) breaker.invoke(wrapper);
|
128 |
torben |
305 |
|
129 |
torben |
829 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
|
130 |
torben |
992 |
Element table = page.getElementById(tableName);
|
131 |
torben |
829 |
|
132 |
torben |
342 |
if (table != null) {
|
133 |
torben |
992 |
Elements tableRows = table.getElementsByTag("tr");
|
134 |
torben |
342 |
|
135 |
torben |
1020 |
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
|
136 |
|
|
boolean passedTidsstreg = false;
|
137 |
|
|
|
138 |
torben |
992 |
for (Element currentRow : tableRows) {
|
139 |
|
|
String rowClass = currentRow.attr("class");
|
140 |
torben |
1020 |
|
141 |
|
|
if (tidsstregExists == true && passedTidsstreg == false) {
|
142 |
|
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
|
143 |
|
|
passedTidsstreg = true;
|
144 |
|
|
} else {
|
145 |
|
|
continue;
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
|
149 |
torben |
342 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
|
150 |
torben |
1020 |
|
151 |
torben |
992 |
Elements fields = currentRow.getElementsByTag("td");
|
152 |
torben |
342 |
|
153 |
torben |
978 |
DepartureEntry departure = new DepartureEntry();
|
154 |
torben |
342 |
|
155 |
torben |
992 |
String time = fields.get(0).text();
|
156 |
torben |
375 |
if (time.equals(""))
|
157 |
|
|
time = "0:00"; //Bane.dk bug work-around
|
158 |
torben |
342 |
departure.setTime(time);
|
159 |
|
|
|
160 |
|
|
int updated = extractUpdated( fields.get(1) );
|
161 |
|
|
departure.setUpdated(updated);
|
162 |
|
|
|
163 |
torben |
992 |
String trainNumber = fields.get(2).text();
|
164 |
torben |
972 |
if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
|
165 |
torben |
349 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
|
166 |
torben |
342 |
departure.setTrainNumber(trainNumber);
|
167 |
|
|
|
168 |
torben |
992 |
String destination = fields.get(3).text();
|
169 |
torben |
342 |
departure.setDestination(destination);
|
170 |
|
|
|
171 |
torben |
992 |
String origin = fields.get(4).text();
|
172 |
torben |
342 |
departure.setOrigin(origin);
|
173 |
|
|
|
174 |
torben |
992 |
String location = fields.get(5).text();
|
175 |
torben |
342 |
departure.setLocation(location);
|
176 |
|
|
|
177 |
torben |
992 |
String status = fields.get(6).text().trim();
|
178 |
torben |
342 |
departure.setStatus(status);
|
179 |
|
|
|
180 |
|
|
String note = extractNote( fields.get(7) );
|
181 |
|
|
departure.setNote(note);
|
182 |
|
|
|
183 |
torben |
972 |
departure.setType(typeString);
|
184 |
torben |
697 |
|
185 |
torben |
978 |
departureBean.departureEntries.add( departure );
|
186 |
torben |
342 |
}
|
187 |
|
|
}
|
188 |
torben |
348 |
} else {
|
189 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type);
|
190 |
torben |
305 |
}
|
191 |
torben |
978 |
|
192 |
torben |
992 |
Element notifDiv = page.getElementById("station_planlagte_text");
|
193 |
torben |
978 |
if (notifDiv != null) {
|
194 |
|
|
|
195 |
torben |
992 |
Elements tables = notifDiv.getElementsByTag("table");
|
196 |
|
|
for (Element tab : tables) {
|
197 |
torben |
978 |
|
198 |
torben |
992 |
Elements anchors = tab.getElementsByTag("a");
|
199 |
torben |
978 |
if (anchors.size() == 2) {
|
200 |
torben |
992 |
departureBean.notifications.add( anchors.get(1).text() );
|
201 |
torben |
978 |
}
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
}
|
205 |
|
|
|
206 |
|
|
|
207 |
|
|
return departureBean;
|
208 |
torben |
305 |
}
|
209 |
|
|
|
210 |
torben |
974 |
/*
|
211 |
|
|
@Deprecated
|
212 |
torben |
580 |
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception {
|
213 |
|
|
|
214 |
|
|
List<DepartureBean> departureList = new ArrayList<DepartureBean>();
|
215 |
|
|
|
216 |
torben |
591 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3);
|
217 |
torben |
580 |
webClient.setTimeout(2500);
|
218 |
|
|
webClient.setJavaScriptEnabled(false);
|
219 |
|
|
|
220 |
|
|
|
221 |
|
|
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode;
|
222 |
|
|
|
223 |
torben |
939 |
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri);
|
224 |
torben |
580 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
225 |
|
|
|
226 |
|
|
HtmlPage page = (HtmlPage) breaker.invoke(wrapper);
|
227 |
|
|
|
228 |
|
|
HtmlElement table = page.getElementById("traf_afgang");
|
229 |
|
|
|
230 |
|
|
if (table != null) {
|
231 |
|
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
|
232 |
|
|
|
233 |
|
|
boolean isFirst = true;
|
234 |
|
|
|
235 |
|
|
for (HtmlElement currentRow : tableRows) {
|
236 |
|
|
if (isFirst == true) { //skip table headers
|
237 |
|
|
isFirst = false;
|
238 |
|
|
continue;
|
239 |
|
|
}
|
240 |
|
|
|
241 |
|
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
|
242 |
|
|
|
243 |
|
|
DepartureBean departure = new DepartureBean();
|
244 |
|
|
|
245 |
|
|
String time = fields.get(0).asText().trim();
|
246 |
torben |
583 |
|
247 |
torben |
580 |
if (time.equals(""))
|
248 |
|
|
time = "0:00"; //Bane.dk bug work-around
|
249 |
|
|
departure.setTime(time);
|
250 |
|
|
|
251 |
|
|
|
252 |
|
|
String trainNumber = fields.get(1).asText();
|
253 |
|
|
departure.setTrainNumber(trainNumber);
|
254 |
|
|
|
255 |
|
|
String destination = fields.get(2).asText();
|
256 |
|
|
departure.setDestination(destination);
|
257 |
|
|
|
258 |
|
|
String origin = fields.get(3).asText();
|
259 |
|
|
departure.setOrigin(origin);
|
260 |
|
|
|
261 |
|
|
String status = fields.get(4).asText();
|
262 |
|
|
departure.setStatus(status);
|
263 |
|
|
|
264 |
torben |
582 |
String note = fields.get(5).asText();
|
265 |
torben |
580 |
departure.setNote(note);
|
266 |
|
|
|
267 |
|
|
departureList.add(departure);
|
268 |
|
|
}
|
269 |
|
|
} else {
|
270 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type);
|
271 |
|
|
}
|
272 |
torben |
591 |
webClient.closeAllWindows();
|
273 |
torben |
580 |
|
274 |
torben |
591 |
|
275 |
torben |
580 |
return departureList;
|
276 |
torben |
974 |
}*/
|
277 |
torben |
580 |
|
278 |
|
|
|
279 |
torben |
992 |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
|
280 |
torben |
305 |
int updated = -1;
|
281 |
|
|
|
282 |
torben |
992 |
Elements updatedImgs = updatedTd.getElementsByTag("img");
|
283 |
|
|
String updatedStr = updatedImgs.get(0).attr("src");
|
284 |
torben |
305 |
|
285 |
|
|
if (updatedStr != null) {
|
286 |
|
|
for (int i=0; i<updatedStr.length(); i++) {
|
287 |
|
|
char c = updatedStr.charAt(i);
|
288 |
|
|
if ( Character.isDigit(c)) {
|
289 |
|
|
updated = Character.digit(c, 10);
|
290 |
|
|
break;
|
291 |
|
|
}
|
292 |
|
|
}
|
293 |
|
|
}
|
294 |
|
|
return updated;
|
295 |
|
|
}
|
296 |
|
|
|
297 |
torben |
992 |
private String extractNote(Element noteTd) {
|
298 |
|
|
String note = noteTd.text().trim();
|
299 |
torben |
313 |
|
300 |
torben |
992 |
|
301 |
|
|
Elements elems = noteTd.getElementsByClass("bemtype");
|
302 |
torben |
313 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
|
303 |
|
|
note = note.substring(0,note.length() -1 );
|
304 |
|
|
|
305 |
|
|
return note;
|
306 |
|
|
}
|
307 |
|
|
|
308 |
torben |
992 |
private String extractTrainNumber(Element trainTd) {
|
309 |
|
|
Element anchorElement = trainTd.getElementsByTag("a").get(0);
|
310 |
|
|
String href = anchorElement.attr("href");
|
311 |
torben |
349 |
|
312 |
torben |
973 |
int pos = href.lastIndexOf('/');
|
313 |
|
|
String number = href.substring(pos+1);
|
314 |
torben |
970 |
|
315 |
torben |
349 |
return number;
|
316 |
|
|
}
|
317 |
|
|
|
318 |
torben |
305 |
//test
|
319 |
torben |
580 |
/*
|
320 |
torben |
451 |
public static void main(String args[]) throws Exception {
|
321 |
torben |
305 |
DepartureFetcher f = new DepartureFetcher();
|
322 |
torben |
307 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
|
323 |
torben |
305 |
for(DepartureBean d : deps) {
|
324 |
|
|
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
|
325 |
|
|
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
System.out.println("--------------------------");
|
329 |
torben |
580 |
}*/
|
330 |
torben |
305 |
}
|