1 |
package dk.thoerup.traininfoservice.banedk;
|
2 |
|
3 |
import java.sql.Connection;
|
4 |
import java.sql.ResultSet;
|
5 |
import java.sql.Statement;
|
6 |
import java.util.ArrayList;
|
7 |
import java.util.Collections;
|
8 |
import java.util.List;
|
9 |
import java.util.logging.Logger;
|
10 |
|
11 |
import com.gargoylesoftware.htmlunit.ProxyConfig;
|
12 |
import com.gargoylesoftware.htmlunit.WebClient;
|
13 |
import com.gargoylesoftware.htmlunit.html.DomNodeList;
|
14 |
import com.gargoylesoftware.htmlunit.html.HtmlElement;
|
15 |
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
16 |
|
17 |
import dk.thoerup.traininfoservice.DBConnection;
|
18 |
|
19 |
public class DepartureFetcher {
|
20 |
|
21 |
Logger logger = Logger.getLogger(DepartureFetcher.class.getName());
|
22 |
|
23 |
|
24 |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception {
|
25 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>();
|
26 |
|
27 |
Connection conn = null;
|
28 |
try
|
29 |
{
|
30 |
conn = DBConnection.getConnection();
|
31 |
|
32 |
String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID;
|
33 |
Statement stmt = conn.createStatement();
|
34 |
ResultSet rs = stmt.executeQuery(SQL);
|
35 |
|
36 |
if (rs.next()) {
|
37 |
String code = rs.getString( 1 );
|
38 |
if (! rs.wasNull() ) {
|
39 |
List<DepartureBean> list = lookupDepartures(code, "FJRN");
|
40 |
departureList.addAll(list);
|
41 |
}
|
42 |
|
43 |
code = rs.getString(2);
|
44 |
if (! rs.wasNull() ) {
|
45 |
List<DepartureBean> list = lookupDepartures(code, "S2");
|
46 |
departureList.addAll(list);
|
47 |
}
|
48 |
Collections.sort( departureList );
|
49 |
|
50 |
}
|
51 |
|
52 |
} finally {
|
53 |
if (conn != null && !conn.isClosed() ) {
|
54 |
conn.close();
|
55 |
}
|
56 |
}
|
57 |
|
58 |
return departureList;
|
59 |
}
|
60 |
|
61 |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception {
|
62 |
|
63 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>();
|
64 |
|
65 |
final WebClient webClient = new WebClient();
|
66 |
webClient.setTimeout(1000);
|
67 |
webClient.setJavaScriptEnabled(false);
|
68 |
|
69 |
|
70 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode);
|
71 |
|
72 |
HtmlElement table = page.getElementById("afgangtabel");
|
73 |
|
74 |
if (table != null) {
|
75 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr");
|
76 |
|
77 |
for (HtmlElement currentRow : tableRows) {
|
78 |
String rowClass = currentRow.getAttribute("class");
|
79 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) {
|
80 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td");
|
81 |
|
82 |
DepartureBean departure = new DepartureBean();
|
83 |
|
84 |
String time = fields.get(0).asText();
|
85 |
departure.setTime(time);
|
86 |
|
87 |
int updated = extractUpdated( fields.get(1) );
|
88 |
departure.setUpdated(updated);
|
89 |
|
90 |
String trainNumber = fields.get(2).asText();
|
91 |
if (trainNumber.trim().length() == 1)
|
92 |
trainNumber = trainNumber + " " + extractTrainNumber(fields.get(2));
|
93 |
departure.setTrainNumber(trainNumber);
|
94 |
|
95 |
String destination = fields.get(3).asText();
|
96 |
departure.setDestination(destination);
|
97 |
|
98 |
String origin = fields.get(4).asText();
|
99 |
departure.setOrigin(origin);
|
100 |
|
101 |
String location = fields.get(5).asText();
|
102 |
departure.setLocation(location);
|
103 |
|
104 |
String status = fields.get(6).asText();
|
105 |
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;
|
118 |
}
|
119 |
|
120 |
private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif"
|
121 |
int updated = -1;
|
122 |
|
123 |
DomNodeList<HtmlElement> updatedImgs = updatedTd.getElementsByTagName("img");
|
124 |
String updatedStr = updatedImgs.get(0).getAttribute("src");
|
125 |
|
126 |
if (updatedStr != null) {
|
127 |
for (int i=0; i<updatedStr.length(); i++) {
|
128 |
char c = updatedStr.charAt(i);
|
129 |
if ( Character.isDigit(c)) {
|
130 |
updated = Character.digit(c, 10);
|
131 |
break;
|
132 |
}
|
133 |
}
|
134 |
}
|
135 |
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
|
168 |
public static void main(String args[]) throws Exception{
|
169 |
DepartureFetcher f = new DepartureFetcher();
|
170 |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN");
|
171 |
for(DepartureBean d : deps) {
|
172 |
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" +
|
173 |
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );
|
174 |
}
|
175 |
|
176 |
System.out.println("--------------------------");
|
177 |
}
|
178 |
}
|