1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
3 |
|
|
4 |
import java.io.IOException; |
|
5 |
import java.net.URL; |
import java.net.URL; |
6 |
import java.sql.SQLException; |
import java.sql.SQLException; |
|
import java.util.ArrayList; |
|
|
import java.util.List; |
|
7 |
import java.util.Map; |
import java.util.Map; |
8 |
import java.util.logging.Level; |
import java.util.logging.Level; |
9 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
10 |
|
|
11 |
import com.gargoylesoftware.htmlunit.BrowserVersion; |
import org.jsoup.nodes.Document; |
12 |
import com.gargoylesoftware.htmlunit.Page; |
import org.jsoup.nodes.Element; |
13 |
import com.gargoylesoftware.htmlunit.RefreshHandler; |
import org.jsoup.select.Elements; |
|
import com.gargoylesoftware.htmlunit.WebClient; |
|
|
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
|
|
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
|
14 |
|
|
15 |
|
import dk.thoerup.android.traininfo.common.StationBean; |
16 |
|
import dk.thoerup.android.traininfo.common.StationEntry; |
17 |
|
import dk.thoerup.android.traininfo.common.TimetableBean; |
18 |
|
import dk.thoerup.android.traininfo.common.TimetableEntry; |
19 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
20 |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
21 |
import dk.thoerup.traininfoservice.StationDAO; |
import dk.thoerup.genericjavautils.TimeoutMap; |
22 |
import dk.thoerup.traininfoservice.Statistics; |
import dk.thoerup.traininfoservice.Statistics; |
23 |
|
import dk.thoerup.traininfoservice.TraininfoSettings; |
24 |
|
import dk.thoerup.traininfoservice.db.StationDAO; |
25 |
|
|
26 |
public class TimetableFetcher { |
public class TimetableFetcher { |
27 |
|
|
|
class NullRefreshHandler implements RefreshHandler { |
|
|
public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { |
|
|
} |
|
|
|
|
|
} |
|
28 |
|
|
29 |
Map<String, List<TimetableBean>> cache; |
Map<String, TimetableBean> cache; |
30 |
Map<String, Integer> stationCache; |
Map<String, StationEntry> stationCache; |
31 |
|
|
32 |
StationDAO stationDao = new StationDAO(); |
StationDAO stationDao = new StationDAO(); |
33 |
|
|
34 |
|
|
35 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
36 |
|
|
37 |
|
TraininfoSettings settings; |
38 |
|
|
39 |
private boolean useTempSite; |
public TimetableFetcher(TraininfoSettings settings) { |
40 |
|
this.settings = settings; |
|
public TimetableFetcher(boolean tmpSite, int cacheTimeout) { |
|
|
useTempSite = tmpSite; |
|
41 |
|
|
42 |
cache = new TimeoutMap<String,List<TimetableBean>>(cacheTimeout); |
cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() ); |
43 |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
stationCache = new TimeoutMap<String,StationEntry>( 3*60*60*1000 ); |
44 |
} |
} |
45 |
|
|
46 |
|
|
47 |
List<TimetableBean> cachedLookupTimetable(String trainID, String type) throws Exception { |
TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { |
48 |
String key = trainID+type; |
String key = trainID+type; |
49 |
List<TimetableBean> list = cache.get(key); |
TimetableBean list = cache.get(key); |
50 |
|
|
51 |
if (list == null) { |
if (list == null) { |
52 |
list = lookupTimetable(trainID,type); |
list = lookupTimetable(trainID,type); |
58 |
return list; |
return list; |
59 |
} |
} |
60 |
|
|
61 |
List<TimetableBean> lookupTimetable(String trainID, String type) throws Exception { |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
62 |
if (useTempSite == false ){ |
if (settings.getBackend() == TraininfoSettings.Backend.Azure ){ |
63 |
return lookupTimetableRealSite(trainID, type); |
return lookupTimetableAzureSite(trainID, type); |
64 |
|
|
65 |
} else { |
} else { |
66 |
return new ArrayList<TimetableBean>(); // no timetable data on temp site |
return lookupTimetableMobileSite(trainID, type); |
67 |
} |
} |
68 |
} |
} |
69 |
|
|
70 |
int getStationId(String name) { |
StationEntry getStationId(String name) { |
71 |
Integer id = stationCache.get(name); |
StationEntry station = stationCache.get(name); |
72 |
|
|
73 |
if (id == null) { |
if (station == null) { |
74 |
try { |
try { |
75 |
id = stationDao.getBySpecificName(name); |
StationBean bean = stationDao.getByName(name); |
76 |
stationCache.put(name, id); |
if (bean.entries.size() == 1) { |
77 |
|
station = bean.entries.get(0); |
78 |
|
stationCache.put(name,station); |
79 |
|
} |
80 |
} catch (SQLException e) { |
} catch (SQLException e) { |
81 |
logger.log(Level.SEVERE, "getStationId failed", e); |
logger.log(Level.SEVERE, "getStationId failed", e); |
|
id = -1; |
|
82 |
} |
} |
83 |
} |
} |
84 |
|
|
85 |
return id; |
return station; |
86 |
|
} |
87 |
|
|
88 |
|
String correctStationName(String name) { |
89 |
|
if (name.equals("København")) |
90 |
|
name = "København H"; //correct inconsistency in naming |
91 |
|
|
92 |
|
return name; |
93 |
} |
} |
94 |
|
|
95 |
List<TimetableBean> lookupTimetableRealSite(String trainID, String type) throws Exception { |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
96 |
List<TimetableBean> timetableList = new ArrayList<TimetableBean>(); |
TimetableBean timetableBean = new TimetableBean(); |
97 |
|
|
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
|
|
|
|
98 |
|
|
99 |
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
100 |
webClient.setTimeout(2500); |
logger.fine("URL:" + url); |
|
webClient.setJavaScriptEnabled(false); |
|
|
webClient.setRefreshHandler( new NullRefreshHandler() ); |
|
|
webClient.setCssEnabled(false); |
|
101 |
|
|
102 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, url); |
|
103 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
104 |
|
|
105 |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
Document doc = (Document) breaker.invoke(wrapper); |
106 |
|
|
107 |
|
|
108 |
boolean currentStation = false; |
boolean currentStation = false; |
109 |
boolean currentStationSaved = false; |
boolean currentStationSaved = false; |
110 |
|
|
111 |
List<HtmlElement> tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); |
Elements tables = doc.getElementsByClass("Rute"); |
112 |
|
|
113 |
if (tables.size() == 1) { |
if (tables.size() == 1) { |
114 |
HtmlElement timetable = tables.get(0); |
Element timetable = tables.get(0); |
115 |
DomNodeList<HtmlElement> rows = timetable.getElementsByTagName("tr"); |
Elements rows = timetable.getElementsByTag("tr"); |
116 |
|
|
117 |
for (int i=0; i<rows.size(); i++) { |
for (int i=0; i<rows.size(); i++) { |
118 |
if (i==0) //First row is column headers |
if (i==0) //First row is column headers |
119 |
continue; |
continue; |
120 |
|
|
121 |
|
|
122 |
HtmlElement row = rows.get(i); |
Element row = rows.get(i); |
123 |
DomNodeList<HtmlElement> fields = row.getElementsByTagName("td"); |
Elements fields = row.getElementsByTag("td"); |
124 |
|
|
125 |
|
|
126 |
if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { |
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
127 |
currentStation = true; |
currentStation = true; |
128 |
continue; |
continue; |
129 |
} |
} |
130 |
|
|
131 |
TimetableBean bean = new TimetableBean(); |
TimetableEntry entry = new TimetableEntry(); |
132 |
|
|
133 |
String station = fields.get(0).asText() ; |
String station = correctStationName( fields.get(0).text() ); |
134 |
if (station.equals("København")) |
|
135 |
station = "København H"; //correct inconsistency in naming |
entry.setStation( station ); |
136 |
|
entry.setArrival( fields.get(1).text() ); |
137 |
bean.setStation( station ); |
entry.setDeparture( fields.get(2).text() ); |
138 |
bean.setArrival( fields.get(1).asText() ); |
|
139 |
bean.setDeparture( fields.get(2).asText() ); |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
140 |
|
entry.setCancelled(cancelled); |
141 |
|
|
142 |
if (currentStation == true && currentStationSaved == false ) { |
if (currentStation == true && currentStationSaved == false ) { |
143 |
bean.setCurrent(currentStation); |
entry.setCurrent(currentStation); |
144 |
currentStationSaved = true; |
currentStationSaved = true; |
145 |
} |
} |
146 |
|
|
147 |
bean.setStationId( getStationId( station )); |
entry.setStationEntry( getStationId( station )); |
148 |
|
|
149 |
timetableList.add(bean); |
timetableBean.entries.add(entry); |
150 |
|
} |
151 |
|
|
152 |
|
//TODO: There is an off-by-one error in this cancelled parser thingie |
153 |
|
final String cancelledString = "Aflyst"; |
154 |
|
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
155 |
|
final int lastIdx = (timetableBean.entries.size() - 1); |
156 |
|
|
157 |
|
TimetableEntry current = timetableBean.entries.get(i); |
158 |
|
if (current.isCancelled()) { |
159 |
|
if (i == 0) { |
160 |
|
current.setDeparture(cancelledString); |
161 |
|
} else if (i == lastIdx) { |
162 |
|
current.setArrival(cancelledString); |
163 |
|
} else if (i>0 && i<lastIdx) { |
164 |
|
TimetableEntry next = timetableBean.entries.get(i+1); |
165 |
|
TimetableEntry prev = timetableBean.entries.get(i-1); |
166 |
|
|
167 |
|
if (next.isCancelled()) |
168 |
|
current.setDeparture(cancelledString); |
169 |
|
if (prev.isCancelled()) |
170 |
|
current.setArrival(cancelledString); |
171 |
|
} |
172 |
|
} |
173 |
} |
} |
174 |
|
|
175 |
} else { |
} else { |
176 |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
177 |
} |
} |
178 |
webClient.closeAllWindows(); |
|
179 |
|
|
180 |
return timetableList; |
return timetableBean; |
181 |
|
} |
182 |
|
|
183 |
|
TimetableBean lookupTimetableMobileSite(String trainID, String type) throws Exception { |
184 |
|
TimetableBean timetableBean = new TimetableBean(); |
185 |
|
|
186 |
|
String url = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=" + trainID + "&webprofil=" + type + "&mode=rute"; |
187 |
|
logger.fine("URL:" + url); |
188 |
|
|
189 |
|
|
190 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
191 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
192 |
|
|
193 |
|
Document doc = (Document) breaker.invoke(wrapper); |
194 |
|
|
195 |
|
Element content = doc.getElementsByClass("contentDiv").get(1); |
196 |
|
Element dlist = content.child(0); |
197 |
|
|
198 |
|
|
199 |
|
Elements rows = dlist.getElementsByTag("dt"); |
200 |
|
|
201 |
|
for (int i=0; i<rows.size(); i++) { |
202 |
|
|
203 |
|
Element row = rows.get(i); |
204 |
|
|
205 |
|
logger.fine( row.text() ); |
206 |
|
|
207 |
|
String parts[] = row.text().split(","); |
208 |
|
|
209 |
|
TimetableEntry entry = new TimetableEntry(); |
210 |
|
|
211 |
|
String station = DepartureFetcher.cleanText( parts[0] ) ; |
212 |
|
station = correctStationName(station); |
213 |
|
|
214 |
|
|
215 |
|
String arrival = DepartureFetcher.cleanText( parts[1] ); |
216 |
|
String departure = DepartureFetcher.cleanText( "" ); |
217 |
|
|
218 |
|
entry.setStation( station ); |
219 |
|
entry.setArrival( arrival ); |
220 |
|
entry.setDeparture( departure ); |
221 |
|
|
222 |
|
|
223 |
|
entry.setStationEntry( getStationId( station )); |
224 |
|
|
225 |
|
timetableBean.entries.add(entry); |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
return timetableBean; |
230 |
|
|
231 |
} |
} |
232 |
|
|
233 |
|
@Deprecated |
234 |
|
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
235 |
|
TimetableBean timetableBean = new TimetableBean(); |
236 |
|
|
237 |
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
238 |
|
logger.fine("URL:" + url); |
239 |
|
|
240 |
|
|
241 |
|
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
242 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
243 |
|
|
244 |
|
Document doc = (Document) breaker.invoke(wrapper); |
245 |
|
|
246 |
|
|
247 |
|
boolean currentStation = false; |
248 |
|
boolean currentStationSaved = false; |
249 |
|
|
250 |
|
Elements tables = doc.getElementsByClass("Rute"); |
251 |
|
|
252 |
|
if (tables.size() == 1) { |
253 |
|
Element timetable = tables.get(0); |
254 |
|
Elements rows = timetable.getElementsByTag("tr"); |
255 |
|
|
256 |
|
for (int i=0; i<rows.size(); i++) { |
257 |
|
if (i==0) //First row is column headers |
258 |
|
continue; |
259 |
|
|
260 |
|
|
261 |
|
Element row = rows.get(i); |
262 |
|
Elements fields = row.getElementsByTag("td"); |
263 |
|
|
264 |
|
|
265 |
|
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
266 |
|
currentStation = true; |
267 |
|
continue; |
268 |
|
} |
269 |
|
|
270 |
|
TimetableEntry entry = new TimetableEntry(); |
271 |
|
|
272 |
|
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
273 |
|
station = correctStationName(station); |
274 |
|
|
275 |
|
|
276 |
|
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
277 |
|
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
278 |
|
|
279 |
|
entry.setStation( station ); |
280 |
|
entry.setArrival( arrival ); |
281 |
|
entry.setDeparture( departure ); |
282 |
|
|
283 |
|
|
284 |
|
if (currentStation == true && currentStationSaved == false ) { |
285 |
|
entry.setCurrent(currentStation); |
286 |
|
currentStationSaved = true; |
287 |
|
} |
288 |
|
|
289 |
|
entry.setStationEntry( getStationId( station )); |
290 |
|
|
291 |
|
timetableBean.entries.add(entry); |
292 |
|
} |
293 |
|
|
294 |
|
} else { |
295 |
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
return timetableBean; |
300 |
|
} |
301 |
|
|
302 |
} |
} |