6 |
import java.util.ArrayList; |
import java.util.ArrayList; |
7 |
import java.util.Collections; |
import java.util.Collections; |
8 |
import java.util.List; |
import java.util.List; |
9 |
|
import java.util.logging.Logger; |
10 |
|
|
|
import com.gargoylesoftware.htmlunit.ProxyConfig; |
|
11 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
12 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
13 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
14 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
15 |
|
|
16 |
|
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
17 |
|
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
18 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.DBConnection; |
19 |
|
|
20 |
public class DepartureFetcher { |
public class DepartureFetcher { |
21 |
|
|
22 |
|
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
23 |
|
|
24 |
|
TimeoutCache<Integer, List<DepartureBean>> cache = new TimeoutCache<Integer,List<DepartureBean>>(120 * 1000); |
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Throwable { |
29 |
|
|
30 |
|
List<DepartureBean> list = cache.get(stationID); |
31 |
|
|
32 |
|
if (list == null) { |
33 |
|
list = lookupDepartures(stationID); |
34 |
|
cache.put(stationID, list); |
35 |
|
} else { |
36 |
|
logger.info("Departure: Cache hit " + stationID); //remove before production |
37 |
|
} |
38 |
|
return list; |
39 |
|
} |
40 |
|
|
41 |
|
|
42 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> lookupDepartures(int stationID) throws Throwable { |
43 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
44 |
|
|
45 |
Connection conn = null; |
Connection conn = null; |
76 |
return departureList; |
return departureList; |
77 |
} |
} |
78 |
|
|
79 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Throwable { |
80 |
|
|
81 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
82 |
|
|
83 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
84 |
webClient.setTimeout(1000); |
webClient.setTimeout(2500); |
85 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
86 |
|
|
87 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
88 |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
89 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
90 |
|
|
91 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
92 |
|
|
93 |
HtmlElement table = page.getElementById("afgangtabel"); |
HtmlElement table = page.getElementById("afgangtabel"); |
94 |
|
|
103 |
DepartureBean departure = new DepartureBean(); |
DepartureBean departure = new DepartureBean(); |
104 |
|
|
105 |
String time = fields.get(0).asText(); |
String time = fields.get(0).asText(); |
106 |
|
if (time.equals("")) |
107 |
|
time = "0:00"; //Bane.dk bug work-around |
108 |
departure.setTime(time); |
departure.setTime(time); |
109 |
|
|
110 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
111 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
112 |
|
|
113 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).asText(); |
114 |
|
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
115 |
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
116 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
117 |
|
|
118 |
String destination = fields.get(3).asText(); |
String destination = fields.get(3).asText(); |
133 |
departureList.add(departure); |
departureList.add(departure); |
134 |
} |
} |
135 |
} |
} |
136 |
|
} else { |
137 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
138 |
} |
} |
139 |
|
|
140 |
return departureList; |
return departureList; |
141 |
} |
} |
142 |
|
|
168 |
return note; |
return note; |
169 |
} |
} |
170 |
|
|
171 |
|
private String extractTrainNumber(HtmlElement trainTd) { |
172 |
|
String number = ""; |
173 |
|
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
174 |
|
String href = anchorElement.getAttribute("href"); |
175 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
176 |
|
|
177 |
|
String args[] = argstring.split("&"); |
178 |
|
for (String arg : args) { |
179 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
180 |
|
|
181 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
182 |
|
number = pair[1]; |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
return number; |
188 |
|
} |
189 |
|
|
190 |
//test |
//test |
191 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Throwable { |
192 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
193 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
194 |
for(DepartureBean d : deps) { |
for(DepartureBean d : deps) { |