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 |
|
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; |
14 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
15 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
16 |
|
|
17 |
|
import dk.thoerup.traininfoservice.DBConnection; |
18 |
|
|
19 |
public class DepartureFetcher { |
public class DepartureFetcher { |
20 |
|
|
21 |
|
Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); |
22 |
static ProxyConfig proxyConfig; |
|
23 |
static { |
|
24 |
proxyConfig = new ProxyConfig(); |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
25 |
proxyConfig.setProxyHost("rafiki.t-hoerup.dk"); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
26 |
proxyConfig.setProxyPort(3128); |
|
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() throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
62 |
|
|
63 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
64 |
|
|
65 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
66 |
webClient.setTimeout(1000); |
webClient.setTimeout(1000); |
67 |
webClient.setProxyConfig(proxyConfig); |
webClient.setJavaScriptEnabled(false); |
68 |
webClient.setJavaScriptEnabled(false); |
|
69 |
|
|
70 |
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); |
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); |
departure.setTrainNumber(trainNumber); |
92 |
|
|
93 |
String origin = fields.get(4).asText(); |
String destination = fields.get(3).asText(); |
94 |
departure.setOrigin(origin); |
departure.setDestination(destination); |
95 |
|
|
96 |
String location = fields.get(5).asText(); |
String origin = fields.get(4).asText(); |
97 |
departure.setLocation(location); |
departure.setOrigin(origin); |
98 |
|
|
99 |
String status = fields.get(6).asText(); |
String location = fields.get(5).asText(); |
100 |
departure.setStatus(status); |
departure.setLocation(location); |
101 |
|
|
102 |
String note = fields.get(7).asText(); |
String status = fields.get(6).asText(); |
103 |
departure.setNote(note); |
departure.setStatus(status); |
104 |
|
|
105 |
departureList.add(departure); |
String note = extractNote( fields.get(7) ); |
106 |
} |
departure.setNote(note); |
107 |
|
|
108 |
|
departureList.add(departure); |
109 |
|
} |
110 |
|
} |
111 |
|
} else { |
112 |
|
logger.warning("No departures found for station=" + stationcode + ", type=" + type); |
113 |
} |
} |
114 |
|
|
115 |
return departureList; |
return departureList; |
133 |
return updated; |
return updated; |
134 |
} |
} |
135 |
|
|
136 |
|
private String extractNote(HtmlElement noteTd) { |
137 |
|
String note = noteTd.asText().trim(); |
138 |
|
|
139 |
|
List<HtmlElement> elems = noteTd.getElementsByAttribute("span", "class", "bemtype"); |
140 |
|
if (elems.size() > 0 && note.charAt(note.length()-1) == 'i') |
141 |
|
note = note.substring(0,note.length() -1 ); |
142 |
|
|
143 |
|
return note; |
144 |
|
} |
145 |
|
|
146 |
//test |
//test |
147 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception{ |
148 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
149 |
List<DepartureBean> deps = f.lookupDepartures(); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
150 |
for(DepartureBean d : deps) { |
for(DepartureBean d : deps) { |
151 |
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + |
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + |
152 |
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() ); |
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() ); |