1 |
package dk.thoerup.traininfoservice.banedk; |
package dk.thoerup.traininfoservice.banedk; |
2 |
|
|
|
import java.sql.Connection; |
|
|
import java.sql.ResultSet; |
|
|
import java.sql.Statement; |
|
3 |
import java.util.ArrayList; |
import java.util.ArrayList; |
4 |
import java.util.Collections; |
import java.util.Collections; |
5 |
import java.util.List; |
import java.util.List; |
6 |
|
import java.util.Map; |
7 |
|
import java.util.logging.Logger; |
8 |
|
|
9 |
import com.gargoylesoftware.htmlunit.ProxyConfig; |
import com.gargoylesoftware.htmlunit.BrowserVersion; |
10 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
11 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
12 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
13 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
14 |
|
|
15 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
16 |
|
import dk.thoerup.circuitbreaker.CircuitBreakerManager; |
17 |
|
import dk.thoerup.traininfoservice.StationBean; |
18 |
|
import dk.thoerup.traininfoservice.StationDAO; |
19 |
|
|
20 |
public class DepartureFetcher { |
public class DepartureFetcher { |
21 |
|
|
22 |
|
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
23 |
|
|
24 |
|
Map<Integer, List<DepartureBean>> cache; |
25 |
|
|
26 |
|
StationDAO stationDao = new StationDAO(); |
27 |
|
|
28 |
|
private boolean useTempSite; |
29 |
|
|
30 |
|
public DepartureFetcher(boolean tempSite, int cacheTimeout) { |
31 |
|
useTempSite = tempSite; |
32 |
|
cache = new TimeoutMap<Integer,List<DepartureBean>>(cacheTimeout); |
33 |
|
} |
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception { |
39 |
|
|
40 |
|
List<DepartureBean> list = cache.get(stationID); |
41 |
|
|
42 |
|
if (list == null) { |
43 |
|
list = lookupDepartures(stationID); |
44 |
|
cache.put(stationID, list); |
45 |
|
} else { |
46 |
|
logger.info("Departure: Cache hit " + stationID); //remove before production |
47 |
|
} |
48 |
|
return list; |
49 |
|
} |
50 |
|
|
51 |
|
|
52 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
53 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
54 |
|
|
55 |
Connection conn = null; |
StationBean station = stationDao.getById(stationID); |
56 |
try |
|
57 |
{ |
if (station.getRegional() != null) { |
58 |
conn = DBConnection.getConnection(); |
List<DepartureBean> list = lookupDepartures(station.getRegional(), "FJRN"); |
59 |
|
departureList.addAll(list); |
|
String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID; |
|
|
Statement stmt = conn.createStatement(); |
|
|
ResultSet rs = stmt.executeQuery(SQL); |
|
|
|
|
|
if (rs.next()) { |
|
|
String code = rs.getString( 1 ); |
|
|
if (! rs.wasNull() ) { |
|
|
List<DepartureBean> list = lookupDepartures(code, "FJRN"); |
|
|
departureList.addAll(list); |
|
|
} |
|
|
|
|
|
code = rs.getString(2); |
|
|
if (! rs.wasNull() ) { |
|
|
List<DepartureBean> list = lookupDepartures(code, "S2"); |
|
|
departureList.addAll(list); |
|
|
} |
|
|
Collections.sort( departureList ); |
|
|
|
|
|
} |
|
|
|
|
|
} finally { |
|
|
if (conn != null && !conn.isClosed() ) { |
|
|
conn.close(); |
|
|
} |
|
60 |
} |
} |
61 |
|
|
62 |
|
if (station.getStrain() != null) { |
63 |
|
List<DepartureBean> list = lookupDepartures(station.getStrain(), "S2"); |
64 |
|
departureList.addAll(list); |
65 |
|
} |
66 |
|
|
67 |
|
Collections.sort( departureList ); |
68 |
|
|
69 |
|
|
70 |
return departureList; |
return departureList; |
71 |
} |
} |
72 |
|
|
73 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
74 |
|
if (useTempSite == false) { |
75 |
|
return lookupDeparturesNormalSite(stationcode, type); |
76 |
|
} else { |
77 |
|
return lookupDeparturesFromTemporarySite(stationcode, type); |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
|
public List<DepartureBean> lookupDeparturesNormalSite(String stationcode, String type) throws Exception { |
82 |
|
|
83 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
84 |
|
|
85 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient( BrowserVersion.FIREFOX_3 ); |
86 |
webClient.setTimeout(1000); |
webClient.setTimeout(2500); |
87 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
88 |
|
|
89 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
90 |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
91 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
92 |
|
|
93 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
94 |
|
|
95 |
HtmlElement table = page.getElementById("afgangtabel"); |
HtmlElement table = page.getElementById("afgangtabel"); |
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
|
96 |
|
|
97 |
for (HtmlElement currentRow : tableRows) { |
if (table != null) { |
98 |
String rowClass = currentRow.getAttribute("class"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
99 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
|
100 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
for (HtmlElement currentRow : tableRows) { |
101 |
|
String rowClass = currentRow.getAttribute("class"); |
102 |
DepartureBean departure = new DepartureBean(); |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
103 |
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
104 |
String time = fields.get(0).asText(); |
|
105 |
departure.setTime(time); |
DepartureBean departure = new DepartureBean(); |
106 |
|
|
107 |
int updated = extractUpdated( fields.get(1) ); |
String time = fields.get(0).asText(); |
108 |
departure.setUpdated(updated); |
if (time.equals("")) |
109 |
|
time = "0:00"; //Bane.dk bug work-around |
110 |
String trainNumber = fields.get(2).asText(); |
departure.setTime(time); |
111 |
departure.setTrainNumber(trainNumber); |
|
112 |
|
int updated = extractUpdated( fields.get(1) ); |
113 |
String destination = fields.get(3).asText(); |
departure.setUpdated(updated); |
114 |
departure.setDestination(destination); |
|
115 |
|
String trainNumber = fields.get(2).asText(); |
116 |
String origin = fields.get(4).asText(); |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
117 |
departure.setOrigin(origin); |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
118 |
|
departure.setTrainNumber(trainNumber); |
119 |
String location = fields.get(5).asText(); |
|
120 |
departure.setLocation(location); |
String destination = fields.get(3).asText(); |
121 |
|
departure.setDestination(destination); |
122 |
String status = fields.get(6).asText(); |
|
123 |
departure.setStatus(status); |
String origin = fields.get(4).asText(); |
124 |
|
departure.setOrigin(origin); |
125 |
String note = extractNote( fields.get(7) ); |
|
126 |
departure.setNote(note); |
String location = fields.get(5).asText(); |
127 |
|
departure.setLocation(location); |
128 |
departureList.add(departure); |
|
129 |
} |
String status = fields.get(6).asText(); |
130 |
|
departure.setStatus(status); |
131 |
|
|
132 |
|
String note = extractNote( fields.get(7) ); |
133 |
|
departure.setNote(note); |
134 |
|
|
135 |
|
departure.setType(type); |
136 |
|
|
137 |
|
departureList.add(departure); |
138 |
|
} |
139 |
|
} |
140 |
|
} else { |
141 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
142 |
} |
} |
143 |
|
webClient.closeAllWindows(); |
144 |
|
|
145 |
return departureList; |
return departureList; |
146 |
} |
} |
147 |
|
|
148 |
|
public List<DepartureBean> lookupDeparturesFromTemporarySite(String stationcode, String type) throws Exception { |
149 |
|
|
150 |
|
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
151 |
|
|
152 |
|
final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); |
153 |
|
webClient.setTimeout(2500); |
154 |
|
webClient.setJavaScriptEnabled(false); |
155 |
|
|
156 |
|
|
157 |
|
String uri = "http://bane.dk/lite/station.asp?w=" + type + "&s=" + stationcode; |
158 |
|
|
159 |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
160 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
161 |
|
|
162 |
|
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
163 |
|
|
164 |
|
HtmlElement table = page.getElementById("traf_afgang"); |
165 |
|
|
166 |
|
if (table != null) { |
167 |
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
168 |
|
|
169 |
|
boolean isFirst = true; |
170 |
|
|
171 |
|
for (HtmlElement currentRow : tableRows) { |
172 |
|
if (isFirst == true) { //skip table headers |
173 |
|
isFirst = false; |
174 |
|
continue; |
175 |
|
} |
176 |
|
|
177 |
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
178 |
|
|
179 |
|
DepartureBean departure = new DepartureBean(); |
180 |
|
|
181 |
|
String time = fields.get(0).asText().trim(); |
182 |
|
|
183 |
|
if (time.equals("")) |
184 |
|
time = "0:00"; //Bane.dk bug work-around |
185 |
|
departure.setTime(time); |
186 |
|
|
187 |
|
|
188 |
|
String trainNumber = fields.get(1).asText(); |
189 |
|
departure.setTrainNumber(trainNumber); |
190 |
|
|
191 |
|
String destination = fields.get(2).asText(); |
192 |
|
departure.setDestination(destination); |
193 |
|
|
194 |
|
String origin = fields.get(3).asText(); |
195 |
|
departure.setOrigin(origin); |
196 |
|
|
197 |
|
String status = fields.get(4).asText(); |
198 |
|
departure.setStatus(status); |
199 |
|
|
200 |
|
String note = fields.get(5).asText(); |
201 |
|
departure.setNote(note); |
202 |
|
|
203 |
|
departureList.add(departure); |
204 |
|
} |
205 |
|
} else { |
206 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
207 |
|
} |
208 |
|
webClient.closeAllWindows(); |
209 |
|
|
210 |
|
|
211 |
|
return departureList; |
212 |
|
} |
213 |
|
|
214 |
|
|
215 |
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" |
216 |
int updated = -1; |
int updated = -1; |
217 |
|
|
240 |
return note; |
return note; |
241 |
} |
} |
242 |
|
|
243 |
|
private String extractTrainNumber(HtmlElement trainTd) { |
244 |
|
String number = ""; |
245 |
|
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
246 |
|
String href = anchorElement.getAttribute("href"); |
247 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
248 |
|
|
249 |
|
String args[] = argstring.split("&"); |
250 |
|
for (String arg : args) { |
251 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
252 |
|
|
253 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
254 |
|
number = pair[1]; |
255 |
|
} |
256 |
|
|
257 |
|
|
258 |
|
return number; |
259 |
|
} |
260 |
|
|
261 |
//test |
//test |
262 |
public static void main(String args[]) throws Exception{ |
/* |
263 |
|
public static void main(String args[]) throws Exception { |
264 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
265 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
266 |
for(DepartureBean d : deps) { |
for(DepartureBean d : deps) { |
269 |
} |
} |
270 |
|
|
271 |
System.out.println("--------------------------"); |
System.out.println("--------------------------"); |
272 |
} |
}*/ |
273 |
} |
} |