extract nearby station scanning to hafas superclass

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@221 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach 2010-10-02 17:33:32 +00:00
parent f7a6b7497f
commit 868a7bbc05
10 changed files with 198 additions and 119 deletions

View file

@ -0,0 +1,72 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package de.schildbach.pte;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author Andreas Schildbach
*/
public abstract class AbstractHafasProvider implements NetworkProvider
{
private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"(zebra[^\"]*)\">(.*?)</tr>", Pattern.DOTALL);
private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)"
+ "&.*?[\\?&]input=(\\d+)&[^\"]*\">([^<]*)<.*?", Pattern.DOTALL);
protected abstract String nearbyStationUri(String stationId);
public List<Station> nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations)
throws IOException
{
if (stationId == null)
throw new IllegalArgumentException("stationId must be given");
final List<Station> stations = new ArrayList<Station>();
final String uri = nearbyStationUri(stationId);
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mCoarse = P_NEARBY_COARSE.matcher(page);
while (mCoarse.find())
{
final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(2));
if (mFine.matches())
{
final int parsedLon = Integer.parseInt(mFine.group(1));
final int parsedLat = Integer.parseInt(mFine.group(2));
final int parsedId = Integer.parseInt(mFine.group(3));
final String parsedName = ParserUtils.resolveEntities(mFine.group(4));
stations.add(new Station(parsedId, parsedName, parsedLat, parsedLon, 0, null, null));
}
else
{
throw new IllegalArgumentException("cannot parse '" + mCoarse.group(2) + "' on " + uri);
}
}
if (maxStations == 0 || maxStations >= stations.size())
return stations;
else
return stations.subList(0, maxStations);
}
}

View file

@ -34,7 +34,7 @@ import org.json.JSONObject;
import de.schildbach.pte.QueryDeparturesResult.Status;
public class OebbProvider implements NetworkProvider
public class OebbProvider extends AbstractHafasProvider
{
public static final String NETWORK_ID = "fahrplan.oebb.at";
public static final String API_BASE = "http://fahrplan.oebb.at/bin/";
@ -121,43 +121,12 @@ public class OebbProvider implements NetworkProvider
}
}
private final String NEARBY_URI = API_BASE + "stboard.exe/dn?distance=50&near=Suchen&input=%d";
private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"zebracol-\\d\">(.*?)</tr>", Pattern.DOTALL);
private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?stboard\\.exe/.*?&input=.*?%23(\\d+)&.*?>(.*?)</a>.*?", Pattern.DOTALL);
private final String NEARBY_URI = API_BASE + "stboard.exe/dn?distance=50&near=Suchen&input=%s";
public List<Station> nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations)
throws IOException
@Override
protected String nearbyStationUri(final String stationId)
{
if (stationId == null)
throw new IllegalArgumentException("stationId must be given");
final List<Station> stations = new ArrayList<Station>();
final String uri = String.format(NEARBY_URI, stationId);
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mCoarse = P_NEARBY_COARSE.matcher(page);
while (mCoarse.find())
{
final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(1));
if (mFine.matches())
{
final int parsedId = Integer.parseInt(mFine.group(1));
final String parsedName = ParserUtils.resolveEntities(mFine.group(2));
final Station station = new Station(parsedId, parsedName, 0, 0, 0, null, null);
stations.add(station);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mCoarse.group(1) + "' on " + uri);
}
}
if (maxStations == 0 || maxStations >= stations.size())
return stations;
else
return stations.subList(0, maxStations);
return String.format(NEARBY_URI, stationId);
}
public StationLocationResult stationLocation(final String stationId) throws IOException

View file

@ -35,7 +35,7 @@ import de.schildbach.pte.QueryDeparturesResult.Status;
/**
* @author Andreas Schildbach
*/
public class RmvProvider implements NetworkProvider
public class RmvProvider extends AbstractHafasProvider
{
public static final String NETWORK_ID = "mobil.rmv.de";
public static final String NETWORK_ID_ALT = "www.rmv.de";
@ -81,12 +81,20 @@ public class RmvProvider implements NetworkProvider
private final static Pattern P_NEARBY_STATIONS = Pattern.compile("<a href=\"/auskunft/bin/jp/stboard.exe/dox.+?input=(\\d+).*?\">\\n"
+ "(.+?)\\s*\\((\\d+) m/[A-Z]+\\)\\n</a>", Pattern.DOTALL);
private final String NEARBY_URI = "http://www.rmv.de/auskunft/bin/jp/stboard.exe/dn?L=vs_rmv&distance=50&near&input=%d";
private final String NEARBY_URI = "http://www.rmv.de/auskunft/bin/jp/stboard.exe/dn?L=vs_rmv&distance=50&near&input=%s";
@Override
protected String nearbyStationUri(final String stationId)
{
return String.format(NEARBY_URI, stationId);
}
private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"zebracol-\\d\">(.*?)</tr>", Pattern.DOTALL);
private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?auskunft/bin/jp/stboard\\.exe/dn\\?L=vs_rmv&input=(\\d+).*?"
+ "&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)" //
+ "&REQMapRoute0\\.Location0\\.Name=(.*?)\">.*?", Pattern.DOTALL);
@Override
public List<Station> nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations)
throws IOException
{
@ -110,8 +118,7 @@ public class RmvProvider implements NetworkProvider
}
else if (stationId != null)
{
final String uri = String.format(NEARBY_URI, stationId);
final String uri = nearbyStationUri(stationId);
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mCoarse = P_NEARBY_COARSE.matcher(page);

View file

@ -35,7 +35,7 @@ import de.schildbach.pte.QueryDeparturesResult.Status;
/**
* @author Andreas Schildbach
*/
public class SbbProvider implements NetworkProvider
public class SbbProvider extends AbstractHafasProvider
{
public static final String NETWORK_ID = "fahrplan.sbb.ch";
@ -78,46 +78,12 @@ public class SbbProvider implements NetworkProvider
return results;
}
private final static String NEARBY_URI = "http://fahrplan.sbb.ch/bin/bhftafel.exe/dn?input=%d&distance=50&near=Anzeigen";
private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"zebra-row-\\d\">(.*?)</tr>", Pattern.DOTALL);
private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)"
+ "&REQMapRoute0\\.Location0\\.Name=(.*?)&sturl=.*?dn\\?input=(\\d+).*?", Pattern.DOTALL);
private final static String NEARBY_URI = "http://fahrplan.sbb.ch/bin/bhftafel.exe/dn?input=%s&distance=50&near=Anzeigen";
public List<Station> nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations)
throws IOException
@Override
protected String nearbyStationUri(final String stationId)
{
if (stationId == null)
throw new IllegalArgumentException("stationId must be given");
final List<Station> stations = new ArrayList<Station>();
final String uri = String.format(NEARBY_URI, stationId);
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mCoarse = P_NEARBY_COARSE.matcher(page);
while (mCoarse.find())
{
final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(1));
if (mFine.matches())
{
final int parsedLon = Integer.parseInt(mFine.group(1));
final int parsedLat = Integer.parseInt(mFine.group(2));
final String parsedName = ParserUtils.resolveEntities(mFine.group(3));
final int parsedId = Integer.parseInt(mFine.group(4));
final Station station = new Station(parsedId, parsedName, parsedLat, parsedLon, 0, null, null);
stations.add(station);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mCoarse.group(1) + "' on " + uri);
}
}
if (maxStations == 0 || maxStations >= stations.size())
return stations;
else
return stations.subList(0, maxStations);
return String.format(NEARBY_URI, stationId);
}
public StationLocationResult stationLocation(final String stationId) throws IOException

View file

@ -13,7 +13,7 @@ import java.util.regex.Pattern;
import de.schildbach.pte.QueryDeparturesResult.Status;
public class SncbProvider implements NetworkProvider
public class SncbProvider extends AbstractHafasProvider
{
public static final String NETWORK_ID = "hari.b-rail.be";
@ -33,46 +33,12 @@ public class SncbProvider implements NetworkProvider
throw new UnsupportedOperationException();
}
private final String NEARBY_URI = "http://hari.b-rail.be/HAFAS/bin/stboard.exe/en?input=%d&distance=50&near=Anzeigen";
private final static Pattern P_NEARBY_COARSE = Pattern.compile("<tr class=\"zebracol-\\d\">(.*?)</tr>", Pattern.DOTALL);
private final static Pattern P_NEARBY_FINE = Pattern.compile(".*?&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)"
+ "&REQMapRoute0\\.Location0\\.Name=(.*?)\">.*?en\\?input=(\\d+).*?", Pattern.DOTALL);
private final String NEARBY_URI = "http://hari.b-rail.be/HAFAS/bin/stboard.exe/en?input=%s&distance=50&near=Anzeigen";
public List<Station> nearbyStations(final String stationId, final int lat, final int lon, final int maxDistance, final int maxStations)
throws IOException
@Override
protected String nearbyStationUri(final String stationId)
{
if (stationId == null)
throw new IllegalArgumentException("stationId must be given");
final List<Station> stations = new ArrayList<Station>();
final String uri = String.format(NEARBY_URI, stationId);
final CharSequence page = ParserUtils.scrape(uri);
final Matcher mCoarse = P_NEARBY_COARSE.matcher(page);
while (mCoarse.find())
{
final Matcher mFine = P_NEARBY_FINE.matcher(mCoarse.group(1));
if (mFine.matches())
{
final int parsedLon = Integer.parseInt(mFine.group(1));
final int parsedLat = Integer.parseInt(mFine.group(2));
final String parsedName = ParserUtils.resolveEntities(mFine.group(3));
final int parsedId = Integer.parseInt(mFine.group(4));
final Station station = new Station(parsedId, parsedName, parsedLat, parsedLon, 0, null, null);
stations.add(station);
}
else
{
throw new IllegalArgumentException("cannot parse '" + mCoarse.group(1) + "' on " + uri);
}
}
if (maxStations == 0 || maxStations >= stations.size())
return stations;
else
return stations.subList(0, maxStations);
return String.format(NEARBY_URI, stationId);
}
public StationLocationResult stationLocation(final String stationId) throws IOException

View file

@ -40,7 +40,7 @@ public class GvhProviderLiveTest
}
@Test
public void nearby() throws Exception
public void nearbyStation() throws Exception
{
final List<Station> results = provider.nearbyStations("25000031", 0, 0, 0, 0);

View file

@ -18,11 +18,13 @@
package de.schildbach.pte.live;
import java.util.Date;
import java.util.List;
import org.junit.Test;
import de.schildbach.pte.OebbProvider;
import de.schildbach.pte.QueryConnectionsResult;
import de.schildbach.pte.Station;
import de.schildbach.pte.NetworkProvider.LocationType;
import de.schildbach.pte.NetworkProvider.WalkSpeed;
@ -33,6 +35,14 @@ public class OebbProviderLiveTest
{
private OebbProvider provider = new OebbProvider();
@Test
public void nearbyStation() throws Exception
{
final List<Station> results = provider.nearbyStations("902006", 0, 0, 0, 0);
System.out.println(results.size() + " " + results);
}
@Test
public void shortConnection() throws Exception
{

View file

@ -0,0 +1,40 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package de.schildbach.pte.live;
import java.util.List;
import org.junit.Test;
import de.schildbach.pte.RmvProvider;
import de.schildbach.pte.Station;
/**
* @author Andreas Schildbach
*/
public class RmvProviderLiveTest
{
private final RmvProvider provider = new RmvProvider();
@Test
public void nearbyStation() throws Exception
{
final List<Station> results = provider.nearbyStations("3000001", 0, 0, 0, 0);
System.out.println(results.size() + " " + results);
}
}

View file

@ -25,6 +25,7 @@ import org.junit.Test;
import de.schildbach.pte.Autocomplete;
import de.schildbach.pte.QueryConnectionsResult;
import de.schildbach.pte.SbbProvider;
import de.schildbach.pte.Station;
import de.schildbach.pte.NetworkProvider.LocationType;
import de.schildbach.pte.NetworkProvider.WalkSpeed;
@ -35,6 +36,14 @@ public class SbbProviderLiveTest
{
private SbbProvider provider = new SbbProvider();
@Test
public void nearbyStation() throws Exception
{
final List<Station> results = provider.nearbyStations("8500010", 0, 0, 0, 0);
System.out.println(results.size() + " " + results);
}
@Test
public void shortConnection() throws Exception
{

View file

@ -0,0 +1,40 @@
/*
* 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 <http://www.gnu.org/licenses/>.
*/
package de.schildbach.pte.live;
import java.util.List;
import org.junit.Test;
import de.schildbach.pte.SncbProvider;
import de.schildbach.pte.Station;
/**
* @author Andreas Schildbach
*/
public class SncbProviderLiveTest
{
private final SncbProvider provider = new SncbProvider();
@Test
public void nearbyStation() throws Exception
{
final List<Station> results = provider.nearbyStations("100080", 0, 0, 0, 0);
System.out.println(results.size() + " " + results);
}
}