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 |
|
|
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; |
17 |
import dk.thoerup.traininfoservice.DBConnection; |
import dk.thoerup.traininfoservice.DBConnection; |
18 |
|
|
19 |
public class DepartureFetcher { |
public class DepartureFetcher { |
20 |
|
|
21 |
|
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
22 |
|
|
23 |
|
TimeoutCache<Integer, List<DepartureBean>> cache = new TimeoutCache<Integer,List<DepartureBean>>(120 * 1000); |
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
public List<DepartureBean> cachedLookupDepartures(int stationID) throws Exception { |
28 |
|
|
29 |
|
List<DepartureBean> list = cache.get(stationID); |
30 |
|
|
31 |
|
if (list == null) { |
32 |
|
logger.warning("Departure: Cache miss " + stationID); //remove before production |
33 |
|
list = lookupDepartures(stationID); |
34 |
|
cache.put(stationID, list); |
35 |
|
} else { |
36 |
|
logger.warning("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 Exception { |
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 |
|
|
100 |
DepartureBean departure = new DepartureBean(); |
DepartureBean departure = new DepartureBean(); |
101 |
|
|
102 |
String time = fields.get(0).asText(); |
String time = fields.get(0).asText(); |
103 |
|
if (time.equals("")) |
104 |
|
time = "0:00"; //Bane.dk bug work-around |
105 |
departure.setTime(time); |
departure.setTime(time); |
106 |
|
|
107 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
108 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
109 |
|
|
110 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).asText(); |
111 |
|
if (trainNumber.trim().length() == 1) |
112 |
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
113 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
114 |
|
|
115 |
String destination = fields.get(3).asText(); |
String destination = fields.get(3).asText(); |
130 |
departureList.add(departure); |
departureList.add(departure); |
131 |
} |
} |
132 |
} |
} |
133 |
|
} else { |
134 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
135 |
} |
} |
136 |
|
|
137 |
return departureList; |
return departureList; |
138 |
} |
} |
139 |
|
|
165 |
return note; |
return note; |
166 |
} |
} |
167 |
|
|
168 |
|
private String extractTrainNumber(HtmlElement trainTd) { |
169 |
|
String number = ""; |
170 |
|
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
171 |
|
String href = anchorElement.getAttribute("href"); |
172 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
173 |
|
|
174 |
|
String args[] = argstring.split("&"); |
175 |
|
for (String arg : args) { |
176 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
177 |
|
|
178 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
179 |
|
number = pair[1]; |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
return number; |
185 |
|
} |
186 |
|
|
187 |
//test |
//test |
188 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception{ |
189 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |