From 8e2da67170dca2987b8c2eb6b3c2f611bdf6d952 Mon Sep 17 00:00:00 2001 From: "andreas.schildbach@gmail.com" Date: Wed, 8 Dec 2010 23:42:12 +0000 Subject: [PATCH] Netherlands git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@386 0924bc21-9374-b0fa-ee44-9ff1593b38f0 --- .../schildbach/pte/AbstractHafasProvider.java | 6 +- src/de/schildbach/pte/NsProvider.java | 226 ++++++++++++++++++ .../pte/live/NsProviderLiveTest.java | 88 +++++++ 3 files changed, 317 insertions(+), 3 deletions(-) create mode 100644 src/de/schildbach/pte/NsProvider.java create mode 100644 test/de/schildbach/pte/live/NsProviderLiveTest.java diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java index dda8fb90..9ecf5fb2 100644 --- a/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -437,9 +437,7 @@ public abstract class AbstractHafasProvider implements NetworkProvider } else { - parts - .add(new Connection.Footway(min, sectionDeparture.id, sectionDeparture.name, sectionArrival.id, - sectionArrival.name)); + parts.add(new Connection.Footway(min, sectionDeparture.id, sectionDeparture.name, sectionArrival.id, sectionArrival.name)); } } @@ -651,6 +649,8 @@ public abstract class AbstractHafasProvider implements NetworkProvider return "S" + normalizedType; if (P_LINE_SN.matcher(normalizedType).matches()) // Nacht-S-Bahn return "S" + normalizedType; + if ("SPR".equals(normalizedType)) // Sprinter, Niederlande + return "S" + normalizedName; if ("Met".equals(normalizedType)) // Metro return "U" + normalizedName; diff --git a/src/de/schildbach/pte/NsProvider.java b/src/de/schildbach/pte/NsProvider.java new file mode 100644 index 00000000..6abd0b99 --- /dev/null +++ b/src/de/schildbach/pte/NsProvider.java @@ -0,0 +1,226 @@ +/* + * 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.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.dto.Departure; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; +import de.schildbach.pte.dto.QueryDeparturesResult; +import de.schildbach.pte.dto.QueryDeparturesResult.Status; +import de.schildbach.pte.util.ParserUtils; + +/** + * @author Andreas Schildbach + */ +public class NsProvider extends AbstractHafasProvider +{ + public static final String NETWORK_ID = "hafas.bene-system.com"; + + private static final long PARSER_DAY_ROLLOVER_THRESHOLD_MS = 12 * 60 * 60 * 1000; + + private static final String API_URI = "http://hafas.bene-system.com/bin/extxml.exe"; + + public NsProvider() + { + super(API_URI, null); + } + + public boolean hasCapabilities(final Capability... capabilities) + { + for (final Capability capability : capabilities) + if (capability == Capability.DEPARTURES || capability == Capability.CONNECTIONS) + return true; + + return false; + } + + private final String NEARBY_URI = "http://hari.b-rail.be/HAFAS/bin/stboard.exe/en?input=%s&distance=50&near=Anzeigen"; + + @Override + protected String nearbyStationUri(final String stationId) + { + return String.format(NEARBY_URI, stationId); + } + + private String departuresQueryUri(final String stationId, final int maxDepartures) + { + final StringBuilder uri = new StringBuilder(); + uri.append("http://hari.b-rail.be/hari3/webserver1/bin/stboard.exe/dox"); + uri.append("?input=").append(stationId); + uri.append("&boardType=dep"); + uri.append("&maxJourneys=").append(maxDepartures != 0 ? maxDepartures : 50); // maximum taken from SNCB site + uri.append("&productsFilter=1:1111111111111111"); + 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(".*?" // + + "(?:" // + + "
\r\n" // + + "

\r\n(.*?)\r\n

\r\n" // head + + "(.*?)\r\n
" // departures + + "|(Eingabe kann nicht interpretiert)|(Verbindung zum Server konnte leider nicht hergestellt werden))" // + + ".*?", Pattern.DOTALL); + private static final Pattern P_DEPARTURES_HEAD_FINE = Pattern.compile("" // + + "(.*?)
\r\n" // location + + "Abfahrt (\\d{1,2}:\\d{2}),\r\n" // time + + "(\\d{2}/\\d{2}/\\d{2})" // date + , Pattern.DOTALL); + private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("

\r\n(.*?)

", Pattern.DOTALL); + private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?" // + + "(.*?).*?" // line + + ">>\r\n" // + + "(.*?)\r\n" // destination + + "
\r\n" // + + "(\\d{1,2}:\\d{2})\r\n" // time + + "(?:([+-]?\\d+|Ausfall)\r\n)?" // delay + , Pattern.DOTALL); + + public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures) throws IOException + { + final CharSequence page = ParserUtils.scrape(departuresQueryUri(stationId, maxDepartures)); + + // parse page + final Matcher mHeadCoarse = P_DEPARTURES_HEAD_COARSE.matcher(page); + if (mHeadCoarse.matches()) + { + // messages + if (mHeadCoarse.group(3) != null) + return new QueryDeparturesResult(Status.INVALID_STATION, Integer.parseInt(stationId)); + else if (mHeadCoarse.group(4) != null) + return new QueryDeparturesResult(Status.SERVICE_DOWN, Integer.parseInt(stationId)); + + 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.parseDateSlash(mHeadFine.group(3)), ParserUtils.parseTime(mHeadFine + .group(2))); + final List departures = new ArrayList(8); + + final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mHeadCoarse.group(2)); + while (mDepCoarse.find()) + { + final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(1)); + if (mDepFine.matches()) + { + final String line = normalizeLine(ParserUtils.resolveEntities(mDepFine.group(1))); + + final String destination = ParserUtils.resolveEntities(mDepFine.group(2)); + + final Calendar current = new GregorianCalendar(); + current.setTime(currentTime); + final Calendar parsed = new GregorianCalendar(); + parsed.setTime(ParserUtils.parseTime(mDepFine.group(3))); + 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); + + mDepFine.group(4); // TODO delay + + final Departure dep = new Departure(parsed.getTime(), line, line != null ? lineColors(line) : null, null, 0, destination); + + if (!departures.contains(dep)) + departures.add(dep); + } + else + { + throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(1) + "' on " + stationId); + } + } + + return new QueryDeparturesResult(new Location(LocationType.STATION, Integer.parseInt(stationId), 0, 0, location), departures, null); + } + else + { + throw new IllegalArgumentException("cannot parse '" + mHeadCoarse.group(1) + "' on " + stationId); + } + } + else + { + throw new IllegalArgumentException("cannot parse '" + page + "' on " + stationId); + } + } + + private String normalizeLine(final String line) + { + if (line == null || line.length() == 0) + return null; + + final Matcher m = P_NORMALIZE_LINE.matcher(line); + if (m.matches()) + { + final String type = m.group(1); + final String number = m.group(2); + + final char normalizedType = normalizeType(type); + if (normalizedType != 0) + return normalizedType + type + number; + + throw new IllegalStateException("cannot normalize type " + type + " number " + number + " line " + line); + } + + throw new IllegalStateException("cannot normalize line " + line); + } + + @Override + protected char normalizeType(final String type) + { + final String ucType = type.toUpperCase(); + + final char t = normalizeCommonTypes(ucType); + if (t != 0) + return t; + + if (ucType.equals("EST")) // Eurostar Frankreich + return 'I'; + if (ucType.equals("INT")) // Zürich-Brüssel + return 'I'; + + if (ucType.equals("L")) + return 'R'; + if (ucType.equals("P")) + return 'R'; + if (ucType.equals("CR")) + return 'R'; + if (ucType.equals("ICT")) // Brügge + return 'R'; + if (ucType.equals("TRN")) // Mons + return 'R'; + + if (ucType.equals("MÉT")) + return 'U'; + + if (ucType.equals("TRA")) + return 'T'; + + return 0; + } +} diff --git a/test/de/schildbach/pte/live/NsProviderLiveTest.java b/test/de/schildbach/pte/live/NsProviderLiveTest.java new file mode 100644 index 00000000..b6fa47c8 --- /dev/null +++ b/test/de/schildbach/pte/live/NsProviderLiveTest.java @@ -0,0 +1,88 @@ +/* + * 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.Date; +import java.util.List; + +import org.junit.Test; + +import de.schildbach.pte.NsProvider; +import de.schildbach.pte.NetworkProvider.WalkSpeed; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; +import de.schildbach.pte.dto.NearbyStationsResult; +import de.schildbach.pte.dto.QueryConnectionsResult; +import de.schildbach.pte.dto.QueryDeparturesResult; + +/** + * @author Andreas Schildbach + */ +public class NsProviderLiveTest +{ + private final NsProvider provider = new NsProvider(); + + @Test + public void queryDepartures() throws Exception + { + final QueryDeparturesResult result = provider.queryDepartures("100080", 0); + + System.out.println(result.status + " " + result.departures.size() + " " + result.departures); + } + + @Test + public void autocompleteIncomplete() throws Exception + { + final List autocompletes = provider.autocompleteStations("Brussel S"); + + list(autocompletes); + } + + private void list(final List autocompletes) + { + System.out.print(autocompletes.size() + " "); + for (final Location autocomplete : autocompletes) + System.out.print(autocomplete.toDebugString() + " "); + System.out.println(); + } + + @Test + public void shortConnection() throws Exception + { + final QueryConnectionsResult result = provider.queryConnections(new Location(LocationType.STATION, 100024, 0, 0, null), null, new Location( + LocationType.STATION, 100066, 0, 0, null), new Date(), true, null, WalkSpeed.FAST); + + System.out.println(result.status + " " + result.connections); + } + + @Test + public void longConnection() throws Exception + { + final QueryConnectionsResult result = provider.queryConnections(new Location(LocationType.STATION, 100024, 0, 0, null), null, new Location( + LocationType.STATION, 103624, 0, 0, null), new Date(), true, null, WalkSpeed.FAST); + + System.out.println(result.status + " " + result.connections); + } + + @Test + public void nearbyStation() throws Exception + { + final NearbyStationsResult result = provider.nearbyStations("100080", 0, 0, 0, 0); + + System.out.println(result.stations.size() + " " + result.stations); + } +}