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