20 |
|
|
21 |
public class DepartureFetcher { |
public class DepartureFetcher { |
22 |
|
|
23 |
|
enum TrainType{ |
24 |
|
STOG, |
25 |
|
REGIONAL |
26 |
|
} |
27 |
|
|
28 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
29 |
|
|
30 |
Map<String, List<DepartureBean>> cache; |
Map<String, List<DepartureBean>> cache; |
64 |
StationBean station = stationDao.getById(stationID); |
StationBean station = stationDao.getById(stationID); |
65 |
|
|
66 |
if (station.getRegional() != null) { |
if (station.getRegional() != null) { |
67 |
List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN", arrival); |
List<DepartureBean> list = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
68 |
departureList.addAll(list); |
departureList.addAll(list); |
69 |
} |
} |
70 |
|
|
71 |
if (station.getStrain() != null) { |
if (station.getStrain() != null) { |
72 |
List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2", arrival); |
List<DepartureBean> list = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
73 |
departureList.addAll(list); |
departureList.addAll(list); |
74 |
} |
} |
75 |
|
|
79 |
return departureList; |
return departureList; |
80 |
} |
} |
81 |
|
|
82 |
public List<DepartureBean> lookupDepartures(String stationcode, String type, boolean arrival) throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
83 |
if (useTempSite == false) { |
if (useTempSite == false) { |
84 |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
return lookupDeparturesNormalSite(stationcode, type, arrival); |
85 |
} else { |
} else { |
86 |
return lookupDeparturesFromTemporarySite(stationcode, type); |
//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 |
} |
} |
90 |
} |
} |
91 |
|
|
92 |
public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type, boolean arrival) throws Exception { |
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 |
|
|
105 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
106 |
|
|
107 |
final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 ); |
final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 ); |
108 |
webClient.setTimeout(2500); |
webClient.setTimeout(2500); |
109 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
110 |
|
|
111 |
|
|
112 |
|
String typeString = getTypeString(type); |
113 |
|
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
114 |
|
|
115 |
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; |
116 |
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
117 |
|
|
118 |
|
//logger.info("URI: " + uri); |
119 |
|
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
120 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
121 |
|
|
122 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
123 |
|
|
124 |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
|
|
|
|
System.out.println(uri); |
|
|
System.out.println(tableName); |
|
|
|
|
125 |
HtmlElement table = page.getElementById(tableName); |
HtmlElement table = page.getElementById(tableName); |
126 |
|
|
127 |
if (table != null) { |
if (table != null) { |
143 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
144 |
|
|
145 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).asText(); |
146 |
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 |
147 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
148 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
149 |
|
|
156 |
String location = fields.get(5).asText(); |
String location = fields.get(5).asText(); |
157 |
departure.setLocation(location); |
departure.setLocation(location); |
158 |
|
|
159 |
String status = fields.get(6).asText(); |
String status = fields.get(6).asText().trim(); |
160 |
departure.setStatus(status); |
departure.setStatus(status); |
161 |
|
|
162 |
String note = extractNote( fields.get(7) ); |
String note = extractNote( fields.get(7) ); |
163 |
departure.setNote(note); |
departure.setNote(note); |
164 |
|
|
165 |
departure.setType(type); |
departure.setType(typeString); |
166 |
|
|
167 |
departureList.add(departure); |
departureList.add(departure); |
168 |
} |
} |
175 |
return departureList; |
return departureList; |
176 |
} |
} |
177 |
|
|
178 |
|
/* |
179 |
|
@Deprecated |
180 |
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
181 |
|
|
182 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
188 |
|
|
189 |
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
190 |
|
|
191 |
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
HtmlunitInvocation wrapper = new HtmlunitInvocation(webClient, uri); |
192 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
193 |
|
|
194 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
241 |
|
|
242 |
|
|
243 |
return departureList; |
return departureList; |
244 |
} |
}*/ |
245 |
|
|
246 |
|
|
247 |
private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
273 |
} |
} |
274 |
|
|
275 |
private String extractTrainNumber(HtmlElement trainTd) { |
private String extractTrainNumber(HtmlElement trainTd) { |
|
String number = ""; |
|
276 |
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
277 |
String href = anchorElement.getAttribute("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]; |
|
|
} |
|
278 |
|
|
279 |
|
int pos = href.lastIndexOf('/'); |
280 |
|
String number = href.substring(pos+1); |
281 |
|
|
282 |
return number; |
return number; |
283 |
} |
} |