--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2011/04/04 10:56:44 1255 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/DepartureFetcher.java 2011/04/20 20:04:05 1366 @@ -16,7 +16,10 @@ import dk.thoerup.android.traininfo.common.StationBean.StationEntry; import dk.thoerup.circuitbreaker.CircuitBreaker; import dk.thoerup.circuitbreaker.CircuitBreakerManager; +import dk.thoerup.genericjavautils.HttpUtil; +import dk.thoerup.genericjavautils.TimeoutMap; import dk.thoerup.traininfoservice.Statistics; +import dk.thoerup.traininfoservice.TraininfoSettings; import dk.thoerup.traininfoservice.db.StationDAO; public class DepartureFetcher { @@ -38,13 +41,12 @@ StationDAO stationDao = new StationDAO(); - private boolean useAzureSite; - private int replyTimeout; + + private TraininfoSettings settings; - public DepartureFetcher(boolean azureSite, int cacheTimeout, int replyTimeout) { - this.replyTimeout = replyTimeout; - useAzureSite = azureSite; - cache = new TimeoutMap(cacheTimeout); + public DepartureFetcher(TraininfoSettings settings) { + this.settings = settings; + cache = new TimeoutMap( settings.getCacheTimeout() ); } @@ -101,10 +103,10 @@ } public DepartureBean lookupDepartures(String stationcode, TrainType type, boolean arrival) throws Exception { - if (useAzureSite == true) { + if ( settings.getUseAzureSite() == true) { return lookupDeparturesAzureSite(stationcode, type, arrival); } else { - return lookupDeparturesWwwSite(stationcode, type, arrival); + return lookupDeparturesMobileSite(stationcode, type, arrival); } } @@ -143,7 +145,7 @@ String uri = "http://trafikinfo.bane.dk/Trafikinformation/AfgangAnkomst/" + arrivalDeparture + "/" + stationcode + "/" + typeString + "/UdvidetVisning"; logger.fine("URI: " + uri); - JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); + JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); Document page = (Document) breaker.invoke(wrapper); @@ -229,6 +231,112 @@ return departureBean; } + public DepartureBean lookupDeparturesMobileSite(String stationcode, TrainType traintype, boolean arrival) throws Exception { + + DepartureBean departureBean = new DepartureBean(); + + + String typeString = getTypeStringWww(traintype); + 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"; + String uri = "http://mobil.bane.dk/mobilStation.asp?artikelID=5332&stat_kode=" + stationcode + "&webprofil=" + typeString +"&beskrivelse=&mode=ankomstafgang&ankomstafgang=" + arrivalDeparture + "&gemstation=&fuldvisning=1"; + logger.fine("URI: " + uri); + JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + + Document page = (Document) breaker.invoke(wrapper); + + + Element content = page.getElementsByClass("contentDiv").get(0); + + + if (content != null) { + Elements tableRows = content.child(0).children(); + + + + for (Element currentRow : tableRows) { + if (currentRow.tagName().equals("br") ) { + break; + } + + + String link = currentRow.child(0).attr("href"); + + logger.fine( currentRow.text() ); + logger.fine("Href: " + link); + + + String parts[] = currentRow.text().split(","); + + + DepartureEntry departure = new DepartureEntry(); + + //if we do these things upfront, then we are allowed to use continue statement when row contains no more data + departure.setType(typeString); + departureBean.entries.add( departure ); + +/* +http://mobil.bane.dk/mobilStation.asp?artikelID=5332&tognummer=111&webprofil=FJRN&mode=rute&strBemaerkning=Afg%E5r+fra+%C5rhus+H+kl%2E07%3A21++&strRefURL=%2FmobilStation%2Easp%3FartikelID%3D5332%26stat%5Fkode%3DAR%26webprofil%3DFJRN%26beskrivelse%3D%25C5rhus%2BH%26mode%3Dankomstafgang%26ankomstafgang%3Dafgang%26gemstation%3D +*/ + int offset = 0; + + String time = parts[offset++]; + if (time.equals("")) + time = "0:00"; //Bane.dk bug work-around + departure.setTime(time); + + int updated = 4; //does not exist on mobile + departure.setUpdated(updated); + + String trainNumber = extractTrainNumberMobile(link); + /*if (traintype == TrainType.STOG) //If it is S-train we need to extract the trainNumber + trainNumber = trainNumber + " " + extractTrainNumberAzure(fields.get(2));*/ + departure.setTrainNumber(trainNumber); + + if (traintype == TrainType.STOG) { //if it is stog the next vield is the "Line" code - this should be used somewhere, but skippint ahead for now + String stogLine = parts[offset++].trim(); + departure.setTrainNumber(stogLine + " " + trainNumber); + } + + String destination = parts[offset++].trim();; + departure.setDestination(destination); + + String origin = "-"; // fields.get(4).text(); does not exist on mobile + departure.setOrigin(origin); + + String location = ""; // fields.get(5).text(); does not exist on mobile + departure.setLocation(location); + + if (offset == parts.length) { + continue; + } + + if (parts[offset].trim().equalsIgnoreCase("NB!")) { + offset++; + } + + if (offset == parts.length) { + continue; + } + + String status = parts[offset++].trim();; //fields.get(6).text().trim(); - extract from url + departure.setStatus(status); + + String note = ""; //extractNote( fields.get(7) ); - extract from url + departure.setNote(note); + + } + } else { + logger.warning("No departures found for station=" + stationcode + ", type=" + traintype); + } + + return departureBean; + } + public static String cleanText(String input) { @@ -236,6 +344,9 @@ return input.replace((char) 0xA0, (char)0x20).trim(); } + + // old www site is not available any more + @Deprecated public DepartureBean lookupDeparturesWwwSite(String stationcode, TrainType trainType, boolean arrival) throws Exception { DepartureBean departureBean = new DepartureBean(); @@ -249,7 +360,7 @@ logger.fine("URI:" + uri); - JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), replyTimeout); + JsoupInvocation wrapper = new JsoupInvocation( new URL(uri), settings.getReplyTimeout() ); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); Element page = (Element) breaker.invoke(wrapper); @@ -367,20 +478,31 @@ return number; } + private String extractTrainNumberMobile(String link) { + Map elements = HttpUtil.decodeParams(link); + + return elements.get("tognummer"); + } + 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 argstring = href.split("?")[1]; + Map elements = HttpUtil.decodeParams(argstring); + number = elements.get("TogNr"); + + /*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; }