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 com.gargoylesoftware.htmlunit.ProxyConfig; |
|
10 |
import com.gargoylesoftware.htmlunit.WebClient; |
import com.gargoylesoftware.htmlunit.WebClient; |
11 |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
import com.gargoylesoftware.htmlunit.html.DomNodeList; |
12 |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
import com.gargoylesoftware.htmlunit.html.HtmlElement; |
13 |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
import com.gargoylesoftware.htmlunit.html.HtmlPage; |
14 |
|
|
15 |
|
import dk.thoerup.traininfoservice.DBConnection; |
16 |
|
|
17 |
public class DepartureFetcher { |
public class DepartureFetcher { |
18 |
|
|
19 |
|
@SuppressWarnings("unchecked") |
20 |
static ProxyConfig proxyConfig; |
public List<DepartureBean> lookupDepartures(int stationID) throws Exception { |
21 |
static { |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
22 |
proxyConfig = new ProxyConfig(); |
|
23 |
proxyConfig.setProxyHost("rafiki.t-hoerup.dk"); |
Connection conn = null; |
24 |
proxyConfig.setProxyPort(3128); |
try |
25 |
|
{ |
26 |
|
conn = DBConnection.getConnection(); |
27 |
|
|
28 |
|
String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID; |
29 |
|
Statement stmt = conn.createStatement(); |
30 |
|
ResultSet rs = stmt.executeQuery(SQL); |
31 |
|
|
32 |
|
if (rs.next()) { |
33 |
|
String code = rs.getString( 1 ); |
34 |
|
if (! rs.wasNull() ) { |
35 |
|
List<DepartureBean> list = lookupDepartures(code, "FJRN"); |
36 |
|
departureList.addAll(list); |
37 |
|
} |
38 |
|
|
39 |
|
code = rs.getString(2); |
40 |
|
if (! rs.wasNull() ) { |
41 |
|
List<DepartureBean> list = lookupDepartures(code, "S2"); |
42 |
|
departureList.addAll(list); |
43 |
|
} |
44 |
|
Collections.sort( departureList ); |
45 |
|
|
46 |
|
} |
47 |
|
|
48 |
|
} finally { |
49 |
|
if (conn != null && !conn.isClosed() ) { |
50 |
|
conn.close(); |
51 |
|
} |
52 |
|
} |
53 |
|
|
54 |
|
return departureList; |
55 |
} |
} |
56 |
|
|
57 |
public List<DepartureBean> lookupDepartures() throws Exception { |
public List<DepartureBean> lookupDepartures(String stationcode, String type) throws Exception { |
58 |
|
|
59 |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
List<DepartureBean> departureList = new ArrayList<DepartureBean>(); |
60 |
|
|
61 |
final WebClient webClient = new WebClient(); |
final WebClient webClient = new WebClient(); |
62 |
webClient.setTimeout(1000); |
webClient.setTimeout(1000); |
|
webClient.setProxyConfig(proxyConfig); |
|
63 |
webClient.setJavaScriptEnabled(false); |
webClient.setJavaScriptEnabled(false); |
64 |
|
|
65 |
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); |
66 |
|
|
67 |
HtmlElement table = page.getElementById("afgangtabel"); |
HtmlElement table = page.getElementById("afgangtabel"); |
68 |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
DomNodeList<HtmlElement> tableRows = table.getElementsByTagName("tr"); |
126 |
//test |
//test |
127 |
public static void main(String args[]) throws Exception{ |
public static void main(String args[]) throws Exception{ |
128 |
DepartureFetcher f = new DepartureFetcher(); |
DepartureFetcher f = new DepartureFetcher(); |
129 |
List<DepartureBean> deps = f.lookupDepartures(); |
List<DepartureBean> deps = f.lookupDepartures("AR", "FJRN"); |
130 |
for(DepartureBean d : deps) { |
for(DepartureBean d : deps) { |
131 |
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + |
System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + |
132 |
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() ); |
d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() ); |