From 1b8c97aa2da9322876d2c475db9bfd4b1401bc3b Mon Sep 17 00:00:00 2001 From: "andreas.schildbach" Date: Sun, 3 Oct 2010 08:52:04 +0000 Subject: [PATCH] departures for Saarland git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@230 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- .../schildbach/pte/AbstractHafasProvider.java | 31 ++- src/de/schildbach/pte/NasaProvider.java | 7 +- src/de/schildbach/pte/OebbProvider.java | 14 - src/de/schildbach/pte/VgsProvider.java | 258 ++++++++++++++++++ .../pte/live/VgsProviderLiveTest.java | 49 ++++ 5 files changed, 337 insertions(+), 22 deletions(-) create mode 100644 src/de/schildbach/pte/VgsProvider.java create mode 100644 test/de/schildbach/pte/live/VgsProviderLiveTest.java diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java index b62a2254..5028feda 100644 --- a/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -59,16 +59,23 @@ public abstract class AbstractHafasProvider implements NetworkProvider else oldZebra = zebra; - final Matcher mFineCoords = P_NEARBY_FINE_COORDS.matcher(mCoarse.group(2)); final Matcher mFineLocation = P_NEARBY_FINE_LOCATION.matcher(mCoarse.group(2)); - if (mFineCoords.find() && mFineLocation.find()) + if (mFineLocation.find()) { - final int parsedLon = Integer.parseInt(mFineCoords.group(1)); - final int parsedLat = Integer.parseInt(mFineCoords.group(2)); + int parsedLon = 0; + int parsedLat = 0; final int parsedId = Integer.parseInt(mFineLocation.group(1)); final String parsedName = ParserUtils.resolveEntities(mFineLocation.group(2)); + final Matcher mFineCoords = P_NEARBY_FINE_COORDS.matcher(mCoarse.group(2)); + + if (mFineCoords.find()) + { + parsedLon = Integer.parseInt(mFineCoords.group(1)); + parsedLat = Integer.parseInt(mFineCoords.group(2)); + } + stations.add(new Station(parsedId, parsedName, parsedLat, parsedLon, 0, null, null)); } else @@ -130,6 +137,16 @@ public abstract class AbstractHafasProvider implements NetworkProvider return 'I'; if (ucType.equals("ECB")) // EC, Verona-München return 'I'; + if (ucType.equals("INZ")) // Nacht + return 'I'; + if (ucType.equals("RHI")) // ICE + return 'I'; + if (ucType.equals("RHT")) // TGV + return 'I'; + if (ucType.equals("TGD")) // TGV + return 'I'; + if (ucType.equals("IRX")) // IC + return 'I'; // Regional Germany if (ucType.equals("ZUG")) // Generic Train @@ -176,6 +193,12 @@ public abstract class AbstractHafasProvider implements NetworkProvider // Bus if (ucType.equals("BUS")) // Generic Bus return 'B'; + if (ucType.equals("AST")) // Anruf-Sammel-Taxi + return 'B'; + if (ucType.equals("SEV")) // Schienen-Ersatz-Verkehr + return 'B'; + if (ucType.equals("FB")) // Luxemburg-Saarbrücken + return 'B'; // Ferry if (ucType.equals("AS")) // SyltShuttle, eigentlich Autoreisezug diff --git a/src/de/schildbach/pte/NasaProvider.java b/src/de/schildbach/pte/NasaProvider.java index 1b9e91c1..732ffc64 100644 --- a/src/de/schildbach/pte/NasaProvider.java +++ b/src/de/schildbach/pte/NasaProvider.java @@ -229,15 +229,14 @@ public class NasaProvider extends AbstractHafasProvider if (ucType.equals("RR")) // Polen return 'R'; + if (ucType.equals("E")) // Stadtbahn Karlsruhe: S4/S31/xxxxx + return 'S'; + if (ucType.equals("BSV")) return 'B'; if (ucType.equals("RBS")) // Rufbus return 'B'; - if (ucType.equals("FB")) // Bus - return 'B'; - if (ucType.equals("E")) // manchmal S4, manchmal 5stellige Zugnr. - return '?'; if (ucType.equals("EB")) // Europa-Park, vermutlich "Erlebnisbahn" return '?'; diff --git a/src/de/schildbach/pte/OebbProvider.java b/src/de/schildbach/pte/OebbProvider.java index f6b43b4b..b633d95f 100644 --- a/src/de/schildbach/pte/OebbProvider.java +++ b/src/de/schildbach/pte/OebbProvider.java @@ -605,8 +605,6 @@ public class OebbProvider extends AbstractHafasProvider return 'I'; if (ucType.equals("AVE")) // Alta Velocidad Española, Spanien return 'I'; - if (ucType.equals("INZ")) // Schweden, Nacht, Connections only? - return 'I'; if (ucType.equals("NZ")) // Schweden, Nacht, via JSON API return 'I'; if (ucType.equals("OZ")) // Schweden, Oeresundzug, Connections only? @@ -625,14 +623,6 @@ public class OebbProvider extends AbstractHafasProvider return 'I'; if (ucType.equals("UUU")) // Italien, Nacht, Connections only? return 'I'; - if (ucType.equals("RHI")) // ICE, Connections only? - return 'I'; - if (ucType.equals("RHT")) // TGV, Connections only? - return 'I'; - if (ucType.equals("TGD")) // TGV, Connections only? - return 'I'; - if (ucType.equals("IRX")) // IC, Connections only? - return 'I'; if (ucType.equals("EZ")) // Erlebniszug return 'R'; @@ -789,14 +779,10 @@ public class OebbProvider extends AbstractHafasProvider return 'B'; if (ucType.equals("OBU")) // Connections only? return 'B'; - if (ucType.equals("AST")) - return 'B'; if (ucType.equals("ASTSV")) // via JSON API return 'B'; if (ucType.equals("ICB")) // ÖBB ICBus return 'B'; - if (ucType.equals("FB")) // Polen, Connections only? - return 'B'; if (ucType.equals("BSV")) // Deutschland, Connections only? return 'B'; if (ucType.equals("LT")) // Linien-Taxi, Connections only? diff --git a/src/de/schildbach/pte/VgsProvider.java b/src/de/schildbach/pte/VgsProvider.java new file mode 100644 index 00000000..d81e325a --- /dev/null +++ b/src/de/schildbach/pte/VgsProvider.java @@ -0,0 +1,258 @@ +/* + * Copyright 2010 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package de.schildbach.pte; + +import java.io.IOException; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import de.schildbach.pte.QueryDeparturesResult.Status; + +/** + * @author Andreas Schildbach + */ +public class VgsProvider extends AbstractHafasProvider +{ + public static final String NETWORK_ID = "www.vgs-online.de"; + private static final String API_BASE = "http://www.vgs-online.de/cgi-bin/"; + + private static final long PARSER_DAY_ROLLOVER_THRESHOLD_MS = 12 * 60 * 60 * 1000; + + public boolean hasCapabilities(final Capability... capabilities) + { + for (final Capability capability : capabilities) + if (capability == Capability.DEPARTURES) + return true; + + return false; + } + + public List autocompleteStations(CharSequence constraint) throws IOException + { + throw new UnsupportedOperationException(); + } + + private final String NEARBY_URI = API_BASE + + "stboard.exe/dn?input=%s&selectDate=today&boardType=dep&productsFilter=11111111&distance=50&near=Anzeigen"; + + @Override + protected String nearbyStationUri(final String stationId) + { + return String.format(NEARBY_URI, stationId); + } + + public StationLocationResult stationLocation(String stationId) throws IOException + { + throw new UnsupportedOperationException(); + } + + public String departuresQueryUri(final String stationId, final int maxDepartures) + { + final DateFormat DATE_FORMAT = new SimpleDateFormat("dd.MM.yy"); + final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm"); + final Date now = new Date(); + + final StringBuilder uri = new StringBuilder(); + uri.append(API_BASE).append("stboard.exe/dn"); + uri.append("?input=").append(stationId); + uri.append("&boardType=dep"); + uri.append("&time=").append(TIME_FORMAT.format(now)); + uri.append("&date=").append(DATE_FORMAT.format(now)); + uri.append("&productsFilter=11111111111"); + if (maxDepartures != 0) + uri.append("&maxJourneys=").append(maxDepartures); + uri.append("&disableEquivs=yes"); // don't use nearby stations + uri.append("&start=yes"); + + return uri.toString(); + } + + private static final Pattern P_DEPARTURES_HEAD_COARSE = Pattern + .compile( + ".*?" // + + "(?:" // + + "]*>(.+?)
.*?" // + + "(?:]*>(.+?)
|(verkehren an dieser Haltestelle keine))"// + + "|(Eingabe kann nicht interpretiert)|(Verbindung zum Server konnte leider nicht hergestellt werden|kann vom Server derzeit leider nicht bearbeitet werden))" // + + ".*?" // + , Pattern.DOTALL); + private static final Pattern P_DEPARTURES_HEAD_FINE = Pattern.compile(".*?" // + + "\\s*(.*?)\\s*<.*?" // location + + "(\\d{2}\\.\\d{2}\\.\\d{2}).*?" // date + + "Abfahrt (\\d{1,2}:\\d{2}).*?" // time + , Pattern.DOTALL); + private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("(.*?)", Pattern.DOTALL); + private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?" // + + "(\\d{1,2}:\\d{2})\n" // plannedTime + + "(?:\n" // + + "(?: |(pünktlich|\\d{1,2}:\\d{2}))\n\n" // predictedTime + + ")?.*?" // + + "]*>\\s*(.*?)\\s*\n" // + + "]*>" // destinationId + + "\\s*(.*?)\\s*\n" // destination + + ".*?" // + + "(?:\n(" + ParserUtils.P_PLATFORM + ").*?)?" // position + , Pattern.DOTALL); + + public QueryDeparturesResult queryDepartures(final String uri) throws IOException + { + // scrape page + final CharSequence page = ParserUtils.scrape(uri); + + // parse page + final Matcher mHeadCoarse = P_DEPARTURES_HEAD_COARSE.matcher(page); + if (mHeadCoarse.matches()) + { + // messages + if (mHeadCoarse.group(3) != null) + return new QueryDeparturesResult(uri, Status.NO_INFO); + else if (mHeadCoarse.group(4) != null) + return new QueryDeparturesResult(uri, Status.INVALID_STATION); + else if (mHeadCoarse.group(5) != null) + return new QueryDeparturesResult(uri, Status.SERVICE_DOWN); + + final Matcher mHeadFine = P_DEPARTURES_HEAD_FINE.matcher(mHeadCoarse.group(1)); + if (mHeadFine.matches()) + { + final String location = ParserUtils.resolveEntities(mHeadFine.group(1)); + final Date currentTime = ParserUtils.joinDateTime(ParserUtils.parseDate(mHeadFine.group(2)), ParserUtils + .parseTime(mHeadFine.group(3))); + final List departures = new ArrayList(8); + String oldZebra = null; + + final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mHeadCoarse.group(2)); + while (mDepCoarse.find()) + { + final String zebra = mDepCoarse.group(1); + if (oldZebra != null && zebra.equals(oldZebra)) + throw new IllegalArgumentException("missed row? last:" + zebra); + else + oldZebra = zebra; + + final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(2)); + if (mDepFine.matches()) + { + final Calendar current = new GregorianCalendar(); + current.setTime(currentTime); + final Calendar parsed = new GregorianCalendar(); + parsed.setTime(ParserUtils.parseTime(mDepFine.group(1))); + parsed.set(Calendar.YEAR, current.get(Calendar.YEAR)); + parsed.set(Calendar.MONTH, current.get(Calendar.MONTH)); + parsed.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH)); + if (ParserUtils.timeDiff(parsed.getTime(), currentTime) < -PARSER_DAY_ROLLOVER_THRESHOLD_MS) + parsed.add(Calendar.DAY_OF_MONTH, 1); + + final Date plannedTime = parsed.getTime(); + + Date predictedTime = null; + final String prognosis = ParserUtils.resolveEntities(mDepFine.group(2)); + if (prognosis != null) + { + if (prognosis.equals("pünktlich")) + predictedTime = plannedTime; + else + predictedTime = ParserUtils.joinDateTime(currentTime, ParserUtils.parseTime(prognosis)); + } + + final String lineType = mDepFine.group(3); + + final String line = normalizeLine(lineType, ParserUtils.resolveEntities(mDepFine.group(4))); + + final int destinationId = mDepFine.group(5) != null ? Integer.parseInt(mDepFine.group(5)) : 0; + + final String destination = ParserUtils.resolveEntities(mDepFine.group(6)); + + final String position = mDepFine.group(7) != null ? "Gl. " + ParserUtils.resolveEntities(mDepFine.group(7)) : null; + + final Departure dep = new Departure(plannedTime, predictedTime, line, line != null ? lineColors(line) : null, null, position, + destinationId, destination, null); + + if (!departures.contains(dep)) + departures.add(dep); + } + else + { + throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(2) + "' on " + uri); + } + } + + return new QueryDeparturesResult(uri, 0, location, departures); + } + else + { + throw new IllegalArgumentException("cannot parse '" + mHeadCoarse.group(1) + "' on " + uri); + } + } + else + { + throw new IllegalArgumentException("cannot parse '" + page + "' on " + uri); + } + } + + @Override + protected char normalizeType(String type) + { + final String ucType = type.toUpperCase(); + + final char t = normalizeCommonTypes(ucType); + if (t != 0) + return t; + + if (ucType.equals("INT")) // Zürich-Brüssel + return 'I'; + + if (ucType.equals("SBS")) + return 'S'; + if (ucType.equals("E")) // Stadtbahn Karlsruhe: S4/S31/xxxxx + return 'S'; + + if (ucType.equals("BSS")) + return 'B'; + if (ucType.equals("BOV")) + return 'B'; + + if (ucType.equals("T84")) // U.K. + return '?'; + + return 0; + } + + public QueryConnectionsResult queryConnections(LocationType fromType, String from, LocationType viaType, String via, LocationType toType, + String to, Date date, boolean dep, WalkSpeed walkSpeed) throws IOException + { + throw new UnsupportedOperationException(); + } + + public QueryConnectionsResult queryMoreConnections(String uri) throws IOException + { + throw new UnsupportedOperationException(); + } + + public GetConnectionDetailsResult getConnectionDetails(String connectionUri) throws IOException + { + throw new UnsupportedOperationException(); + } +} diff --git a/test/de/schildbach/pte/live/VgsProviderLiveTest.java b/test/de/schildbach/pte/live/VgsProviderLiveTest.java new file mode 100644 index 00000000..e1500646 --- /dev/null +++ b/test/de/schildbach/pte/live/VgsProviderLiveTest.java @@ -0,0 +1,49 @@ +/* + * Copyright 2010 the original author or authors. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.schildbach.pte.live; + +import java.util.List; + +import org.junit.Test; + +import de.schildbach.pte.QueryDeparturesResult; +import de.schildbach.pte.Station; +import de.schildbach.pte.VgsProvider; + +/** + * @author Andreas Schildbach + */ +public class VgsProviderLiveTest +{ + private final VgsProvider provider = new VgsProvider(); + + @Test + public void nearbyStation() throws Exception + { + final List results = provider.nearbyStations("8000244", 0, 0, 0, 0); + + System.out.println(results.size() + " " + results); + } + + @Test + public void queryDepartures() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures(provider.departuresQueryUri("8000244", 0)); + + System.out.println(result.departures.size() + " " + result.departures); + } +}