mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-21 01:39:51 +00:00
Kiel
git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@603 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
parent
a8f006821f
commit
40da8789c6
2 changed files with 350 additions and 0 deletions
268
src/de/schildbach/pte/ShProvider.java
Normal file
268
src/de/schildbach/pte/ShProvider.java
Normal file
|
@ -0,0 +1,268 @@
|
|||
/*
|
||||
* Copyright 2010, 2011 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.schildbach.pte;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
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.dto.StationDepartures;
|
||||
import de.schildbach.pte.util.ParserUtils;
|
||||
|
||||
/**
|
||||
* @author Andreas Schildbach
|
||||
*/
|
||||
public class ShProvider extends AbstractHafasProvider
|
||||
{
|
||||
public static final NetworkId NETWORK_ID = NetworkId.SH;
|
||||
private static final String API_BASE = "http://scout.hafas.de/bin/";
|
||||
|
||||
private static final long PARSER_DAY_ROLLOVER_THRESHOLD_MS = 12 * 60 * 60 * 1000;
|
||||
|
||||
public ShProvider()
|
||||
{
|
||||
super(API_BASE + "query.exe/dn", null);
|
||||
}
|
||||
|
||||
public NetworkId id()
|
||||
{
|
||||
return NETWORK_ID;
|
||||
}
|
||||
|
||||
public boolean hasCapabilities(final Capability... capabilities)
|
||||
{
|
||||
for (final Capability capability : capabilities)
|
||||
if (capability == Capability.AUTOCOMPLETE_ONE_LINE || capability == Capability.DEPARTURES || capability == Capability.CONNECTIONS)
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
|
||||
{
|
||||
return xmlMLcReq(constraint);
|
||||
}
|
||||
|
||||
private final String NEARBY_URI = API_BASE
|
||||
+ "stboard.exe/dn?input=%s&selectDate=today&boardType=dep&productsFilter=1111111111111111&distance=50&near=Anzeigen";
|
||||
|
||||
@Override
|
||||
protected String nearbyStationUri(final String stationId)
|
||||
{
|
||||
return String.format(NEARBY_URI, ParserUtils.urlEncode(stationId));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected char normalizeType(final String type)
|
||||
{
|
||||
final String ucType = type.toUpperCase();
|
||||
|
||||
if ("KBS".equals(ucType))
|
||||
return 'B';
|
||||
if ("KB1".equals(ucType))
|
||||
return 'B';
|
||||
|
||||
// from connections:
|
||||
|
||||
if ("NOB".equals(ucType)) // Nord-Ostsee-Bahn
|
||||
return 'R';
|
||||
if ("ME".equals(ucType)) // metronom
|
||||
return 'R';
|
||||
if ("MER".equals(ucType)) // metronom regional
|
||||
return 'R';
|
||||
if ("AKN".equals(ucType)) // AKN Eisenbahn AG
|
||||
return 'R';
|
||||
if ("SHB".equals(ucType)) // Schleswig-Holstein-Bahn
|
||||
return 'R';
|
||||
if ("NEG".equals(ucType)) // Norddeutsche Eisenbahngesellschaft Niebüll
|
||||
return 'R';
|
||||
if ("NBE".equals(ucType)) // Nordbahn Eisenbahngesellschaft
|
||||
return 'R';
|
||||
|
||||
final char t = normalizeCommonTypes(ucType);
|
||||
if (t != 0)
|
||||
return t;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private String departuresQueryUri(final String stationId, final int maxDepartures)
|
||||
{
|
||||
final StringBuilder uri = new StringBuilder();
|
||||
|
||||
uri.append(API_BASE).append("stboard.exe/dn");
|
||||
uri.append("?input=").append(stationId);
|
||||
uri.append("&boardType=dep");
|
||||
uri.append("&productsFilter=11111111");
|
||||
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(
|
||||
".*?" //
|
||||
+ "(?:" //
|
||||
+ "<table class=\"hafasResult\"[^>]*>(.+?)</table>.*?" //
|
||||
+ "(?:<table cellspacing=\"0\" class=\"hafasResult\"[^>]*>(.+?)</table>|(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(".*?" //
|
||||
+ "<td class=\"querysummary screennowrap\">\\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("<tr class=\"(depboard-\\w*)\">(.*?)</tr>", Pattern.DOTALL);
|
||||
private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?" //
|
||||
+ "<td class=\"[\\w ]*\">(\\d{1,2}:\\d{2})</td>\n" // plannedTime
|
||||
+ "(?:<td class=\"[\\w ]*prognosis[\\w ]*\">\n" //
|
||||
+ "(?: |<span class=\"rtLimit\\d\">(pünktlich|\\d{1,2}:\\d{2})</span>)\n</td>\n" // predictedTime
|
||||
+ ")?.*?" //
|
||||
+ "<img src=\"/hafas-res/img/(\\w+)_pic\\.gif\"[^>]*>\\s*(.*?)\\s*</.*?" // type, line
|
||||
+ "<span class=\"bold\">\n" //
|
||||
+ "<a href=\"http://scout\\.hafas\\.de/bin/stboard\\.exe/dn\\?input=(\\d+)&[^>]*>" // destinationId
|
||||
+ "\\s*(.*?)\\s*</a>\n" // destination
|
||||
+ "</span>.*?" //
|
||||
+ "(?:<td class=\"center sepline top\">\n(" + ParserUtils.P_PLATFORM + ").*?)?" // position
|
||||
, Pattern.DOTALL);
|
||||
|
||||
public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures, boolean equivs) throws IOException
|
||||
{
|
||||
final QueryDeparturesResult result = new QueryDeparturesResult();
|
||||
|
||||
// scrape page
|
||||
final String uri = departuresQueryUri(stationId, maxDepartures);
|
||||
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)
|
||||
{
|
||||
result.stationDepartures.add(new StationDepartures(new Location(LocationType.STATION, Integer.parseInt(stationId)), Collections
|
||||
.<Departure> emptyList(), null));
|
||||
return result;
|
||||
}
|
||||
else if (mHeadCoarse.group(4) != null)
|
||||
return new QueryDeparturesResult(Status.INVALID_STATION);
|
||||
else if (mHeadCoarse.group(5) != null)
|
||||
return new QueryDeparturesResult(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 Calendar currentTime = new GregorianCalendar(timeZone());
|
||||
currentTime.clear();
|
||||
ParserUtils.parseGermanDate(currentTime, mHeadFine.group(2));
|
||||
ParserUtils.parseEuropeanTime(currentTime, mHeadFine.group(3));
|
||||
final List<Departure> departures = new ArrayList<Departure>(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 plannedTime = new GregorianCalendar(timeZone());
|
||||
plannedTime.setTimeInMillis(currentTime.getTimeInMillis());
|
||||
ParserUtils.parseEuropeanTime(plannedTime, mDepFine.group(1));
|
||||
|
||||
if (plannedTime.getTimeInMillis() - currentTime.getTimeInMillis() < -PARSER_DAY_ROLLOVER_THRESHOLD_MS)
|
||||
plannedTime.add(Calendar.DAY_OF_MONTH, 1);
|
||||
|
||||
final Calendar predictedTime;
|
||||
final String prognosis = ParserUtils.resolveEntities(mDepFine.group(2));
|
||||
if (prognosis != null)
|
||||
{
|
||||
predictedTime = new GregorianCalendar(timeZone());
|
||||
if (prognosis.equals("pünktlich"))
|
||||
{
|
||||
predictedTime.setTimeInMillis(plannedTime.getTimeInMillis());
|
||||
}
|
||||
else
|
||||
{
|
||||
predictedTime.setTimeInMillis(currentTime.getTimeInMillis());
|
||||
ParserUtils.parseEuropeanTime(predictedTime, prognosis);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
predictedTime = null;
|
||||
}
|
||||
|
||||
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.getTime(), predictedTime != null ? predictedTime.getTime() : null, 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 " + stationId);
|
||||
}
|
||||
}
|
||||
|
||||
result.stationDepartures.add(new StationDepartures(new Location(LocationType.STATION, Integer.parseInt(stationId), null, location),
|
||||
departures, null));
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("cannot parse '" + mHeadCoarse.group(1) + "' on " + stationId);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("cannot parse '" + page + "' on " + stationId);
|
||||
}
|
||||
}
|
||||
}
|
82
test/de/schildbach/pte/live/ShProviderLiveTest.java
Normal file
82
test/de/schildbach/pte/live/ShProviderLiveTest.java
Normal file
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
* Copyright 2010, 2011 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 <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.schildbach.pte.live;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import de.schildbach.pte.NetworkProvider.WalkSpeed;
|
||||
import de.schildbach.pte.ShProvider;
|
||||
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 ShProviderLiveTest
|
||||
{
|
||||
private final ShProvider provider = new ShProvider();
|
||||
private static final String ALL_PRODUCTS = "IRSUTBFC";
|
||||
|
||||
@Test
|
||||
public void autocomplete() throws Exception
|
||||
{
|
||||
final List<Location> autocompletes = provider.autocompleteStations("Flughafen");
|
||||
|
||||
list(autocompletes);
|
||||
}
|
||||
|
||||
private void list(final List<Location> autocompletes)
|
||||
{
|
||||
System.out.print(autocompletes.size() + " ");
|
||||
for (final Location autocomplete : autocompletes)
|
||||
System.out.print(autocomplete.toDebugString() + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nearbyStation() throws Exception
|
||||
{
|
||||
final NearbyStationsResult result = provider.nearbyStations("715210", 0, 0, 0, 0);
|
||||
|
||||
System.out.println(result.stations.size() + " " + result.stations);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryDepartures() throws Exception
|
||||
{
|
||||
final QueryDeparturesResult result = provider.queryDepartures("715210", 0, false);
|
||||
|
||||
System.out.println(result.stationDepartures);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shortConnection() throws Exception
|
||||
{
|
||||
final QueryConnectionsResult result = provider.queryConnections(new Location(LocationType.STATION, 8002547, null, "Flughafen Hamburg"), null,
|
||||
new Location(LocationType.STATION, 715210, null, "Flughafen, Lübeck"), new Date(), true, ALL_PRODUCTS, WalkSpeed.NORMAL);
|
||||
System.out.println(result);
|
||||
final QueryConnectionsResult moreResult = provider.queryMoreConnections(result.context);
|
||||
System.out.println(moreResult);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue