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 |
1061 |
import dk.thoerup.android.traininfo.common.DepartureBean;
|
15 |
|
|
import dk.thoerup.android.traininfo.common.DepartureEntry;
|
16 |
|
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry;
|
17 |
torben |
468 |
import dk.thoerup.circuitbreaker.CircuitBreaker;
|
18 |
|
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager;
|
19 |
torben |
588 |
import dk.thoerup.traininfoservice.StationDAO;
|
20 |
torben |
711 |
import dk.thoerup.traininfoservice.Statistics;
|
21 |
torben |
307 |
|
22 |
torben |
305 |
public class DepartureFetcher {
|
23 |
torben |
348 |
|
24 |
torben |
972 |
enum TrainType{
|
25 |
|
|
STOG,
|
26 |
|
|
REGIONAL
|
27 |
|
|
}
|
28 |
|
|
|
29 |
torben |
348 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
|
30 |
torben |
387 |
|
31 |
torben |
978 |
Map<String, DepartureBean> cache;
|
32 |
torben |
387 |
|
33 |
torben |
588 |
StationDAO stationDao = new StationDAO();
|
34 |
|
|
|
35 |
torben |
1034 |
private boolean useAzureSite;
|
36 |
torben |
1026 |
private int replyTimeout;
|
37 |
torben |
580 |
|
38 |
torben |
1034 |
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) {
|
39 |
torben |
1026 |
this.replyTimeout = replyTimeout;
|
40 |
torben |
1034 |
useAzureSite = azureSite;
|
41 |
torben |
978 |
cache = new TimeoutMap<String,DepartureBean>(cacheTimeout);
|
42 |
torben |
580 |
}
|
43 |
|
|
|
44 |
|
|
|
45 |
torben |
307 |
|
46 |
torben |
387 |
|
47 |
torben |
978 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception {
|
48 |
torben |
829 |
final String key = "" + stationID + ":" + arrival;
|
49 |
torben |
980 |
|
50 |
torben |
978 |
DepartureBean departureBean = cache.get(key);
|
51 |
torben |
308 |
|
52 |
torben |
387 |
|
53 |
torben |
978 |
if (departureBean == null) {
|
54 |
|
|
departureBean = lookupDepartures(stationID,arrival);
|
55 |
|
|
cache.put(key, departureBean);
|
56 |
torben |
387 |
} else {
|
57 |
torben |
711 |
Statistics.getInstance().incrementDepartureCacheHits();
|
58 |
torben |
829 |
logger.info("Departure: Cache hit " + key); //remove before production
|
59 |
torben |
387 |
}
|
60 |
torben |
980 |
return departureBean;
|
61 |
torben |
387 |
}
|
62 |
|
|
|
63 |
|
|
|
64 |
torben |
978 |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception {
|
65 |
torben |
307 |
|
66 |
torben |
978 |
DepartureBean departureBean = new DepartureBean();
|
67 |
|
|
|
68 |
torben |
1060 |
StationEntry station = stationDao.getById(stationID);
|
69 |
torben |
307 |
|
70 |
torben |
1021 |
departureBean.stationName = station.getName();
|
71 |
|
|
|
72 |
torben |
588 |
if (station.getRegional() != null) {
|
73 |
torben |
978 |
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival);
|
74 |
torben |
1063 |
departureBean.entries.addAll( tempBean.entries );
|
75 |
torben |
978 |
departureBean.notifications.addAll(tempBean.notifications);
|
76 |
torben |
307 |
}
|
77 |
|
|
|
78 |
torben |
588 |
if (station.getStrain() != null) {
|
79 |
torben |
978 |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival);
|
80 |
torben |
1063 |
departureBean.entries.addAll( tempBean.entries );
|
81 |
torben |
978 |
departureBean.notifications.addAll(tempBean.notifications);
|
82 |
torben |
588 |
}
|
83 |
|
|
|
84 |
torben |
1063 |
if (departureBean.entries.size() == 0) {
|
85 |
torben |
1037 |
logger.info("No departures found for station " + stationID);
|
86 |
|
|
}
|
87 |
|
|
|
88 |
torben |
1063 |
Collections.sort( departureBean.entries );
|
89 |
torben |
588 |
|
90 |
|
|
|
91 |
torben |
978 |
return departureBean;
|
92 |
torben |
305 |
}
|
93 |
|
|
|
94 |
torben |
978 |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception {
|
95 |
torben |
1034 |
if (useAzureSite == true) {
|
96 |
|
|
return lookupDeparturesAzureSite(stationcode, type, arrival);
|
97 |
|
|
} else {
|
98 |
|
|
return lookupDeparturesWwwSite(stationcode, type, arrival);
|
99 |
torben |
580 |
}
|
100 |
|
|
}
|
101 |
|
|
|
102 |
torben |
1034 |
private String getTypeStringAzure(TrainType type) {
|
103 |
torben |
972 |
switch (type) {
|
104 |
|
|
case STOG:
|
105 |
|
|
return "S-Tog";
|
106 |
|
|
case REGIONAL:
|
107 |
|
|
return "Fjerntog";
|
108 |
|
|
default:
|
109 |
|
|
return ""; //Can not happen
|
110 |
|
|
}
|
111 |
|
|
}
|
112 |
|
|
|
113 |
torben |
1034 |
private String getTypeStringWww(TrainType type) {
|
114 |
|
|
switch (type) {
|
115 |
|
|
case STOG:
|
116 |
|
|
return "S2";
|
117 |
|
|
case REGIONAL:
|
118 |
|
|
return "FJRN";
|
119 |
|
|
default:
|
120 |
|
|
return ""; //Can not happen
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
|
|
|
124 |
|
|
public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception {
|
125 |
torben |
305 |
|
126 |
torben |
978 |
DepartureBean departureBean = new DepartureBean();
|
127 |
torben |
992 |
|
128 |
torben |
970 |
|
129 |
torben |
1034 |
String typeString = getTypeStringAzure(type);
|
130 |
torben |
970 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst";
|
131 |
torben |
994 |
|
132 |
|
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1");
|
133 |
torben |
970 |
|
134 |
torben |
1034 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning";
|
135 |
torben |
994 |
|
136 |
torben |
1048 |
logger.fine("URI: " + uri);
|
137 |
torben |
1026 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
|
138 |
torben |
421 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
139 |
torben |
305 |
|
140 |
torben |
992 |
Document page = (Document) breaker.invoke(wrapper);
|
141 |
torben |
305 |
|
142 |
torben |
829 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
|
143 |
torben |
992 |
Element table = page.getElementById(tableName);
|
144 |
torben |
829 |
|
145 |
torben |
342 |
if (table != null) {
|
146 |
torben |
992 |
Elements tableRows = table.getElementsByTag("tr");
|
147 |
torben |
342 |
|
148 |
torben |
1188 |
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
|
149 |
|
|
//boolean passedTidsstreg = false;
|
150 |
torben |
1020 |
|
151 |
torben |
992 |
for (Element currentRow : tableRows) {
|
152 |
|
|
String rowClass = currentRow.attr("class");
|
153 |
torben |
1188 |
/*
|
154 |
torben |
1020 |
if (tidsstregExists == true && passedTidsstreg == false) {
|
155 |
|
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
|
156 |
|
|
passedTidsstreg = true;
|
157 |
|
|
} else {
|
158 |
|
|
continue;
|
159 |
|
|
}
|
160 |
torben |
1188 |
}*/
|
161 |
torben |
1020 |
|
162 |
torben |
342 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
|
163 |
torben |
1020 |
|
164 |
torben |
992 |
Elements fields = currentRow.getElementsByTag("td");
|
165 |
torben |
342 |
|
166 |
torben |
978 |
DepartureEntry departure = new DepartureEntry();
|
167 |
torben |
342 |
|
168 |
torben |
992 |
String time = fields.get(0).text();
|
169 |
torben |
375 |
if (time.equals(""))
|
170 |
|
|
time = "0:00"; //Bane.dk bug work-around
|
171 |
torben |
342 |
departure.setTime(time);
|
172 |
|
|
|
173 |
|
|
int updated = extractUpdated( fields.get(1) );
|
174 |
|
|
departure.setUpdated(updated);
|
175 |
|
|
|
176 |
torben |
992 |
String trainNumber = fields.get(2).text();
|
177 |
torben |
972 |
if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber
|
178 |
torben |
1039 |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));
|
179 |
torben |
342 |
departure.setTrainNumber(trainNumber);
|
180 |
|
|
|
181 |
torben |
992 |
String destination = fields.get(3).text();
|
182 |
torben |
342 |
departure.setDestination(destination);
|
183 |
|
|
|
184 |
torben |
992 |
String origin = fields.get(4).text();
|
185 |
torben |
342 |
departure.setOrigin(origin);
|
186 |
|
|
|
187 |
torben |
992 |
String location = fields.get(5).text();
|
188 |
torben |
342 |
departure.setLocation(location);
|
189 |
|
|
|
190 |
torben |
992 |
String status = fields.get(6).text().trim();
|
191 |
torben |
342 |
departure.setStatus(status);
|
192 |
|
|
|
193 |
|
|
String note = extractNote( fields.get(7) );
|
194 |
|
|
departure.setNote(note);
|
195 |
|
|
|
196 |
torben |
972 |
departure.setType(typeString);
|
197 |
torben |
697 |
|
198 |
torben |
1063 |
departureBean.entries.add( departure );
|
199 |
torben |
342 |
}
|
200 |
|
|
}
|
201 |
torben |
348 |
} else {
|
202 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type);
|
203 |
torben |
305 |
}
|
204 |
torben |
978 |
|
205 |
torben |
992 |
Element notifDiv = page.getElementById("station_planlagte_text");
|
206 |
torben |
978 |
if (notifDiv != null) {
|
207 |
|
|
|
208 |
torben |
992 |
Elements tables = notifDiv.getElementsByTag("table");
|
209 |
|
|
for (Element tab : tables) {
|
210 |
torben |
978 |
|
211 |
torben |
992 |
Elements anchors = tab.getElementsByTag("a");
|
212 |
torben |
978 |
if (anchors.size() == 2) {
|
213 |
torben |
992 |
departureBean.notifications.add( anchors.get(1).text() );
|
214 |
torben |
978 |
}
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
}
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
return departureBean;
|
221 |
torben |
305 |
}
|
222 |
|
|
|
223 |
torben |
1038 |
|
224 |
|
|
|
225 |
torben |
1040 |
public static String cleanText(String input) {
|
226 |
|
|
//apparently JSoup translates characters on www.bane.dk to 0xA0
|
227 |
torben |
1038 |
return input.replace((char) 0xA0, (char)0x20).trim();
|
228 |
|
|
}
|
229 |
|
|
|
230 |
torben |
1034 |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception {
|
231 |
torben |
580 |
|
232 |
torben |
1034 |
DepartureBean departureBean = new DepartureBean();
|
233 |
torben |
580 |
|
234 |
torben |
1034 |
String type = getTypeStringWww(trainType);
|
235 |
|
|
|
236 |
torben |
1045 |
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1");
|
237 |
|
|
|
238 |
torben |
1034 |
|
239 |
|
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode;
|
240 |
torben |
1048 |
logger.fine("URI:" + uri);
|
241 |
|
|
|
242 |
torben |
1047 |
|
243 |
torben |
1034 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout);
|
244 |
torben |
580 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk");
|
245 |
|
|
|
246 |
torben |
1034 |
Element page = (Element) breaker.invoke(wrapper);
|
247 |
torben |
580 |
|
248 |
torben |
1034 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel";
|
249 |
|
|
Element table = page.getElementById(tableName);
|
250 |
torben |
580 |
|
251 |
torben |
1046 |
|
252 |
|
|
|
253 |
torben |
1034 |
if (table != null) {
|
254 |
|
|
Elements tableRows = table.getElementsByTag("tr");
|
255 |
torben |
580 |
|
256 |
torben |
1188 |
//boolean passedTidsstreg = false;
|
257 |
|
|
//boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0);
|
258 |
torben |
1046 |
|
259 |
torben |
1034 |
for (Element currentRow : tableRows) {
|
260 |
|
|
String rowClass = currentRow.attr("class");
|
261 |
torben |
1188 |
/*
|
262 |
torben |
1046 |
if (tidsstregExists == true && passedTidsstreg == false) {
|
263 |
|
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) {
|
264 |
|
|
passedTidsstreg = true;
|
265 |
|
|
} else {
|
266 |
|
|
continue;
|
267 |
|
|
}
|
268 |
torben |
1188 |
}*/
|
269 |
torben |
1046 |
|
270 |
|
|
|
271 |
torben |
1034 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
|
272 |
|
|
Elements fields = currentRow.getElementsByTag("td");
|
273 |
|
|
|
274 |
|
|
DepartureEntry departure = new DepartureEntry();
|
275 |
|
|
|
276 |
|
|
|
277 |
|
|
|
278 |
torben |
1038 |
String time = cleanText( fields.get(0).getAllElements().get(2).text() );
|
279 |
torben |
1034 |
if (time.equals(""))
|
280 |
|
|
time = "0:00"; //Bane.dk bug work-around
|
281 |
|
|
departure.setTime(time);
|
282 |
|
|
|
283 |
|
|
int updated = extractUpdated( fields.get(1) );
|
284 |
|
|
departure.setUpdated(updated);
|
285 |
|
|
|
286 |
torben |
1038 |
String trainNumber = cleanText( fields.get(2).text() );
|
287 |
torben |
1034 |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber
|
288 |
torben |
1039 |
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2));
|
289 |
torben |
1034 |
departure.setTrainNumber(trainNumber);
|
290 |
|
|
|
291 |
torben |
1038 |
String destination = cleanText( fields.get(3).text() );
|
292 |
torben |
1034 |
departure.setDestination(destination);
|
293 |
|
|
|
294 |
torben |
1038 |
String origin = cleanText( fields.get(4).text() );
|
295 |
torben |
1034 |
departure.setOrigin(origin);
|
296 |
|
|
|
297 |
torben |
1038 |
String location = cleanText( fields.get(5).text() );
|
298 |
torben |
1034 |
departure.setLocation(location);
|
299 |
|
|
|
300 |
torben |
1038 |
String status = cleanText( fields.get(6).text() );
|
301 |
torben |
1034 |
departure.setStatus(status);
|
302 |
|
|
|
303 |
torben |
1038 |
String note = cleanText( extractNote( fields.get(7) ) );
|
304 |
torben |
1034 |
departure.setNote(note);
|
305 |
|
|
|
306 |
|
|
departure.setType(type);
|
307 |
|
|
|
308 |
torben |
1063 |
departureBean.entries.add(departure);
|
309 |
torben |
1034 |
|
310 |
|
|
|
311 |
torben |
580 |
}
|
312 |
|
|
}
|
313 |
|
|
} else {
|
314 |
|
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type);
|
315 |
|
|
}
|
316 |
|
|
|
317 |
torben |
591 |
|
318 |
torben |
1034 |
return departureBean;
|
319 |
|
|
}
|
320 |
|
|
|
321 |
torben |
580 |
|
322 |
torben |
992 |
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
|
323 |
torben |
305 |
int updated = -1;
|
324 |
|
|
|
325 |
torben |
992 |
Elements updatedImgs = updatedTd.getElementsByTag("img");
|
326 |
|
|
String updatedStr = updatedImgs.get(0).attr("src");
|
327 |
torben |
305 |
|
328 |
|
|
if (updatedStr != null) {
|
329 |
|
|
for (int i=0; i<updatedStr.length(); i++) {
|
330 |
|
|
char c = updatedStr.charAt(i);
|
331 |
|
|
if ( Character.isDigit(c)) {
|
332 |
|
|
updated = Character.digit(c, 10);
|
333 |
|
|
break;
|
334 |
|
|
}
|
335 |
|
|
}
|
336 |
|
|
}
|
337 |
|
|
return updated;
|
338 |
|
|
}
|
339 |
|
|
|
340 |
torben |
992 |
private String extractNote(Element noteTd) {
|
341 |
|
|
String note = noteTd.text().trim();
|
342 |
torben |
313 |
|
343 |
torben |
992 |
|
344 |
|
|
Elements elems = noteTd.getElementsByClass("bemtype");
|
345 |
torben |
313 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i')
|
346 |
|
|
note = note.substring(0,note.length() -1 );
|
347 |
|
|
|
348 |
torben |
1038 |
return note.trim();
|
349 |
torben |
313 |
}
|
350 |
|
|
|
351 |
torben |
1039 |
private String extractTrainNumberAzure(Element trainTd) {
|
352 |
torben |
992 |
Element anchorElement = trainTd.getElementsByTag("a").get(0);
|
353 |
|
|
String href = anchorElement.attr("href");
|
354 |
torben |
349 |
|
355 |
torben |
973 |
int pos = href.lastIndexOf('/');
|
356 |
|
|
String number = href.substring(pos+1);
|
357 |
torben |
970 |
|
358 |
torben |
349 |
return number;
|
359 |
|
|
}
|
360 |
|
|
|
361 |
torben |
1039 |
private String extractTrainNumberWww(Element trainTd) {
|
362 |
|
|
String number = "";
|
363 |
|
|
Element anchorElement = trainTd.getElementsByTag("a").get(0);
|
364 |
|
|
String href = anchorElement.attr("href");
|
365 |
|
|
String argstring = href.substring( href.indexOf('?') + 1);
|
366 |
|
|
|
367 |
|
|
String args[] = argstring.split("&");
|
368 |
|
|
for (String arg : args) {
|
369 |
|
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1]
|
370 |
|
|
|
371 |
|
|
if (pair[0].equalsIgnoreCase("TogNr"))
|
372 |
|
|
number = pair[1];
|
373 |
|
|
}
|
374 |
|
|
|
375 |
|
|
|
376 |
|
|
return number;
|
377 |
|
|
}
|
378 |
|
|
|
379 |
|
|
|
380 |
torben |
305 |
//test
|
381 |
torben |
580 |
/*
|
382 |
torben |
451 |
public static void main(String args[]) throws Exception {
|
383 |
torben |
305 |
DepartureFetcher f = new DepartureFetcher();
|
384 |
torben |
307 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
|
385 |
torben |
305 |
for(DepartureBean d : deps) {
|
386 |
|
|
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
|
387 |
|
|
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
System.out.println("--------------------------");
|
391 |
torben |
580 |
}*/
|
392 |
torben |
305 |
}
|