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; |
import com.gargoylesoftware.htmlunit.ProxyConfig; |
12 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
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 { |
88 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
89 |
|
|
90 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).asText(); |
91 |
|
if (trainNumber.trim().length() == 1) |
92 |
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
93 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
94 |
|
|
95 |
String destination = fields.get(3).asText(); |
String destination = fields.get(3).asText(); |
110 |
departureList.add(departure); |
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; |
118 |
} |
} |
119 |
|
|
145 |
return note; |
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(); |