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; |
11 |
|
|
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.circuitbreaker.CircuitBreaker; |
18 |
|
import dk.thoerup.circuitbreaker.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()); |
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 Exception { |
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 Exception { |
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"); |
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
|
95 |
|
|
96 |
for (HtmlElement currentRow : tableRows) { |
if (table != null) { |
97 |
String rowClass = currentRow.getAttribute("class"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
98 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
|
99 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
for (HtmlElement currentRow : tableRows) { |
100 |
|
String rowClass = currentRow.getAttribute("class"); |
101 |
DepartureBean departure = new DepartureBean(); |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
102 |
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
103 |
String time = fields.get(0).asText(); |
|
104 |
departure.setTime(time); |
DepartureBean departure = new DepartureBean(); |
105 |
|
|
106 |
int updated = extractUpdated( fields.get(1) ); |
String time = fields.get(0).asText(); |
107 |
departure.setUpdated(updated); |
if (time.equals("")) |
108 |
|
time = "0:00"; //Bane.dk bug work-around |
109 |
String trainNumber = fields.get(2).asText(); |
departure.setTime(time); |
110 |
departure.setTrainNumber(trainNumber); |
|
111 |
|
int updated = extractUpdated( fields.get(1) ); |
112 |
String destination = fields.get(3).asText(); |
departure.setUpdated(updated); |
113 |
departure.setDestination(destination); |
|
114 |
|
String trainNumber = fields.get(2).asText(); |
115 |
String origin = fields.get(4).asText(); |
if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber |
116 |
departure.setOrigin(origin); |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
117 |
|
departure.setTrainNumber(trainNumber); |
118 |
String location = fields.get(5).asText(); |
|
119 |
departure.setLocation(location); |
String destination = fields.get(3).asText(); |
120 |
|
departure.setDestination(destination); |
121 |
String status = fields.get(6).asText(); |
|
122 |
departure.setStatus(status); |
String origin = fields.get(4).asText(); |
123 |
|
departure.setOrigin(origin); |
124 |
String note = fields.get(7).asText(); |
|
125 |
departure.setNote(note); |
String location = fields.get(5).asText(); |
126 |
|
departure.setLocation(location); |
127 |
departureList.add(departure); |
|
128 |
} |
String status = fields.get(6).asText(); |
129 |
|
departure.setStatus(status); |
130 |
|
|
131 |
|
String note = extractNote( fields.get(7) ); |
132 |
|
departure.setNote(note); |
133 |
|
|
134 |
|
departureList.add(departure); |
135 |
|
} |
136 |
|
} |
137 |
|
} else { |
138 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
139 |
} |
} |
140 |
|
|
141 |
return departureList; |
return departureList; |
159 |
return updated; |
return updated; |
160 |
} |
} |
161 |
|
|
162 |
|
private String extractNote(HtmlElement noteTd) { |
163 |
|
String note = noteTd.asText().trim(); |
164 |
|
|
165 |
|
List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype"); |
166 |
|
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
167 |
|
note = note.substring(0,note.length() -1 ); |
168 |
|
|
169 |
|
return note; |
170 |
|
} |
171 |
|
|
172 |
|
private String extractTrainNumber(HtmlElement trainTd) { |
173 |
|
String number = ""; |
174 |
|
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
175 |
|
String href = anchorElement.getAttribute("href"); |
176 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
177 |
|
|
178 |
|
String args[] = argstring.split("&"); |
179 |
|
for (String arg : args) { |
180 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
181 |
|
|
182 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
183 |
|
number = pair[1]; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
return number; |
189 |
|
} |
190 |
|
|
191 |
//test |
//test |
192 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception { |
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) { |