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