1 |
package dk.thoerup.traininfoservice.banedk; |
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; |
import java.util.ArrayList; |
7 |
|
import java.util.Collections; |
8 |
import java.util.List; |
import java.util.List; |
9 |
|
|
10 |
import com.gargoylesoftware.htmlunit.ProxyConfig; |
import com.gargoylesoftware.htmlunit.ProxyConfig; |
13 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
14 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
15 |
|
|
16 |
|
import dk.thoerup.traininfoservice.DBConnection; |
17 |
|
|
18 |
public class DepartureFetcher { |
public class DepartureFetcher { |
19 |
|
|
20 |
|
|
21 |
static ProxyConfig proxyConfig; |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
22 |
static { |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
23 |
proxyConfig = new ProxyConfig(); |
|
24 |
proxyConfig.setProxyHost("rafiki.t-hoerup.dk"); |
Connection conn = null; |
25 |
proxyConfig.setProxyPort(3128); |
try |
26 |
|
{ |
27 |
|
conn = DBConnection.getConnection(); |
28 |
|
|
29 |
|
String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID; |
30 |
|
Statement stmt = conn.createStatement(); |
31 |
|
ResultSet rs = stmt.executeQuery(SQL); |
32 |
|
|
33 |
|
if (rs.next()) { |
34 |
|
String code = rs.getString( 1 ); |
35 |
|
if (! rs.wasNull() ) { |
36 |
|
List<DepartureBean> list = lookupDepartures(code, "FJRN"); |
37 |
|
departureList.addAll(list); |
38 |
|
} |
39 |
|
|
40 |
|
code = rs.getString(2); |
41 |
|
if (! rs.wasNull() ) { |
42 |
|
List<DepartureBean> list = lookupDepartures(code, "S2"); |
43 |
|
departureList.addAll(list); |
44 |
|
} |
45 |
|
Collections.sort( departureList ); |
46 |
|
|
47 |
|
} |
48 |
|
|
49 |
|
} finally { |
50 |
|
if (conn != null && !conn.isClosed() ) { |
51 |
|
conn.close(); |
52 |
|
} |
53 |
|
} |
54 |
|
|
55 |
|
return departureList; |
56 |
} |
} |
57 |
|
|
58 |
public List<DepartureBean> lookupDepartures() throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
59 |
|
|
60 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
61 |
|
|
62 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
63 |
webClient.setTimeout(1000); |
webClient.setTimeout(1000); |
64 |
webClient.setProxyConfig(proxyConfig); |
webClient.setJavaScriptEnabled(false); |
65 |
webClient.setJavaScriptEnabled(false); |
|
66 |
|
|
67 |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=FJRN&S=BJ"); |
final HtmlPage page = webClient.getPage("http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode); |
68 |
|
|
69 |
HtmlElement table = page.getElementById("afgangtabel"); |
HtmlElement table = page.getElementById("afgangtabel"); |
|
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
|
70 |
|
|
71 |
for (HtmlElement currentRow : tableRows) { |
if (table != null) { |
72 |
String rowClass = currentRow.getAttribute("class"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
73 |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
|
74 |
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
for (HtmlElement currentRow : tableRows) { |
75 |
|
String rowClass = currentRow.getAttribute("class"); |
76 |
DepartureBean departure = new DepartureBean(); |
if (rowClass != null && rowClass.toLowerCase().contains("station") ) { |
77 |
|
DomNodeList<HtmlElement> fields = currentRow.getElementsByTagName("td"); |
78 |
String time = fields.get(0).asText(); |
|
79 |
departure.setTime(time); |
DepartureBean departure = new DepartureBean(); |
80 |
|
|
81 |
int updated = extractUpdated( fields.get(1) ); |
String time = fields.get(0).asText(); |
82 |
departure.setUpdated(updated); |
departure.setTime(time); |
83 |
|
|
84 |
String trainNumber = fields.get(2).asText(); |
int updated = extractUpdated( fields.get(1) ); |
85 |
departure.setTrainNumber(trainNumber); |
departure.setUpdated(updated); |
86 |
|
|
87 |
String destination = fields.get(3).asText(); |
String trainNumber = fields.get(2).asText(); |
88 |
departure.setDestination(destination); |
departure.setTrainNumber(trainNumber); |
89 |
|
|
90 |
String origin = fields.get(4).asText(); |
String destination = fields.get(3).asText(); |
91 |
departure.setOrigin(origin); |
departure.setDestination(destination); |
92 |
|
|
93 |
String location = fields.get(5).asText(); |
String origin = fields.get(4).asText(); |
94 |
departure.setLocation(location); |
departure.setOrigin(origin); |
95 |
|
|
96 |
String status = fields.get(6).asText(); |
String location = fields.get(5).asText(); |
97 |
departure.setStatus(status); |
departure.setLocation(location); |
98 |
|
|
99 |
String note = fields.get(7).asText(); |
String status = fields.get(6).asText(); |
100 |
departure.setNote(note); |
departure.setStatus(status); |
101 |
|
|
102 |
departureList.add(departure); |
String note = extractNote( fields.get(7) ); |
103 |
} |
departure.setNote(note); |
104 |
|
|
105 |
|
departureList.add(departure); |
106 |
|
} |
107 |
|
} |
108 |
} |
} |
|
|
|
109 |
return departureList; |
return departureList; |
110 |
} |
} |
111 |
|
|
127 |
return updated; |
return updated; |
128 |
} |
} |
129 |
|
|
130 |
|
private String extractNote(HtmlElement noteTd) { |
131 |
|
String note = noteTd.asText().trim(); |
132 |
|
|
133 |
|
List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype"); |
134 |
|
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
135 |
|
note = note.substring(0,note.length() -1 ); |
136 |
|
|
137 |
|
return note; |
138 |
|
} |
139 |
|
|
140 |
//test |
//test |
141 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception{ |
142 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
143 |
List<DepartureBean> deps = f.lookupDepartures(); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
144 |
for(DepartureBean d : deps) { |
for(DepartureBean d : deps) { |
145 |
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + |
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + |
146 |
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() ); |
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() ); |