mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-08 22:08:47 +00:00
Philadelphia
git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@464 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
parent
b2ba8a1c79
commit
37a93d92c3
5 changed files with 318 additions and 2 deletions
|
@ -727,7 +727,7 @@ 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_COARSE = Pattern.compile("<tr class=\"(zebra[^\"]*)\">(.*?)</tr>", Pattern.DOTALL);
|
||||||
private final static Pattern P_NEARBY_FINE_COORDS = Pattern
|
private final static Pattern P_NEARBY_FINE_COORDS = Pattern
|
||||||
.compile("&REQMapRoute0\\.Location0\\.X=(-?\\d+)&REQMapRoute0\\.Location0\\.Y=(-?\\d+)&");
|
.compile("REQMapRoute0\\.Location0\\.X=(-?\\d+)&(?:amp;)?REQMapRoute0\\.Location0\\.Y=(-?\\d+)&");
|
||||||
private final static Pattern P_NEARBY_FINE_LOCATION = Pattern.compile("[\\?&]input=(\\d+)&[^\"]*\">([^<]*)<");
|
private final static Pattern P_NEARBY_FINE_LOCATION = Pattern.compile("[\\?&]input=(\\d+)&[^\"]*\">([^<]*)<");
|
||||||
|
|
||||||
protected abstract String nearbyStationUri(String stationId);
|
protected abstract String nearbyStationUri(String stationId);
|
||||||
|
|
|
@ -50,5 +50,5 @@ public enum NetworkId
|
||||||
DUB,
|
DUB,
|
||||||
|
|
||||||
// United States
|
// United States
|
||||||
SF
|
SF, SEPTA
|
||||||
}
|
}
|
||||||
|
|
244
src/de/schildbach/pte/SeptaProvider.java
Normal file
244
src/de/schildbach/pte/SeptaProvider.java
Normal file
|
@ -0,0 +1,244 @@
|
||||||
|
/*
|
||||||
|
* 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.text.DateFormat;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Collections;
|
||||||
|
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 SeptaProvider extends AbstractHafasProvider
|
||||||
|
{
|
||||||
|
public static final NetworkId NETWORK_ID = NetworkId.SEPTA;
|
||||||
|
private static final String API_BASE = "http://airs1.septa.org/bin/";
|
||||||
|
private static final String API_URI = "http://airs1.septa.org/bin/extxml.exe";
|
||||||
|
|
||||||
|
private static final long PARSER_DAY_ROLLOVER_THRESHOLD_MS = 12 * 60 * 60 * 1000;
|
||||||
|
|
||||||
|
public SeptaProvider()
|
||||||
|
{
|
||||||
|
super(API_URI, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public NetworkId id()
|
||||||
|
{
|
||||||
|
return NETWORK_ID;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected char normalizeType(final String type)
|
||||||
|
{
|
||||||
|
final String ucType = type.toUpperCase();
|
||||||
|
|
||||||
|
// skip parsing of "common" lines, because this is America
|
||||||
|
|
||||||
|
// Regional
|
||||||
|
if (ucType.equals("RAI"))
|
||||||
|
return 'R';
|
||||||
|
|
||||||
|
// Subway
|
||||||
|
if (ucType.equals("BSS"))
|
||||||
|
return 'U';
|
||||||
|
if (ucType.equals("MFL"))
|
||||||
|
return 'U';
|
||||||
|
|
||||||
|
// Tram
|
||||||
|
if (ucType.equals("TRM"))
|
||||||
|
return 'T';
|
||||||
|
if (ucType.equals("NHS")) // Tro NHSL
|
||||||
|
return 'T';
|
||||||
|
|
||||||
|
// Bus
|
||||||
|
if (ucType.equals("BUS"))
|
||||||
|
return 'B';
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasCapabilities(final Capability... capabilities)
|
||||||
|
{
|
||||||
|
for (final Capability capability : capabilities)
|
||||||
|
if (capability == Capability.DEPARTURES)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private final String NEARBY_URI = API_BASE
|
||||||
|
+ "stboard.exe/en?input=%s&selectDate=today&boardType=dep&productsFilter=1111&distance=50&near=Anzeigen";
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected String nearbyStationUri(final String stationId)
|
||||||
|
{
|
||||||
|
return String.format(NEARBY_URI, ParserUtils.urlEncode(stationId));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String departuresQueryUri(final String stationId, final int maxDepartures)
|
||||||
|
{
|
||||||
|
final DateFormat DATE_FORMAT = new SimpleDateFormat("MM/dd/yyyy");
|
||||||
|
final DateFormat TIME_FORMAT = new SimpleDateFormat("h:mm a");
|
||||||
|
final Date now = new Date();
|
||||||
|
|
||||||
|
final StringBuilder uri = new StringBuilder();
|
||||||
|
uri.append(API_BASE).append("stboard.exe/en");
|
||||||
|
uri.append("?input=").append(stationId);
|
||||||
|
uri.append("&boardType=dep");
|
||||||
|
uri.append("&time=").append(ParserUtils.urlEncode(TIME_FORMAT.format(now)));
|
||||||
|
uri.append("&date=").append(ParserUtils.urlEncode(DATE_FORMAT.format(now)));
|
||||||
|
uri.append("&productsFilter=1111");
|
||||||
|
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_PAGE_COARSE = Pattern
|
||||||
|
.compile(
|
||||||
|
".*?" //
|
||||||
|
+ "(?:" //
|
||||||
|
+ "<div class=\"hfsTitleText\">([^<]*)<.*?" // location
|
||||||
|
+ "\n(\\d{2}/\\d{2}/\\d{4})[^\n]*\n" // date
|
||||||
|
+ "Departure (\\d{1,2}:\\d{2} [AP]M)\n.*?" // time
|
||||||
|
+ "(?:<table class=\"resultTable\"[^>]*>(.+?)</table>|(No trains in this space of time))" //
|
||||||
|
+ "|(input cannot be interpreted)|(Verbindung zum Server konnte leider nicht hergestellt werden|kann vom Server derzeit leider nicht bearbeitet werden))" //
|
||||||
|
+ ".*?" //
|
||||||
|
, 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=\"time\">(\\d{1,2}:\\d{2} [AP]M)</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 class=\"product\" src=\"/hafas-res/img/products/(\\w+)_pic\\.gif\" width=\"\\d+\" height=\"\\d+\" alt=\"([^\"]*)\".*?" // type,
|
||||||
|
// line
|
||||||
|
+ "<strong>\n" //
|
||||||
|
+ "<a href=\"http://airs1\\.septa\\.org/bin/stboard\\.exe/en\\?input=(\\d+)&[^>]*>" // destinationId
|
||||||
|
+ "\\s*(.*?)\\s*</a>\n" // destination
|
||||||
|
+ "</strong>.*?" //
|
||||||
|
+ "(?:<td class=\"center sepline top\">\n(" + ParserUtils.P_PLATFORM + ").*?)?" // position
|
||||||
|
, Pattern.DOTALL);
|
||||||
|
|
||||||
|
public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures) throws IOException
|
||||||
|
{
|
||||||
|
// scrape page
|
||||||
|
final String uri = departuresQueryUri(stationId, maxDepartures);
|
||||||
|
final CharSequence page = ParserUtils.scrape(uri);
|
||||||
|
|
||||||
|
// parse page
|
||||||
|
final Matcher mPageCoarse = P_DEPARTURES_PAGE_COARSE.matcher(page);
|
||||||
|
if (mPageCoarse.matches())
|
||||||
|
{
|
||||||
|
// messages
|
||||||
|
if (mPageCoarse.group(5) != null)
|
||||||
|
return new QueryDeparturesResult(new Location(LocationType.STATION, Integer.parseInt(stationId)),
|
||||||
|
Collections.<Departure> emptyList(), null);
|
||||||
|
else if (mPageCoarse.group(6) != null)
|
||||||
|
return new QueryDeparturesResult(Status.INVALID_STATION, Integer.parseInt(stationId));
|
||||||
|
else if (mPageCoarse.group(7) != null)
|
||||||
|
return new QueryDeparturesResult(Status.SERVICE_DOWN, Integer.parseInt(stationId));
|
||||||
|
|
||||||
|
final String location = ParserUtils.resolveEntities(mPageCoarse.group(1));
|
||||||
|
final Date currentTime = ParserUtils.joinDateTime(ParserUtils.parseAmericanDate(mPageCoarse.group(2)),
|
||||||
|
ParserUtils.parseAmericanTime(mPageCoarse.group(3)));
|
||||||
|
|
||||||
|
final List<Departure> departures = new ArrayList<Departure>(8);
|
||||||
|
String oldZebra = null;
|
||||||
|
|
||||||
|
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mPageCoarse.group(4));
|
||||||
|
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.parseAmericanTime(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.parseAmericanTime(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 " + stationId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return new QueryDeparturesResult(new Location(LocationType.STATION, Integer.parseInt(stationId), null, location), departures, null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new IllegalArgumentException("cannot parse '" + page + "' on " + stationId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -279,6 +279,18 @@ public final class ParserUtils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Date parseAmericanDate(final String str)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new SimpleDateFormat("MM/dd/yyyy").parse(str);
|
||||||
|
}
|
||||||
|
catch (final ParseException x)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static Date parseTime(final String str)
|
public static Date parseTime(final String str)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -291,6 +303,18 @@ public final class ParserUtils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Date parseAmericanTime(final String str)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return new SimpleDateFormat("h:mm a").parse(str);
|
||||||
|
}
|
||||||
|
catch (final ParseException x)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static Date joinDateTime(final Date date, final Date time)
|
public static Date joinDateTime(final Date date, final Date time)
|
||||||
{
|
{
|
||||||
final Calendar cDate = new GregorianCalendar();
|
final Calendar cDate = new GregorianCalendar();
|
||||||
|
|
48
test/de/schildbach/pte/live/SeptaProviderLiveTest.java
Normal file
48
test/de/schildbach/pte/live/SeptaProviderLiveTest.java
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
/*
|
||||||
|
* 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 org.junit.Test;
|
||||||
|
|
||||||
|
import de.schildbach.pte.SeptaProvider;
|
||||||
|
import de.schildbach.pte.dto.NearbyStationsResult;
|
||||||
|
import de.schildbach.pte.dto.QueryDeparturesResult;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Andreas Schildbach
|
||||||
|
*/
|
||||||
|
public class SeptaProviderLiveTest
|
||||||
|
{
|
||||||
|
private final SeptaProvider provider = new SeptaProvider();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void nearbyStation() throws Exception
|
||||||
|
{
|
||||||
|
final NearbyStationsResult result = provider.nearbyStations("1000001", 0, 0, 0, 0);
|
||||||
|
|
||||||
|
System.out.println(result.stations.size() + " " + result.stations);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void queryDepartures() throws Exception
|
||||||
|
{
|
||||||
|
final QueryDeparturesResult result = provider.queryDepartures("1000002", 0);
|
||||||
|
|
||||||
|
System.out.println(result.departures.size() + " " + result.departures);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue