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 { |
82 |
DepartureBean departure = new DepartureBean(); |
DepartureBean departure = new DepartureBean(); |
83 |
|
|
84 |
String time = fields.get(0).asText(); |
String time = fields.get(0).asText(); |
85 |
|
if (time.equals("")) |
86 |
|
time = "0:00"; //Bane.dk bug work-around |
87 |
departure.setTime(time); |
departure.setTime(time); |
88 |
|
|
89 |
int updated = extractUpdated( fields.get(1) ); |
int updated = extractUpdated( fields.get(1) ); |
90 |
departure.setUpdated(updated); |
departure.setUpdated(updated); |
91 |
|
|
92 |
String trainNumber = fields.get(2).asText(); |
String trainNumber = fields.get(2).asText(); |
93 |
|
if (trainNumber.trim().length() == 1) |
94 |
|
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2)); |
95 |
departure.setTrainNumber(trainNumber); |
departure.setTrainNumber(trainNumber); |
96 |
|
|
97 |
String destination = fields.get(3).asText(); |
String destination = fields.get(3).asText(); |
112 |
departureList.add(departure); |
departureList.add(departure); |
113 |
} |
} |
114 |
} |
} |
115 |
|
} else { |
116 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
117 |
} |
} |
118 |
|
|
119 |
return departureList; |
return departureList; |
120 |
} |
} |
121 |
|
|
147 |
return note; |
return note; |
148 |
} |
} |
149 |
|
|
150 |
|
private String extractTrainNumber(HtmlElement trainTd) { |
151 |
|
String number = ""; |
152 |
|
HtmlElement anchorElement = trainTd.getElementsByTagName("a").get(0); |
153 |
|
String href = anchorElement.getAttribute("href"); |
154 |
|
String argstring = href.substring( href.indexOf('?') + 1); |
155 |
|
|
156 |
|
String args[] = argstring.split("&"); |
157 |
|
for (String arg : args) { |
158 |
|
String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] |
159 |
|
|
160 |
|
if (pair[0].equalsIgnoreCase("TogNr")) |
161 |
|
number = pair[1]; |
162 |
|
} |
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
return number; |
167 |
|
} |
168 |
|
|
169 |
//test |
//test |
170 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception{ |
171 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |