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 |
711 |
import dk.thoerup.traininfoservice.Statistics; |
20 |
torben |
1303 |
import dk.thoerup.traininfoservice.TraininfoSettings; |
21 |
torben |
1255 |
import dk.thoerup.traininfoservice.db.StationDAO; |
22 |
torben |
421 |
|
23 |
torben |
350 |
public class TimetableFetcher { |
24 |
torben |
992 |
|
25 |
torben |
350 |
|
26 |
torben |
1060 |
Map<String, TimetableBean> cache; |
27 |
torben |
836 |
Map<String, Integer> stationCache; |
28 |
|
|
|
29 |
|
|
StationDAO stationDao = new StationDAO(); |
30 |
torben |
350 |
|
31 |
torben |
387 |
|
32 |
torben |
350 |
Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); |
33 |
torben |
1303 |
|
34 |
|
|
TraininfoSettings settings; |
35 |
torben |
387 |
|
36 |
torben |
1303 |
public TimetableFetcher(TraininfoSettings settings) { |
37 |
|
|
this.settings = settings; |
38 |
torben |
584 |
|
39 |
torben |
1303 |
cache = new TimeoutMap<String,TimetableBean>( settings.getCacheTimeout() ); |
40 |
torben |
836 |
stationCache = new TimeoutMap<String,Integer>( 3*60*60*1000 ); |
41 |
torben |
581 |
} |
42 |
|
|
|
43 |
|
|
|
44 |
torben |
1060 |
TimetableBean cachedLookupTimetable(String trainID, String type) throws Exception { |
45 |
torben |
387 |
String key = trainID+type; |
46 |
torben |
1060 |
TimetableBean list = cache.get(key); |
47 |
torben |
387 |
|
48 |
|
|
if (list == null) { |
49 |
|
|
list = lookupTimetable(trainID,type); |
50 |
|
|
cache.put(key, list); |
51 |
|
|
} else { |
52 |
torben |
711 |
Statistics.getInstance().incrementTimetableCacheHits(); |
53 |
torben |
389 |
logger.info("Timetable: Cache hit " + trainID); |
54 |
torben |
387 |
} |
55 |
|
|
return list; |
56 |
|
|
} |
57 |
torben |
581 |
|
58 |
torben |
1060 |
TimetableBean lookupTimetable(String trainID, String type) throws Exception { |
59 |
torben |
1303 |
if (settings.getUseAzureSite() == true ){ |
60 |
torben |
1036 |
return lookupTimetableAzureSite(trainID, type); |
61 |
|
|
|
62 |
torben |
581 |
} else { |
63 |
torben |
1036 |
return lookupTimetableWwwSite(trainID, type); |
64 |
torben |
581 |
} |
65 |
|
|
} |
66 |
torben |
836 |
|
67 |
|
|
int getStationId(String name) { |
68 |
|
|
Integer id = stationCache.get(name); |
69 |
|
|
|
70 |
|
|
if (id == null) { |
71 |
|
|
try { |
72 |
torben |
842 |
id = stationDao.getIdByName(name); |
73 |
torben |
836 |
stationCache.put(name, id); |
74 |
|
|
} catch (SQLException e) { |
75 |
|
|
logger.log(Level.SEVERE, "getStationId failed", e); |
76 |
|
|
id = -1; |
77 |
|
|
} |
78 |
|
|
} |
79 |
torben |
350 |
|
80 |
torben |
836 |
return id; |
81 |
|
|
} |
82 |
|
|
|
83 |
torben |
1060 |
TimetableBean lookupTimetableAzureSite(String trainID, String type) throws Exception { |
84 |
|
|
TimetableBean timetableBean = new TimetableBean(); |
85 |
torben |
350 |
|
86 |
torben |
1048 |
|
87 |
torben |
970 |
String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; |
88 |
torben |
1048 |
logger.fine("URL:" + url); |
89 |
torben |
350 |
|
90 |
torben |
1303 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
91 |
torben |
421 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
92 |
torben |
350 |
|
93 |
torben |
992 |
Document doc = (Document) breaker.invoke(wrapper); |
94 |
torben |
421 |
|
95 |
torben |
350 |
|
96 |
|
|
boolean currentStation = false; |
97 |
|
|
boolean currentStationSaved = false; |
98 |
|
|
|
99 |
torben |
992 |
Elements tables = doc.getElementsByClass("Rute"); |
100 |
|
|
|
101 |
torben |
350 |
if (tables.size() == 1) { |
102 |
torben |
992 |
Element timetable = tables.get(0); |
103 |
|
|
Elements rows = timetable.getElementsByTag("tr"); |
104 |
torben |
350 |
|
105 |
|
|
for (int i=0; i<rows.size(); i++) { |
106 |
|
|
if (i==0) //First row is column headers |
107 |
|
|
continue; |
108 |
|
|
|
109 |
|
|
|
110 |
torben |
992 |
Element row = rows.get(i); |
111 |
|
|
Elements fields = row.getElementsByTag("td"); |
112 |
|
|
|
113 |
torben |
350 |
|
114 |
torben |
992 |
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
115 |
torben |
350 |
currentStation = true; |
116 |
|
|
continue; |
117 |
|
|
} |
118 |
|
|
|
119 |
torben |
1060 |
TimetableEntry entry = new TimetableEntry(); |
120 |
torben |
836 |
|
121 |
torben |
992 |
String station = fields.get(0).text() ; |
122 |
torben |
836 |
if (station.equals("København")) |
123 |
|
|
station = "København H"; //correct inconsistency in naming |
124 |
|
|
|
125 |
torben |
1060 |
entry.setStation( station ); |
126 |
|
|
entry.setArrival( fields.get(1).text() ); |
127 |
|
|
entry.setDeparture( fields.get(2).text() ); |
128 |
torben |
350 |
|
129 |
torben |
992 |
boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); |
130 |
torben |
1060 |
entry.setCancelled(cancelled); |
131 |
torben |
975 |
|
132 |
torben |
350 |
if (currentStation == true && currentStationSaved == false ) { |
133 |
torben |
1060 |
entry.setCurrent(currentStation); |
134 |
torben |
350 |
currentStationSaved = true; |
135 |
|
|
} |
136 |
|
|
|
137 |
torben |
1060 |
entry.setStationId( getStationId( station )); |
138 |
torben |
836 |
|
139 |
torben |
1060 |
timetableBean.entries.add(entry); |
140 |
torben |
350 |
} |
141 |
|
|
|
142 |
torben |
977 |
//TODO: There is an off-by-one error in this cancelled parser thingie |
143 |
torben |
975 |
final String cancelledString = "Aflyst"; |
144 |
torben |
1060 |
for (int i=0;i<timetableBean.entries.size(); i++) { //handle cancelled labels |
145 |
|
|
final int lastIdx = (timetableBean.entries.size() - 1); |
146 |
torben |
975 |
|
147 |
torben |
1060 |
TimetableEntry current = timetableBean.entries.get(i); |
148 |
torben |
976 |
if (current.isCancelled()) { |
149 |
|
|
if (i == 0) { |
150 |
|
|
current.setDeparture(cancelledString); |
151 |
|
|
} else if (i == lastIdx) { |
152 |
|
|
current.setArrival(cancelledString); |
153 |
|
|
} else if (i>0 && i<lastIdx) { |
154 |
torben |
1060 |
TimetableEntry next = timetableBean.entries.get(i+1); |
155 |
|
|
TimetableEntry prev = timetableBean.entries.get(i-1); |
156 |
torben |
976 |
|
157 |
|
|
if (next.isCancelled()) |
158 |
|
|
current.setDeparture(cancelledString); |
159 |
|
|
if (prev.isCancelled()) |
160 |
|
|
current.setArrival(cancelledString); |
161 |
|
|
} |
162 |
torben |
975 |
} |
163 |
|
|
} |
164 |
|
|
|
165 |
torben |
350 |
} else { |
166 |
|
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
167 |
|
|
} |
168 |
torben |
992 |
|
169 |
torben |
350 |
|
170 |
torben |
1060 |
return timetableBean; |
171 |
torben |
350 |
} |
172 |
torben |
1036 |
|
173 |
torben |
1060 |
TimetableBean lookupTimetableWwwSite(String trainID, String type) throws Exception { |
174 |
|
|
TimetableBean timetableBean = new TimetableBean(); |
175 |
torben |
1036 |
|
176 |
|
|
String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; |
177 |
torben |
1048 |
logger.fine("URL:" + url); |
178 |
torben |
1036 |
|
179 |
|
|
|
180 |
torben |
1303 |
JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , settings.getReplyTimeout() ); |
181 |
torben |
1036 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
182 |
|
|
|
183 |
|
|
Document doc = (Document) breaker.invoke(wrapper); |
184 |
|
|
|
185 |
|
|
|
186 |
|
|
boolean currentStation = false; |
187 |
|
|
boolean currentStationSaved = false; |
188 |
|
|
|
189 |
|
|
Elements tables = doc.getElementsByClass("Rute"); |
190 |
|
|
|
191 |
|
|
if (tables.size() == 1) { |
192 |
|
|
Element timetable = tables.get(0); |
193 |
|
|
Elements rows = timetable.getElementsByTag("tr"); |
194 |
|
|
|
195 |
|
|
for (int i=0; i<rows.size(); i++) { |
196 |
|
|
if (i==0) //First row is column headers |
197 |
|
|
continue; |
198 |
|
|
|
199 |
|
|
|
200 |
|
|
Element row = rows.get(i); |
201 |
|
|
Elements fields = row.getElementsByTag("td"); |
202 |
|
|
|
203 |
|
|
|
204 |
|
|
if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { |
205 |
|
|
currentStation = true; |
206 |
|
|
continue; |
207 |
|
|
} |
208 |
|
|
|
209 |
torben |
1060 |
TimetableEntry entry = new TimetableEntry(); |
210 |
torben |
1036 |
|
211 |
torben |
1040 |
String station = DepartureFetcher.cleanText( fields.get(0).text() ) ; |
212 |
torben |
1036 |
if (station.equals("København")) |
213 |
|
|
station = "København H"; //correct inconsistency in naming |
214 |
|
|
|
215 |
torben |
1040 |
String arrival = DepartureFetcher.cleanText( fields.get(1).text() ); |
216 |
|
|
String departure = DepartureFetcher.cleanText( fields.get(2).text() ); |
217 |
|
|
|
218 |
torben |
1060 |
entry.setStation( station ); |
219 |
|
|
entry.setArrival( arrival ); |
220 |
|
|
entry.setDeparture( departure ); |
221 |
torben |
1036 |
|
222 |
|
|
|
223 |
|
|
if (currentStation == true && currentStationSaved == false ) { |
224 |
torben |
1060 |
entry.setCurrent(currentStation); |
225 |
torben |
1036 |
currentStationSaved = true; |
226 |
|
|
} |
227 |
|
|
|
228 |
torben |
1060 |
entry.setStationId( getStationId( station )); |
229 |
torben |
1036 |
|
230 |
torben |
1060 |
timetableBean.entries.add(entry); |
231 |
torben |
1036 |
} |
232 |
|
|
|
233 |
|
|
} else { |
234 |
|
|
logger.warning("No time table found, trainID=" + trainID + " type=" + type); |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
|
238 |
torben |
1060 |
return timetableBean; |
239 |
torben |
1036 |
} |
240 |
torben |
350 |
|
241 |
|
|
} |