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