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.Map; |
10 |
import java.util.logging.Logger; |
import java.util.logging.Logger; |
11 |
|
|
|
import com.gargoylesoftware.htmlunit.ProxyConfig; |
|
12 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
13 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
14 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
15 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
16 |
|
|
17 |
|
import dk.thoerup.curcuitbreaker.CircuitBreaker; |
18 |
|
import dk.thoerup.curcuitbreaker.CircuitBreakerManager; |
19 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.DBConnection; |
20 |
|
|
21 |
public class DepartureFetcher { |
public class DepartureFetcher { |
22 |
|
|
23 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
24 |
|
|
25 |
|
Map<Integer, List<DepartureBean>> cache = new TimeoutMap<Integer,List<DepartureBean>>(120 * 1000); |
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Throwable { |
30 |
|
|
31 |
|
List<DepartureBean> list = cache.get(stationID); |
32 |
|
|
33 |
|
if (list == null) { |
34 |
|
list = lookupDepartures(stationID); |
35 |
|
cache.put(stationID, list); |
36 |
|
} else { |
37 |
|
logger.info("Departure: Cache hit " + stationID); //remove before production |
38 |
|
} |
39 |
|
return list; |
40 |
|
} |
41 |
|
|
42 |
|
|
43 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> lookupDepartures(int stationID) throws Throwable { |
44 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
45 |
|
|
46 |
Connection conn = null; |
Connection conn = null; |
77 |
return departureList; |
return departureList; |
78 |
} |
} |
79 |
|
|
80 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Throwable { |
81 |
|
|
82 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
83 |
|
|
84 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
85 |
webClient.setTimeout(1000); |
webClient.setTimeout(2500); |
86 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
87 |
|
|
88 |
|
String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; |
89 |
|
BanedkInvocation wrapper = new BanedkInvocation(webClient, uri); |
90 |
|
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); |
91 |
|
|
92 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
HtmlPage page = (HtmlPage) breaker.invoke(wrapper); |
93 |
|
|
94 |
HtmlElement table = page.getElementById("afgangtabel"); |
HtmlElement table = page.getElementById("afgangtabel"); |
95 |
|
|
104 |
DepartureBean departure = new DepartureBean(); |
DepartureBean departure = new DepartureBean(); |
105 |
|
|
106 |
String time = fields.get(0).asText(); |
String time = fields.get(0).asText(); |
107 |
|
if (time.equals("")) |
108 |
|
time = "0:00"; //Bane.dk bug work-around |
109 |
departure.setTime(time); |
departure.setTime(time); |
110 |
|
|
111 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
112 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
113 |
|
|
114 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).asText(); |
115 |
if (trainNumber.trim().length() == 1) |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
116 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
117 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
118 |
|
|
189 |
} |
} |
190 |
|
|
191 |
//test |
//test |
192 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Throwable { |
193 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
194 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
195 |
for(DepartureBean d : deps) { |
for(DepartureBean d : deps) { |