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 |
|
|
24 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
64 |
|
|
65 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
66 |
webClient.setTimeout(1000); |
webClient.setTimeout(1000); |
67 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
68 |
|
|
69 |
|
|
70 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
71 |
|
|
72 |
HtmlElement table = page.getElementById("afgangtabel"); |
HtmlElement table = page.getElementById("afgangtabel"); |
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
|
73 |
|
|
74 |
for (HtmlElement currentRow : tableRows) { |
if (table != null) { |
75 |
String rowClass = currentRow.getAttribute("class"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
76 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
|
77 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
for (HtmlElement currentRow : tableRows) { |
78 |
|
String rowClass = currentRow.getAttribute("class"); |
79 |
DepartureBean departure = new DepartureBean(); |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
80 |
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
81 |
String time = fields.get(0).asText(); |
|
82 |
departure.setTime(time); |
DepartureBean departure = new DepartureBean(); |
83 |
|
|
84 |
int updated = extractUpdated( fields.get(1) ); |
String time = fields.get(0).asText(); |
85 |
departure.setUpdated(updated); |
departure.setTime(time); |
86 |
|
|
87 |
String trainNumber = fields.get(2).asText(); |
int updated = extractUpdated( fields.get(1) ); |
88 |
departure.setTrainNumber(trainNumber); |
departure.setUpdated(updated); |
89 |
|
|
90 |
String destination = fields.get(3).asText(); |
String trainNumber = fields.get(2).asText(); |
91 |
departure.setDestination(destination); |
if (trainNumber.trim().length() == 1) |
92 |
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
93 |
String origin = fields.get(4).asText(); |
departure.setTrainNumber(trainNumber); |
94 |
departure.setOrigin(origin); |
|
95 |
|
String destination = fields.get(3).asText(); |
96 |
String location = fields.get(5).asText(); |
departure.setDestination(destination); |
97 |
departure.setLocation(location); |
|
98 |
|
String origin = fields.get(4).asText(); |
99 |
String status = fields.get(6).asText(); |
departure.setOrigin(origin); |
100 |
departure.setStatus(status); |
|
101 |
|
String location = fields.get(5).asText(); |
102 |
String note = fields.get(7).asText(); |
departure.setLocation(location); |
103 |
departure.setNote(note); |
|
104 |
|
String status = fields.get(6).asText(); |
105 |
departureList.add(departure); |
departure.setStatus(status); |
106 |
} |
|
107 |
|
String note = extractNote( fields.get(7) ); |
108 |
|
departure.setNote(note); |
109 |
|
|
110 |
|
departureList.add(departure); |
111 |
|
} |
112 |
|
} |
113 |
|
} else { |
114 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
115 |
} |
} |
116 |
|
|
117 |
return departureList; |
return departureList; |
135 |
return updated; |
return updated; |
136 |
} |
} |
137 |
|
|
138 |
|
private String extractNote(HtmlElement noteTd) { |
139 |
|
String note = noteTd.asText().trim(); |
140 |
|
|
141 |
|
List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype"); |
142 |
|
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
143 |
|
note = note.substring(0,note.length() -1 ); |
144 |
|
|
145 |
|
return note; |
146 |
|
} |
147 |
|
|
148 |
|
private String extractTrainNumber(HtmlElement trainTd) { |
149 |
|
String number = ""; |
150 |
|
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
151 |
|
String href = anchorElement.getAttribute("href"); |
152 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
153 |
|
|
154 |
|
String args[] = argstring.split("&"); |
155 |
|
for (String arg : args) { |
156 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
157 |
|
|
158 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
159 |
|
number = pair[1]; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
return number; |
165 |
|
} |
166 |
|
|
167 |
//test |
//test |
168 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception{ |
169 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |