1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
import java.sql.Connection; |
|
4 |
import java.sql.ResultSet; |
import java.net.URL; |
5 |
import java.sql.Statement; |
import java.net.URLEncoder; |
|
import java.util.ArrayList; |
|
6 |
import java.util.Collections; |
import java.util.Collections; |
7 |
import java.util.List; |
import java.util.HashMap; |
8 |
|
import java.util.Map; |
9 |
|
import java.util.logging.Level; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
12 |
|
import net.sf.jsr107cache.Cache; |
13 |
|
import net.sf.jsr107cache.CacheException; |
14 |
|
import net.sf.jsr107cache.CacheManager; |
15 |
|
|
16 |
|
import org.jsoup.nodes.Document; |
17 |
|
import org.jsoup.nodes.Element; |
18 |
|
import org.jsoup.select.Elements; |
19 |
|
|
20 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.google.appengine.api.memcache.jsr107cache.GCacheFactory; |
|
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
|
21 |
|
|
22 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.android.traininfo.common.DepartureBean; |
23 |
|
import dk.thoerup.android.traininfo.common.DepartureEntry; |
24 |
|
import dk.thoerup.android.traininfo.common.StationBean.StationEntry; |
25 |
|
import dk.thoerup.circuitbreaker.CircuitBreaker; |
26 |
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
27 |
|
import dk.thoerup.traininfoservice.StationDAO; |
28 |
|
import dk.thoerup.traininfoservice.Statistics; |
29 |
|
|
30 |
public class DepartureFetcher { |
public class DepartureFetcher { |
31 |
|
|
32 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
enum TrainType{ |
33 |
|
STOG, |
34 |
|
REGIONAL |
35 |
|
} |
36 |
|
Cache cache; |
37 |
|
|
38 |
TimeoutCache<Integer, List<DepartureBean>> cache = new TimeoutCache<Integer,List<DepartureBean>>(120 * 1000); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
39 |
|
|
40 |
|
StationDAO stationDao = new StationDAO(); |
41 |
|
|
42 |
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception { |
private boolean useAzureSite; |
43 |
|
private int replyTimeout; |
44 |
|
|
45 |
|
@SuppressWarnings("unchecked") |
46 |
|
public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { |
47 |
|
this.replyTimeout = replyTimeout; |
48 |
|
useAzureSite = azureSite; |
49 |
|
|
50 |
List<DepartureBean> list = cache.get(stationID); |
Map props = new HashMap(); |
51 |
|
props.put(GCacheFactory.EXPIRATION_DELTA_MILLIS, cacheTimeout); |
52 |
|
|
53 |
if (list == null) { |
try { |
54 |
logger.warning("Departure: Cache miss " + stationID); //remove before production |
cache = CacheManager.getInstance().getCacheFactory().createCache(props); |
55 |
list = lookupDepartures(stationID); |
} catch (CacheException e) { |
56 |
cache.put(stationID, list); |
logger.log(Level.WARNING, "error creating cache", e); |
57 |
|
} |
58 |
|
|
59 |
|
} |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
public DepartureBean cachedLookupDepartures(int stationID, boolean arrival) throws Exception { |
65 |
|
final String key = "departure:" + stationID + ":" + arrival; |
66 |
|
|
67 |
|
DepartureBean departureBean = (DepartureBean) cache.get(key); |
68 |
|
|
69 |
|
if (departureBean == null) { |
70 |
|
departureBean = lookupDepartures(stationID,arrival); |
71 |
|
cache.put(key, departureBean); |
72 |
|
logger.info("Departure: Cache miss " + key + " !!! "); //remove before production |
73 |
} else { |
} else { |
74 |
logger.warning("Departure: Cache hit " + stationID); //remove before production |
Statistics.getInstance().incrementDepartureCacheHits(); |
75 |
|
logger.info("Departure: Cache hit " + key); |
76 |
} |
} |
77 |
return list; |
|
78 |
|
return departureBean; |
79 |
} |
} |
80 |
|
|
81 |
|
|
82 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public DepartureBean lookupDepartures(int stationID, boolean arrival) throws Exception { |
|
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
|
83 |
|
|
84 |
Connection conn = null; |
DepartureBean departureBean = new DepartureBean(); |
|
try |
|
|
{ |
|
|
conn = DBConnection.getConnection(); |
|
85 |
|
|
86 |
String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID; |
//TODO: debug code |
87 |
Statement stmt = conn.createStatement(); |
StationEntry station = new StationEntry(); // stationDao.getById(stationID); |
88 |
ResultSet rs = stmt.executeQuery(SQL); |
station.setId(82); |
89 |
|
station.setName("Test Station"); |
90 |
if (rs.next()) { |
station.setRegional("HS"); |
91 |
String code = rs.getString( 1 ); |
//TODO: end of debug code |
92 |
if (! rs.wasNull() ) { |
|
93 |
List<DepartureBean> list = lookupDepartures(code, "FJRN"); |
departureBean.stationName = station.getName(); |
94 |
departureList.addAll(list); |
|
95 |
} |
if (station.getRegional() != null) { |
96 |
|
DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); |
97 |
code = rs.getString(2); |
departureBean.entries.addAll( tempBean.entries ); |
98 |
if (! rs.wasNull() ) { |
departureBean.notifications.addAll(tempBean.notifications); |
99 |
List<DepartureBean> list = lookupDepartures(code, "S2"); |
} |
100 |
departureList.addAll(list); |
|
101 |
} |
if (station.getStrain() != null) { |
102 |
Collections.sort( departureList ); |
DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); |
103 |
|
departureBean.entries.addAll( tempBean.entries ); |
104 |
} |
departureBean.notifications.addAll(tempBean.notifications); |
105 |
|
} |
106 |
} finally { |
|
107 |
if (conn != null && !conn.isClosed() ) { |
if (departureBean.entries.size() == 0) { |
108 |
conn.close(); |
logger.info("No departures found for station " + stationID); |
|
} |
|
109 |
} |
} |
110 |
|
|
111 |
return departureList; |
Collections.sort( departureBean.entries ); |
112 |
|
|
113 |
|
|
114 |
|
return departureBean; |
115 |
} |
} |
116 |
|
|
117 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { |
118 |
|
if (useAzureSite == true) { |
119 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
return lookupDeparturesAzureSite(stationcode, type, arrival); |
120 |
|
} else { |
121 |
|
return lookupDeparturesWwwSite(stationcode, type, arrival); |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
private String getTypeStringAzure(TrainType type) { |
126 |
|
switch (type) { |
127 |
|
case STOG: |
128 |
|
return "S-Tog"; |
129 |
|
case REGIONAL: |
130 |
|
return "Fjerntog"; |
131 |
|
default: |
132 |
|
return ""; //Can not happen |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
private String getTypeStringWww(TrainType type) { |
137 |
|
switch (type) { |
138 |
|
case STOG: |
139 |
|
return "S2"; |
140 |
|
case REGIONAL: |
141 |
|
return "FJRN"; |
142 |
|
default: |
143 |
|
return ""; //Can not happen |
144 |
|
} |
145 |
|
} |
146 |
|
|
147 |
|
public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception { |
148 |
|
|
149 |
final WebClient webClient = new WebClient(); |
DepartureBean departureBean = new DepartureBean(); |
150 |
webClient.setTimeout(2500); |
|
|
webClient.setJavaScriptEnabled(false); |
|
|
|
|
151 |
|
|
152 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
String typeString = getTypeStringAzure(type); |
153 |
|
String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; |
154 |
|
|
155 |
HtmlElement table = page.getElementById("afgangtabel"); |
stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); |
156 |
|
|
157 |
|
String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; |
158 |
|
|
159 |
|
logger.fine("URI: " + uri); |
160 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
161 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
162 |
|
|
163 |
|
Document page = (Document) breaker.invoke(wrapper); |
164 |
|
|
165 |
|
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
166 |
|
Element table = page.getElementById(tableName); |
167 |
|
|
168 |
if (table != null) { |
if (table != null) { |
169 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
Elements tableRows = table.getElementsByTag("tr"); |
170 |
|
|
171 |
for (HtmlElement currentRow : tableRows) { |
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
172 |
String rowClass = currentRow.getAttribute("class"); |
boolean passedTidsstreg = false; |
173 |
|
|
174 |
|
for (Element currentRow : tableRows) { |
175 |
|
String rowClass = currentRow.attr("class"); |
176 |
|
|
177 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
178 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
179 |
|
passedTidsstreg = true; |
180 |
|
} else { |
181 |
|
continue; |
182 |
|
} |
183 |
|
} |
184 |
|
|
185 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
186 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
|
187 |
|
Elements fields = currentRow.getElementsByTag("td"); |
188 |
|
|
189 |
DepartureBean departure = new DepartureBean(); |
DepartureEntry departure = new DepartureEntry(); |
190 |
|
|
191 |
String time = fields.get(0).asText(); |
String time = fields.get(0).text(); |
192 |
if (time.equals("")) |
if (time.equals("")) |
193 |
time = "0:00"; //Bane.dk bug work-around |
time = "0:00"; //Bane.dk bug work-around |
194 |
departure.setTime(time); |
departure.setTime(time); |
196 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
197 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
198 |
|
|
199 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).text(); |
200 |
if (trainNumber.trim().length() == 1) |
if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber |
201 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); |
202 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
203 |
|
|
204 |
String destination = fields.get(3).asText(); |
String destination = fields.get(3).text(); |
205 |
departure.setDestination(destination); |
departure.setDestination(destination); |
206 |
|
|
207 |
String origin = fields.get(4).asText(); |
String origin = fields.get(4).text(); |
208 |
departure.setOrigin(origin); |
departure.setOrigin(origin); |
209 |
|
|
210 |
String location = fields.get(5).asText(); |
String location = fields.get(5).text(); |
211 |
departure.setLocation(location); |
departure.setLocation(location); |
212 |
|
|
213 |
String status = fields.get(6).asText(); |
String status = fields.get(6).text().trim(); |
214 |
departure.setStatus(status); |
departure.setStatus(status); |
215 |
|
|
216 |
String note = extractNote( fields.get(7) ); |
String note = extractNote( fields.get(7) ); |
217 |
departure.setNote(note); |
departure.setNote(note); |
218 |
|
|
219 |
departureList.add(departure); |
departure.setType(typeString); |
220 |
|
|
221 |
|
departureBean.entries.add( departure ); |
222 |
} |
} |
223 |
} |
} |
224 |
} else { |
} else { |
225 |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
226 |
} |
} |
227 |
|
|
228 |
return departureList; |
Element notifDiv = page.getElementById("station_planlagte_text"); |
229 |
|
if (notifDiv != null) { |
230 |
|
|
231 |
|
Elements tables = notifDiv.getElementsByTag("table"); |
232 |
|
for (Element tab : tables) { |
233 |
|
|
234 |
|
Elements anchors = tab.getElementsByTag("a"); |
235 |
|
if (anchors.size() == 2) { |
236 |
|
departureBean.notifications.add( anchors.get(1).text() ); |
237 |
|
} |
238 |
|
} |
239 |
|
|
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
return departureBean; |
244 |
|
} |
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
public static String cleanText(String input) { |
249 |
|
//apparently JSoup translates characters on www.bane.dk to 0xA0 |
250 |
|
return input.replace((char) 0xA0, (char)0x20).trim(); |
251 |
} |
} |
252 |
|
|
253 |
private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { |
254 |
|
|
255 |
|
DepartureBean departureBean = new DepartureBean(); |
256 |
|
|
257 |
|
String type = getTypeStringWww(trainType); |
258 |
|
|
259 |
|
stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); |
260 |
|
|
261 |
|
|
262 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
263 |
|
logger.fine("URI:" + uri); |
264 |
|
|
265 |
|
|
266 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); |
267 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
268 |
|
|
269 |
|
Element page = (Element) breaker.invoke(wrapper); |
270 |
|
|
271 |
|
String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; |
272 |
|
Element table = page.getElementById(tableName); |
273 |
|
|
274 |
|
|
275 |
|
|
276 |
|
if (table != null) { |
277 |
|
Elements tableRows = table.getElementsByTag("tr"); |
278 |
|
|
279 |
|
boolean passedTidsstreg = false; |
280 |
|
boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); |
281 |
|
|
282 |
|
for (Element currentRow : tableRows) { |
283 |
|
String rowClass = currentRow.attr("class"); |
284 |
|
|
285 |
|
if (tidsstregExists == true && passedTidsstreg == false) { |
286 |
|
if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { |
287 |
|
passedTidsstreg = true; |
288 |
|
} else { |
289 |
|
continue; |
290 |
|
} |
291 |
|
} |
292 |
|
|
293 |
|
|
294 |
|
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
295 |
|
Elements fields = currentRow.getElementsByTag("td"); |
296 |
|
|
297 |
|
DepartureEntry departure = new DepartureEntry(); |
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
String time = cleanText( fields.get(0).getAllElements().get(2).text() ); |
302 |
|
if (time.equals("")) |
303 |
|
time = "0:00"; //Bane.dk bug work-around |
304 |
|
departure.setTime(time); |
305 |
|
|
306 |
|
int updated = extractUpdated( fields.get(1) ); |
307 |
|
departure.setUpdated(updated); |
308 |
|
|
309 |
|
String trainNumber = cleanText( fields.get(2).text() ); |
310 |
|
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
311 |
|
trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); |
312 |
|
departure.setTrainNumber(trainNumber); |
313 |
|
|
314 |
|
String destination = cleanText( fields.get(3).text() ); |
315 |
|
departure.setDestination(destination); |
316 |
|
|
317 |
|
String origin = cleanText( fields.get(4).text() ); |
318 |
|
departure.setOrigin(origin); |
319 |
|
|
320 |
|
String location = cleanText( fields.get(5).text() ); |
321 |
|
departure.setLocation(location); |
322 |
|
|
323 |
|
String status = cleanText( fields.get(6).text() ); |
324 |
|
departure.setStatus(status); |
325 |
|
|
326 |
|
String note = cleanText( extractNote( fields.get(7) ) ); |
327 |
|
departure.setNote(note); |
328 |
|
|
329 |
|
departure.setType(type); |
330 |
|
|
331 |
|
departureBean.entries.add(departure); |
332 |
|
|
333 |
|
|
334 |
|
} |
335 |
|
} |
336 |
|
} else { |
337 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
338 |
|
} |
339 |
|
|
340 |
|
|
341 |
|
return departureBean; |
342 |
|
} |
343 |
|
|
344 |
|
|
345 |
|
private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" |
346 |
int updated = -1; |
int updated = -1; |
347 |
|
|
348 |
DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img"); |
Elements updatedImgs = updatedTd.getElementsByTag("img"); |
349 |
String updatedStr = updatedImgs.get(0).getAttribute("src"); |
String updatedStr = updatedImgs.get(0).attr("src"); |
350 |
|
|
351 |
if (updatedStr != null) { |
if (updatedStr != null) { |
352 |
for (int i=0; i<updatedStr.length(); i++) { |
for (int i=0; i<updatedStr.length(); i++) { |
360 |
return updated; |
return updated; |
361 |
} |
} |
362 |
|
|
363 |
private String extractNote(HtmlElement noteTd) { |
private String extractNote(Element noteTd) { |
364 |
String note = noteTd.asText().trim(); |
String note = noteTd.text().trim(); |
365 |
|
|
366 |
|
|
367 |
List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype"); |
Elements elems = noteTd.getElementsByClass("bemtype"); |
368 |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
369 |
note = note.substring(0,note.length() -1 ); |
note = note.substring(0,note.length() -1 ); |
370 |
|
|
371 |
return note; |
return note.trim(); |
372 |
|
} |
373 |
|
|
374 |
|
private String extractTrainNumberAzure(Element trainTd) { |
375 |
|
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
376 |
|
String href = anchorElement.attr("href"); |
377 |
|
|
378 |
|
int pos = href.lastIndexOf('/'); |
379 |
|
String number = href.substring(pos+1); |
380 |
|
|
381 |
|
return number; |
382 |
} |
} |
383 |
|
|
384 |
private String extractTrainNumber(HtmlElement trainTd) { |
private String extractTrainNumberWww(Element trainTd) { |
385 |
String number = ""; |
String number = ""; |
386 |
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
Element anchorElement = trainTd.getElementsByTag("a").get(0); |
387 |
String href = anchorElement.getAttribute("href"); |
String href = anchorElement.attr("href"); |
388 |
String argstring = href.substring( href.indexOf('?') + 1); |
String argstring = href.substring( href.indexOf('?') + 1); |
389 |
|
|
390 |
String args[] = argstring.split("&"); |
String args[] = argstring.split("&"); |
396 |
} |
} |
397 |
|
|
398 |
|
|
|
|
|
399 |
return number; |
return number; |
400 |
} |
} |
401 |
|
|
402 |
|
|
403 |
//test |
//test |
404 |
public static void main(String args[]) throws Exception{ |
/* |
405 |
|
public static void main(String args[]) throws Exception { |
406 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
407 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
408 |
for(DepartureBean d : deps) { |
for(DepartureBean d : deps) { |
411 |
} |
} |
412 |
|
|
413 |
System.out.println("--------------------------"); |
System.out.println("--------------------------"); |
414 |
} |
}*/ |
415 |
} |
} |