package dk.thoerup.traininfoservice.banedk; import java.net.URL; import java.net.URLEncoder; import java.util.Collections; import java.util.Map; import java.util.logging.Logger; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import dk.thoerup.android.traininfo.common.DepartureBean; import dk.thoerup.android.traininfo.common.DepartureEntry; import dk.thoerup.android.traininfo.common.StationBean.StationEntry; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; import dk.thoerup.traininfoservice.StationDAO; import dk.thoerup.traininfoservice.Statistics; public class DepartureFetcher { enum TrainType{ STOG, REGIONAL } enum FetchTrainType { STOG, REGIONAL, BOTH } Logger logger = Logger.getLogger(DepartureFetcher.class.getName()); Map cache; StationDAO stationDao = new StationDAO(); private boolean useAzureSite; private int replyTimeout; public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { this.replyTimeout = replyTimeout; useAzureSite = azureSite; cache = new TimeoutMap(cacheTimeout); } public DepartureBean cachedLookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { final String key = "" + stationID + ":" + arrival + ":" + type.toString(); DepartureBean departureBean = cache.get(key); if (departureBean == null) { departureBean = lookupDepartures(stationID, arrival, type); cache.put(key, departureBean); } else { Statistics.getInstance().incrementDepartureCacheHits(); logger.info("Departure: Cache hit " + key); //remove before production } return departureBean; } public DepartureBean lookupDepartures(int stationID, boolean arrival, FetchTrainType type) throws Exception { DepartureBean departureBean = new DepartureBean(); StationEntry station = stationDao.getById(stationID); departureBean.stationName = station.getName(); if (station.getRegional() != null && (type == FetchTrainType.REGIONAL||type == FetchTrainType.BOTH) ) { DepartureBean tempBean = lookupDepartures(station.getRegional(), TrainType.REGIONAL, arrival); departureBean.entries.addAll( tempBean.entries ); departureBean.notifications.addAll(tempBean.notifications); } if (station.getStrain() != null && (type == FetchTrainType.STOG||type == FetchTrainType.BOTH)) { DepartureBean tempBean = lookupDepartures(station.getStrain(), TrainType.STOG, arrival); departureBean.entries.addAll( tempBean.entries ); departureBean.notifications.addAll(tempBean.notifications); } if (departureBean.entries.size() == 0) { logger.info("No departures found for station " + stationID); } Collections.sort( departureBean.entries ); return departureBean; } public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { if (useAzureSite == true) { return lookupDeparturesAzureSite(stationcode, type, arrival); } else { return lookupDeparturesWwwSite(stationcode, type, arrival); } } private String getTypeStringAzure(TrainType type) { switch (type) { case STOG: return "S-Tog"; case REGIONAL: return "Fjerntog"; default: return ""; //Can not happen } } private String getTypeStringWww(TrainType type) { switch (type) { case STOG: return "S2"; case REGIONAL: return "FJRN"; default: return ""; //Can not happen } } public DepartureBean lookupDeparturesAzureSite(String stationcode, TrainType type, boolean arrival) throws Exception { DepartureBean departureBean = new DepartureBean(); String typeString = getTypeStringAzure(type); String arrivalDeparture = (arrival==false) ? "Afgang" : "Ankomst"; stationcode = URLEncoder.encode(stationcode,"ISO-8859-1"); String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; logger.fine("URI: " + uri); JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); Document page = (Document) breaker.invoke(wrapper); String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; Element table = page.getElementById(tableName); if (table != null) { Elements tableRows = table.getElementsByTag("tr"); //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); //boolean passedTidsstreg = false; for (Element currentRow : tableRows) { String rowClass = currentRow.attr("class"); /* if (tidsstregExists == true && passedTidsstreg == false) { if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { passedTidsstreg = true; } else { continue; } }*/ if (rowClass != null && rowClass.toLowerCase().contains("station") ) { Elements fields = currentRow.getElementsByTag("td"); DepartureEntry departure = new DepartureEntry(); String time = fields.get(0).text(); if (time.equals("")) time = "0:00"; //Bane.dk bug work-around departure.setTime(time); int updated = extractUpdated( fields.get(1) ); departure.setUpdated(updated); String trainNumber = fields.get(2).text(); if (type == TrainType.STOG) //If it is S-train we need to extract the trainNumber trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2)); departure.setTrainNumber(trainNumber); String destination = fields.get(3).text(); departure.setDestination(destination); String origin = fields.get(4).text(); departure.setOrigin(origin); String location = fields.get(5).text(); departure.setLocation(location); String status = fields.get(6).text().trim(); departure.setStatus(status); String note = extractNote( fields.get(7) ); departure.setNote(note); departure.setType(typeString); departureBean.entries.add( departure ); } } } else { logger.warning("No departures found for station=" + stationcode + ", type=" + type); } Element notifDiv = page.getElementById("station_planlagte_text"); if (notifDiv != null) { Elements tables = notifDiv.getElementsByTag("table"); for (Element tab : tables) { Elements anchors = tab.getElementsByTag("a"); if (anchors.size() == 2) { departureBean.notifications.add( anchors.get(1).text() ); } } } return departureBean; } public static String cleanText(String input) { //apparently JSoup translates   characters on www.bane.dk to 0xA0 return input.replace((char) 0xA0, (char)0x20).trim(); } public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { DepartureBean departureBean = new DepartureBean(); String type = getTypeStringWww(trainType); stationcode = URLEncoder.encode(stationcode, "ISO-8859-1"); String uri = "http://www.bane.dk/visStation.asp?ArtikelID=4275&W=" + type + "&S=" + stationcode; logger.fine("URI:" + uri); JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); Element page = (Element) breaker.invoke(wrapper); String tableName = arrival == false ? "afgangtabel" : "ankomsttabel"; Element table = page.getElementById(tableName); if (table != null) { Elements tableRows = table.getElementsByTag("tr"); //boolean passedTidsstreg = false; //boolean tidsstregExists = (table.getElementsByAttributeValue("class", "Tidsstreg").size() > 0); for (Element currentRow : tableRows) { String rowClass = currentRow.attr("class"); /* if (tidsstregExists == true && passedTidsstreg == false) { if (currentRow.getElementsByAttributeValue("class", "Tidsstreg").size() > 0) { passedTidsstreg = true; } else { continue; } }*/ if (rowClass != null && rowClass.toLowerCase().contains("station") ) { Elements fields = currentRow.getElementsByTag("td"); DepartureEntry departure = new DepartureEntry(); String time = cleanText( fields.get(0).getAllElements().get(2).text() ); if (time.equals("")) time = "0:00"; //Bane.dk bug work-around departure.setTime(time); int updated = extractUpdated( fields.get(1) ); departure.setUpdated(updated); String trainNumber = cleanText( fields.get(2).text() ); if (type.equalsIgnoreCase("S2")) //If it is S-train we need to extract the trainNumber trainNumber = trainNumber + " " + extractTrainNumberWww(fields.get(2)); departure.setTrainNumber(trainNumber); String destination = cleanText( fields.get(3).text() ); departure.setDestination(destination); String origin = cleanText( fields.get(4).text() ); departure.setOrigin(origin); String location = cleanText( fields.get(5).text() ); departure.setLocation(location); String status = cleanText( fields.get(6).text() ); departure.setStatus(status); String note = cleanText( extractNote( fields.get(7) ) ); departure.setNote(note); departure.setType(type); departureBean.entries.add(departure); } } } else { logger.warning("No departures found for station=" + stationcode + ", type=" + type); } return departureBean; } private int extractUpdated(Element updatedTd) { //extract the digit (in this case: 4) from "media/trafikinfo/opdater4.gif" int updated = -1; Elements updatedImgs = updatedTd.getElementsByTag("img"); String updatedStr = updatedImgs.get(0).attr("src"); if (updatedStr != null) { for (int i=0; i 0 && note.charAt(note.length()-1) == 'i') note = note.substring(0,note.length() -1 ); return note.trim(); } private String extractTrainNumberAzure(Element trainTd) { Element anchorElement = trainTd.getElementsByTag("a").get(0); String href = anchorElement.attr("href"); int pos = href.lastIndexOf('/'); String number = href.substring(pos+1); return number; } private String extractTrainNumberWww(Element trainTd) { String number = ""; Element anchorElement = trainTd.getElementsByTag("a").get(0); String href = anchorElement.attr("href"); String argstring = href.substring( href.indexOf('?') + 1); String args[] = argstring.split("&"); for (String arg : args) { String pair[] = arg.split("="); // Key=pair[0], Value=pair[1] if (pair[0].equalsIgnoreCase("TogNr")) number = pair[1]; } return number; } //test /* public static void main(String args[]) throws Exception { DepartureFetcher f = new DepartureFetcher(); 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() ); } System.out.println("--------------------------"); }*/ }