--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2009/09/10 18:10:42 306 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2009/09/10 18:11:53 307 @@ -1,34 +1,68 @@ package dk.thoerup.traininfoservice.banedk; +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.Statement; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import com.gargoylesoftware.htmlunit.ProxyConfig; import com.gargoylesoftware.htmlunit.WebClient; import com.gargoylesoftware.htmlunit.html.DomNodeList; import com.gargoylesoftware.htmlunit.html.HtmlElement; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import dk.thoerup.traininfoservice.DBConnection; + public class DepartureFetcher { - - - static ProxyConfig proxyConfig; - static { - proxyConfig = new ProxyConfig(); - proxyConfig.setProxyHost("rafiki.t-hoerup.dk"); - proxyConfig.setProxyPort(3128); + + @SuppressWarnings("unchecked") + public List lookupDepartures(int stationID) throws Exception { + List departureList = new ArrayList(); + + Connection conn = null; + try + { + conn = DBConnection.getConnection(); + + String SQL = "SELECT stationcode_fjrn, stationcode_stog FROM trainstations WHERE id=" + stationID; + Statement stmt = conn.createStatement(); + ResultSet rs = stmt.executeQuery(SQL); + + if (rs.next()) { + String code = rs.getString( 1 ); + if (! rs.wasNull() ) { + List list = lookupDepartures(code, "FJRN"); + departureList.addAll(list); + } + + code = rs.getString(2); + if (! rs.wasNull() ) { + List list = lookupDepartures(code, "S2"); + departureList.addAll(list); + } + Collections.sort( departureList ); + + } + + } finally { + if (conn != null && !conn.isClosed() ) { + conn.close(); + } + } + + return departureList; } - public List lookupDepartures() throws Exception { + public List lookupDepartures(String stationcode, String type) throws Exception { List departureList = new ArrayList(); final WebClient webClient = new WebClient(); webClient.setTimeout(1000); - webClient.setProxyConfig(proxyConfig); webClient.setJavaScriptEnabled(false); - 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); HtmlElement table = page.getElementById("afgangtabel"); DomNodeList tableRows = table.getElementsByTagName("tr"); @@ -92,7 +126,7 @@ //test public static void main(String args[]) throws Exception{ DepartureFetcher f = new DepartureFetcher(); - List deps = f.lookupDepartures(); + List deps = f.lookupDepartures("AR", "FJRN"); for(DepartureBean d : deps) { System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() );