--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/06/11 20:50:40 842 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2010/09/14 06:10:30 1048 @@ -1,7 +1,7 @@ package dk.thoerup.traininfoservice.banedk; -import java.io.IOException; + import java.net.URL; import java.sql.SQLException; import java.util.ArrayList; @@ -10,13 +10,9 @@ import java.util.logging.Level; import java.util.logging.Logger; -import com.gargoylesoftware.htmlunit.BrowserVersion; -import com.gargoylesoftware.htmlunit.Page; -import com.gargoylesoftware.htmlunit.RefreshHandler; -import com.gargoylesoftware.htmlunit.WebClient; -import com.gargoylesoftware.htmlunit.html.DomNodeList; -import com.gargoylesoftware.htmlunit.html.HtmlElement; -import com.gargoylesoftware.htmlunit.html.HtmlPage; +import org.jsoup.nodes.Document; +import org.jsoup.nodes.Element; +import org.jsoup.select.Elements; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; @@ -24,12 +20,7 @@ import dk.thoerup.traininfoservice.Statistics; public class TimetableFetcher { - - class NullRefreshHandler implements RefreshHandler { - public void handleRefresh(Page arg0, URL arg1, int arg2) throws IOException { - } - - } + Map> cache; Map stationCache; @@ -39,10 +30,12 @@ Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); - private boolean useTempSite; + private boolean useAzureSite; + private int replyTimeout; - public TimetableFetcher(boolean tmpSite, int cacheTimeout) { - useTempSite = tmpSite; + public TimetableFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { + useAzureSite = azureSite; + this.replyTimeout = replyTimeout; cache = new TimeoutMap>(cacheTimeout); stationCache = new TimeoutMap( 3*60*60*1000 ); @@ -64,10 +57,11 @@ } List lookupTimetable(String trainID, String type) throws Exception { - if (useTempSite == false ){ - return lookupTimetableRealSite(trainID, type); + if (useAzureSite == true ){ + return lookupTimetableAzureSite(trainID, type); + } else { - return new ArrayList(); // no timetable data on temp site + return lookupTimetableWwwSite(trainID, type); } } @@ -87,55 +81,54 @@ return id; } - List lookupTimetableRealSite(String trainID, String type) throws Exception { + List lookupTimetableAzureSite(String trainID, String type) throws Exception { List timetableList = new ArrayList(); - String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; - - final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3); - webClient.setTimeout(2500); - webClient.setJavaScriptEnabled(false); - webClient.setRefreshHandler( new NullRefreshHandler() ); - webClient.setCssEnabled(false); + String url = "http://trafikinfo.bane.dk/TrafikInformation/Ruteplan/" + trainID; + logger.fine("URL:" + url); - - BanedkInvocation wrapper = new BanedkInvocation(webClient, url); + JsoupInvocation wrapper = new JsoupInvocation( new URL(url) , replyTimeout); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); - HtmlPage page = (HtmlPage) breaker.invoke(wrapper); + Document doc = (Document) breaker.invoke(wrapper); boolean currentStation = false; boolean currentStationSaved = false; - List tables = page.getDocumentElement().getElementsByAttribute("table", "class", "Rute"); + Elements tables = doc.getElementsByClass("Rute"); + if (tables.size() == 1) { - HtmlElement timetable = tables.get(0); - DomNodeList rows = timetable.getElementsByTagName("tr"); + Element timetable = tables.get(0); + Elements rows = timetable.getElementsByTag("tr"); for (int i=0; i fields = row.getElementsByTagName("td"); + Element row = rows.get(i); + Elements fields = row.getElementsByTag("td"); + - if (currentStationSaved == false && fields.get(0).getAttribute("class").equalsIgnoreCase("Tidsstreg")) { + if (currentStationSaved == false && fields.get(0).attr("class").equalsIgnoreCase("Tidsstreg")) { currentStation = true; continue; } TimetableBean bean = new TimetableBean(); - String station = fields.get(0).asText() ; + String station = fields.get(0).text() ; if (station.equals("København")) station = "København H"; //correct inconsistency in naming bean.setStation( station ); - bean.setArrival( fields.get(1).asText() ); - bean.setDeparture( fields.get(2).asText() ); + bean.setArrival( fields.get(1).text() ); + bean.setDeparture( fields.get(2).text() ); + + boolean cancelled = fields.get(3).text().equalsIgnoreCase("aflyst"); + bean.setCancelled(cancelled); if (currentStation == true && currentStationSaved == false ) { bean.setCurrent(currentStation); @@ -145,15 +138,105 @@ bean.setStationId( getStationId( station )); timetableList.add(bean); - + } + + //TODO: There is an off-by-one error in this cancelled parser thingie + final String cancelledString = "Aflyst"; + for (int i=0;i0 && i