1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
|
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; |
42 |
|
|
43 |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
44 |
final String key = "" + stationID + ":" + arrival; |
final String key = "" + stationID + ":" + arrival; |
45 |
/* |
|
46 |
DepartureBean departureBean = cache.get(key); |
DepartureBean departureBean = cache.get(key); |
47 |
|
|
48 |
|
|
53 |
Statistics.getInstance().incrementDepartureCacheHits(); |
Statistics.getInstance().incrementDepartureCacheHits(); |
54 |
logger.info("Departure: Cache hit " + key); //remove before production |
logger.info("Departure: Cache hit " + key); //remove before production |
55 |
} |
} |
56 |
return departureBean;*/ |
return departureBean; |
|
|
|
|
return lookupDepartures(stationID,arrival); |
|
57 |
} |
} |
58 |
|
|
59 |
|
|
105 |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
public DepartureBean lookupDeparturesNormalSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
106 |
|
|
107 |
DepartureBean departureBean = new DepartureBean(); |
DepartureBean departureBean = new DepartureBean(); |
108 |
|
|
|
final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 ); |
|
|
webClient.setTimeout(2500); |
|
|
webClient.setJavaScriptEnabled(false); |
|
|
|
|
109 |
|
|
110 |
String typeString = getTypeString(type); |
String typeString = getTypeString(type); |
111 |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
112 |
|
|
113 |
|
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
114 |
//String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
//String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
115 |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
116 |
|
|
117 |
//logger.info("URI: " + uri); |
|
118 |
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
|
119 |
|
//logger.info("URI: " + uri); |
120 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), 2500); |
121 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
122 |
|
|
123 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Document page = (Document) breaker.invoke(wrapper); |
124 |
|
|
125 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
126 |
HtmlElement table = page.getElementById(tableName); |
Element table = page.getElementById(tableName); |
127 |
|
|
128 |
if (table != null) { |
if (table != null) { |
129 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
130 |
|
|
131 |
for (HtmlElement currentRow : tableRows) { |
for (Element currentRow : tableRows) { |
132 |
String rowClass = currentRow.getAttribute("class"); |
String rowClass = currentRow.attr("class"); |
133 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
134 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
Elements fields = currentRow.getElementsByTag("td"); |
135 |
|
|
136 |
DepartureEntry departure = new DepartureEntry(); |
DepartureEntry departure = new DepartureEntry(); |
137 |
|
|
138 |
String time = fields.get(0).asText(); |
String time = fields.get(0).text(); |
139 |
if (time.equals("")) |
if (time.equals("")) |
140 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
141 |
departure.setTime(time); |
departure.setTime(time); |
143 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
144 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
145 |
|
|
146 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).text(); |
147 |
if (type == TrainType.STOG) //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 |
148 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
149 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
150 |
|
|
151 |
String destination = fields.get(3).asText(); |
String destination = fields.get(3).text(); |
152 |
departure.setDestination(destination); |
departure.setDestination(destination); |
153 |
|
|
154 |
String origin = fields.get(4).asText(); |
String origin = fields.get(4).text(); |
155 |
departure.setOrigin(origin); |
departure.setOrigin(origin); |
156 |
|
|
157 |
String location = fields.get(5).asText(); |
String location = fields.get(5).text(); |
158 |
departure.setLocation(location); |
departure.setLocation(location); |
159 |
|
|
160 |
String status = fields.get(6).asText().trim(); |
String status = fields.get(6).text().trim(); |
161 |
departure.setStatus(status); |
departure.setStatus(status); |
162 |
|
|
163 |
String note = extractNote( fields.get(7) ); |
String note = extractNote( fields.get(7) ); |
172 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
173 |
} |
} |
174 |
|
|
175 |
HtmlElement notifDiv = page.getElementById("station_planlagte_text"); |
Element notifDiv = page.getElementById("station_planlagte_text"); |
176 |
if (notifDiv != null) { |
if (notifDiv != null) { |
177 |
|
|
178 |
DomNodeList<HtmlElement> tables = notifDiv.getElementsByTagName("table"); |
Elements tables = notifDiv.getElementsByTag("table"); |
179 |
for (HtmlElement tab : tables) { |
for (Element tab : tables) { |
180 |
|
|
181 |
DomNodeList<HtmlElement> anchors = tab.getElementsByTagName("a"); |
Elements anchors = tab.getElementsByTag("a"); |
182 |
if (anchors.size() == 2) { |
if (anchors.size() == 2) { |
183 |
departureBean.notifications.add( anchors.get(1).getTextContent() ); |
departureBean.notifications.add( anchors.get(1).text() ); |
184 |
} |
} |
185 |
} |
} |
186 |
|
|
187 |
} |
} |
188 |
|
|
189 |
|
|
|
webClient.closeAllWindows(); |
|
|
|
|
190 |
return departureBean; |
return departureBean; |
191 |
} |
} |
192 |
|
|
259 |
}*/ |
}*/ |
260 |
|
|
261 |
|
|
262 |
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" |
263 |
int updated = -1; |
int updated = -1; |
264 |
|
|
265 |
DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img"); |
Elements updatedImgs = updatedTd.getElementsByTag("img"); |
266 |
String updatedStr = updatedImgs.get(0).getAttribute("src"); |
String updatedStr = updatedImgs.get(0).attr("src"); |
267 |
|
|
268 |
if (updatedStr != null) { |
if (updatedStr != null) { |
269 |
for (int i=0; i<updatedStr.length(); i++) { |
for (int i=0; i<updatedStr.length(); i++) { |
277 |
return updated; |
return updated; |
278 |
} |
} |
279 |
|
|
280 |
private String extractNote(HtmlElement noteTd) { |
private String extractNote(Element noteTd) { |
281 |
String note = noteTd.asText().trim(); |
String note = noteTd.text().trim(); |
282 |
|
|
283 |
|
|
284 |
List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype"); |
Elements elems = noteTd.getElementsByClass("bemtype"); |
285 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
286 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
287 |
|
|
288 |
return note; |
return note; |
289 |
} |
} |
290 |
|
|
291 |
private String extractTrainNumber(HtmlElement trainTd) { |
private String extractTrainNumber(Element trainTd) { |
292 |
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
293 |
String href = anchorElement.getAttribute("href"); |
String href = anchorElement.attr("href"); |
294 |
|
|
295 |
int pos = href.lastIndexOf('/'); |
int pos = href.lastIndexOf('/'); |
296 |
String number = href.substring(pos+1); |
String number = href.substring(pos+1); |