package dk.thoerup.traininfoservice.banedk; import java.util.ArrayList; 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; public class DepartureFetcher { static ProxyConfig proxyConfig; static { proxyConfig = new ProxyConfig(); proxyConfig.setProxyHost("rafiki.t-hoerup.dk"); proxyConfig.setProxyPort(3128); } public List lookupDepartures() 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"); HtmlElement table = page.getElementById("afgangtabel"); DomNodeList tableRows = table.getElementsByTagName("tr"); for (HtmlElement currentRow : tableRows) { String rowClass = currentRow.getAttribute("class"); if (rowClass != null && rowClass.toLowerCase().contains("station") ) { DomNodeList fields = currentRow.getElementsByTagName("td"); DepartureBean departure = new DepartureBean(); String time = fields.get(0).asText(); departure.setTime(time); int updated = extractUpdated( fields.get(1) ); departure.setUpdated(updated); String trainNumber = fields.get(2).asText(); departure.setTrainNumber(trainNumber); String destination = fields.get(3).asText(); departure.setDestination(destination); String origin = fields.get(4).asText(); departure.setOrigin(origin); String location = fields.get(5).asText(); departure.setLocation(location); String status = fields.get(6).asText(); departure.setStatus(status); String note = fields.get(7).asText(); departure.setNote(note); departureList.add(departure); } } return departureList; } private int extractUpdated(HtmlElement updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" int updated = -1; DomNodeList updatedImgs = updatedTd.getElementsByTagName("img"); String updatedStr = updatedImgs.get(0).getAttribute("src"); if (updatedStr != null) { for (int i=0; i deps = f.lookupDepartures(); for(DepartureBean d : deps) { System.out.println( d.getTime() + ";" + d.getUpdated() + ";" + d.getTrainNumber() + ";" + d.getDestination() + ";" + d.getOrigin() + ";" + d.getLocation() + ";" + d.getStatus() + ";" + d.getNote() ); } System.out.println("--------------------------"); } }