();
while (mAddresses.find())
{
final String address = ParserUtils.resolveEntities(mAddresses.group(1)).trim();
if (!addresses.contains(address))
addresses.add(new Location(LocationType.ANY, 0, 0, 0, address + "!"));
}
if (type.equals("REQ0JourneyStopsS0K"))
fromAddresses = addresses;
else if (type.equals("REQ0JourneyStopsZ0K"))
toAddresses = addresses;
else if (type.equals("REQ0JourneyStops1.0K"))
viaAddresses = addresses;
else
throw new IllegalStateException(type);
}
if (fromAddresses != null || viaAddresses != null || toAddresses != null)
return new QueryConnectionsResult(fromAddresses, viaAddresses, toAddresses);
else
return queryConnections(baseUri, page);
}
@Override
public QueryConnectionsResult queryMoreConnections(final String uri) throws IOException
{
final CharSequence page = ParserUtils.scrape(uri, false, null, null, true);
final Matcher mError = P_QUERY_CONNECTIONS_ERROR.matcher(page);
if (mError.find())
{
if (mError.group(1) != null)
return QueryConnectionsResult.NO_CONNECTIONS;
if (mError.group(2) != null)
return QueryConnectionsResult.INVALID_DATE;
if (mError.group(3) != null)
throw new SessionExpiredException();
}
return queryConnections(uri, page);
}
private static final Pattern P_CONNECTIONS_ALL_DETAILS = Pattern.compile("" //
+ "");
private static final Pattern P_CONNECTIONS_HEAD = Pattern.compile(".*?" //
+ "von:\n\\s*(.*?)\\s*.*?" // from
+ "nach:\n\\s*(.*?)\\s*.*?" // to
+ "\nDatum:\n\n.., (\\d{2}\\.\\d{2}\\.\\d{2}).*?" // date
+ "(?:]*>\n(.*?)\n" //
+ "\n(.*?)Seitenanfang.*?
" //
, Pattern.DOTALL);
private static final Pattern P_CONNECTIONS_FINE = Pattern.compile(".*?" //
+ "]*>(\\d{2}\\.\\d{2}\\.\\d{2})" // departureDate
+ "(?: (\\d{2}\\.\\d{2}\\.\\d{2}))?.*?" // arrivalDate
+ "(\\d{1,2}:\\d{2}) ab.*?" // departureTime
+ "(\\d{1,2}:\\d{2}) an.*?" // arrivalTime
, Pattern.DOTALL);
private static final Pattern P_CONNECTION_DETAILS_COARSE = Pattern.compile("" //
+ " | \n(.*?)
\n" //
+ "\n(.*?)
\n" //
+ "" //
, Pattern.DOTALL);
private static final Pattern P_CONNECTION_DETAILS_FINE = Pattern.compile(".*?" //
+ "(?:]*>)?" // departureId
+ "([^\n<]*).*?" // departure
+ " | (?:(\\d{2}\\.\\d{2}\\.\\d{2})| ) | .*?" // departureDate
+ "\n?ab (\\d{2}:\\d{2}).*?" // departureTime
+ " | \\s*(?: |(.*?))\\s* | .*?" // departurePosition
+ "
]*>(.*?).*?)?" // line
+ "(?:]*>)?" // arrivalId
+ "([^\n<]*).*?" // arrival
+ " | (?:(\\d{2}\\.\\d{2}\\.\\d{2})| ) | .*?" // arrivalDate
+ "\n?an (\\d{2}:\\d{2}).*?" // arrivalTime
+ " | \\s*(?: |(.*?))\\s* | .*?" // arrivalPosition
, Pattern.DOTALL);
private QueryConnectionsResult queryConnections(final String firstUri, final CharSequence firstPage) throws IOException
{
// ugly workaround to fetch all details
final Matcher mAllDetailsAction = P_CONNECTIONS_ALL_DETAILS.matcher(firstPage);
if (!mAllDetailsAction.find())
throw new IOException("cannot find all details link in '" + firstPage + "' on " + firstUri);
final String allDetailsUri = mAllDetailsAction.group(1);
final CharSequence page = ParserUtils.scrape(allDetailsUri, false, null, null, true);
final Matcher mError = P_QUERY_CONNECTIONS_ERROR.matcher(page);
if (mError.find())
{
if (mError.group(1) != null)
return QueryConnectionsResult.NO_CONNECTIONS;
if (mError.group(2) != null)
return QueryConnectionsResult.INVALID_DATE;
if (mError.group(3) != null)
throw new SessionExpiredException();
}
// parse page
final Matcher mHead = P_CONNECTIONS_HEAD.matcher(page);
if (mHead.matches())
{
final Location from = new Location(LocationType.ANY, 0, 0, 0, ParserUtils.resolveEntities(mHead.group(1)));
final Location to = new Location(LocationType.ANY, 0, 0, 0, ParserUtils.resolveEntities(mHead.group(2)));
final Date currentDate = ParserUtils.parseDate(mHead.group(3));
final String linkEarlier = mHead.group(4) != null ? ParserUtils.resolveEntities(mHead.group(4)) : null;
final String linkLater = mHead.group(5) != null ? ParserUtils.resolveEntities(mHead.group(5)) : null;
final List connections = new ArrayList();
Date lastDate = currentDate;
final Matcher mConCoarse = P_CONNECTIONS_COARSE.matcher(page);
while (mConCoarse.find())
{
final String id = mConCoarse.group(1);
final String overview = mConCoarse.group(2);
final String details = mConCoarse.group(3);
final Matcher mConFine = P_CONNECTIONS_FINE.matcher(overview);
if (mConFine.matches())
{
final Date overviewDepartureDate = ParserUtils.parseDate(mConFine.group(1));
final Date overviewArrivalDate = mConFine.group(2) != null ? ParserUtils.parseDate(mConFine.group(2)) : null;
final Date overviewDepartureTime = ParserUtils.joinDateTime(overviewDepartureDate, ParserUtils.parseTime(mConFine.group(3)));
final Date overviewArrivalTime = ParserUtils.joinDateTime(overviewArrivalDate != null ? overviewArrivalDate
: overviewDepartureDate, ParserUtils.parseTime(mConFine.group(4)));
final String link = allDetailsUri; // TODO use print link?
final Connection connection = new Connection(id, link, overviewDepartureTime, overviewArrivalTime, null, null, 0, from.name, 0,
to.name, new ArrayList(1), null);
connections.add(connection);
final Matcher mDetCoarse = P_CONNECTION_DETAILS_COARSE.matcher(details);
while (mDetCoarse.find())
{
final String set = mDetCoarse.group(1) + mDetCoarse.group(2);
final Matcher mDetFine = P_CONNECTION_DETAILS_FINE.matcher(set);
if (mDetFine.matches())
{
final int departureId = mDetFine.group(1) != null ? Integer.parseInt(mDetFine.group(1)) : 0;
final String departure = ParserUtils.resolveEntities(mDetFine.group(2));
Date detailsDepartureDate = mDetFine.group(3) != null ? ParserUtils.parseDate(mDetFine.group(3)) : lastDate;
if (detailsDepartureDate != null)
lastDate = detailsDepartureDate;
final Date detailsDepartureTime = ParserUtils.parseTime(mDetFine.group(4));
final Date detailsDepartureDateTime = ParserUtils.joinDateTime(detailsDepartureDate, detailsDepartureTime);
final String lineType = mDetFine.group(6);
final int arrivalId = mDetFine.group(8) != null ? Integer.parseInt(mDetFine.group(8)) : 0;
final String arrival = ParserUtils.resolveEntities(mDetFine.group(9));
Date detailsArrivalDate = mDetFine.group(10) != null ? ParserUtils.parseDate(mDetFine.group(10)) : lastDate;
if (detailsArrivalDate != null)
lastDate = detailsArrivalDate;
final Date detailsArrivalTime = ParserUtils.parseTime(mDetFine.group(11));
final Date detailsArrivalDateTime = ParserUtils.joinDateTime(detailsArrivalDate, detailsArrivalTime);
if (!lineType.equals("fuss"))
{
if (departureId == 0)
throw new IllegalStateException("departureId");
final String departurePosition = mDetFine.group(5) != null ? ParserUtils.resolveEntities(mDetFine.group(5)) : null;
final String line = normalizeLine(lineType, ParserUtils.resolveEntities(mDetFine.group(7)));
if (arrivalId == 0)
throw new IllegalStateException("arrivalId");
final String arrivalPosition = mDetFine.group(12) != null ? ParserUtils.resolveEntities(mDetFine.group(12)) : null;
final Connection.Trip trip = new Connection.Trip(line, lineColors(line), 0, null, detailsDepartureDateTime,
departurePosition, departureId, departure, detailsArrivalDateTime, arrivalPosition, arrivalId, arrival, null);
connection.parts.add(trip);
}
else
{
final int min = (int) (detailsArrivalDateTime.getTime() - detailsDepartureDateTime.getTime()) / 1000 / 60;
final Connection.Footway footway = new Connection.Footway(min, departureId, departure, arrivalId, arrival);
connection.parts.add(footway);
}
}
else
{
throw new IllegalArgumentException("cannot parse '" + set + "' on " + allDetailsUri);
}
}
}
else
{
throw new IllegalArgumentException("cannot parse '" + overview + "' on " + allDetailsUri);
}
}
return new QueryConnectionsResult(allDetailsUri, from, null, to, linkLater, connections);
}
else
{
throw new IllegalArgumentException("cannot parse '" + page + "' on " + allDetailsUri);
}
}
@Override
public GetConnectionDetailsResult getConnectionDetails(final String connectionUri) throws IOException
{
throw new UnsupportedOperationException();
}
private String departuresQueryUri(final String stationId, final int maxDepartures)
{
final DateFormat TIME_FORMAT = new SimpleDateFormat("HH:mm");
final Date now = new Date();
final StringBuilder uri = new StringBuilder();
uri.append(API_BASE);
uri.append("stboard.exe/dn?L=vs_scotty.vs_stb");
uri.append("&input=").append(stationId);
uri.append("&boardType=dep");
uri.append("&time=").append(TIME_FORMAT.format(now));
uri.append("&productsFilter=111111111111");
uri.append("&additionalTime=0");
uri.append("&maxJourneys=").append(maxDepartures != 0 ? maxDepartures : 20);
uri.append("&start=yes");
uri.append("&selectDate=today");
uri.append("&monitor=1");
uri.append("&requestType=0");
uri.append("&view=preview");
uri.append("&disableEquivs=yes"); // don't use nearby stations
return uri.toString();
}
private static final Pattern P_DEPARTURES_ERROR = Pattern.compile("(Verbindung zum Server konnte leider nicht hergestellt werden)");
public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures) throws IOException
{
// scrape page
final String page = ParserUtils.scrape(departuresQueryUri(stationId, maxDepartures)).toString().substring(14);
final Matcher mError = P_DEPARTURES_ERROR.matcher(page);
if (mError.find())
{
if (mError.group(1) != null)
return new QueryDeparturesResult(Status.SERVICE_DOWN, Integer.parseInt(stationId));
}
try
{
final JSONObject head = new JSONObject(page);
final String location = ParserUtils.resolveEntities(head.getString("stationName"));
final int locationId = head.optInt("stationEvaId", -1);
// final boolean rt = head.optBoolean("rtInfo");
if (locationId == -1)
return new QueryDeparturesResult(Status.INVALID_STATION, Integer.parseInt(stationId));
final List departures = new ArrayList(8);
final JSONArray aDeparture = head.optJSONArray("journey");
if (aDeparture != null)
{
for (int i = 0; i < aDeparture.length(); i++)
{
final JSONObject departure = aDeparture.optJSONObject(i);
if (departure != null)
{
final Date time = ParserUtils.joinDateTime(ParserUtils.parseDate(departure.getString("da")), ParserUtils.parseTime(departure
.getString("ti")));
final String line = normalizeLine(ParserUtils.resolveEntities(departure.getString("pr")));
final String destination = ParserUtils.resolveEntities(departure.getString("st"));
String position = departure.optString("tr");
if (position != null)
position = "Gl. " + position;
final boolean rt = head.optBoolean("rt", false);
final String lineLink = departure.optString("tinfoline");
departures.add(new Departure(!rt ? time : null, rt ? time : null, line, line != null ? lineColors(line) : null, lineLink,
position, 0, destination, null));
}
}
}
return new QueryDeparturesResult(new Location(LocationType.STATION, locationId, 0, 0, location), departures, null);
}
catch (final JSONException x)
{
throw new RuntimeException("cannot parse: '" + page + "' on " + stationId, x);
}
}
private static final Pattern P_NORMALIZE_LINE_NUMBER = Pattern.compile("\\d{2,5}");
private static final Pattern P_NORMALIZE_LINE_RUSSIA = Pattern.compile("\\d{1,3}[A-Z]{2}");
private static final Pattern P_NORMALIZE_LINE_RUSSIA_INT = Pattern.compile("\\d{3}Y");
private String normalizeLine(final String line)
{
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;
// return '?' + strippedLine;
throw new IllegalStateException("cannot normalize type " + type + " line " + line);
}
if (line.length() == 0)
return "?";
if (P_NORMALIZE_LINE_RUSSIA.matcher(line).matches())
return "R" + line;
if (P_NORMALIZE_LINE_RUSSIA_INT.matcher(line).matches())
return "I" + line;
if (P_NORMALIZE_LINE_NUMBER.matcher(line).matches())
return "?" + 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("X")) // Interconnex, Connections only?
// return 'I';
if (ucType.equals("INT")) // Rußland, Connections only?
return 'I';
if (ucType.equals("D")) // Rußland
return 'I';
if (ucType.equals("RR")) // Finnland, Connections only?
return 'I';
if (ucType.equals("TLK")) // Tanie Linie Kolejowe, Polen
return 'I';
if (ucType.equals("EE")) // Rumänien, Connections only?
return 'I';
if (ucType.equals("SC")) // SuperCity, Tschechien
return 'I';
if (ucType.equals("EST")) // Eurostar Frankreich
return 'I';
if (ucType.equals("ALS")) // Spanien
return 'I';
if (ucType.equals("ARC")) // Spanien
return 'I';
if (ucType.equals("TLG")) // Spanien, Madrid
return 'I';
if (ucType.equals("HOT")) // Spanien, Nacht
return 'I';
if (ucType.equals("AVE")) // Alta Velocidad Española, Spanien
return 'I';
if (ucType.equals("NZ")) // Schweden, Nacht, via JSON API
return 'I';
if (ucType.equals("OZ")) // Schweden, Oeresundzug, Connections only?
return 'I';
if (ucType.equals("X2")) // Schweden, Connections only?
return 'I';
if (ucType.equals("X")) // Schweden, via JSON API
return 'I';
if (ucType.equals("LYN")) // Dänemark
return 'I';
if (ucType.equals("ARZ")) // Frankreich, Nacht
return 'I';
if (ucType.equals("ES")) // Eurostar Italia
return 'I';
if (ucType.equals("ICN")) // Italien, Nacht
return 'I';
if (ucType.equals("UUU")) // Italien, Nacht, Connections only?
return 'I';
if (ucType.equals("EZ")) // Erlebniszug
return 'R';
if (ucType.equals("S2")) // Helsinki-Turku, Connections only?
return 'R';
if (ucType.equals("RE")) // RegionalExpress Deutschland
return 'R';
if (ucType.equals("DPN")) // Connections only? TODO nicht evtl. doch eher ne S-Bahn?
return 'R';
if (ucType.equals("BRB")) // ABELLIO Rail, via JSON API
return 'R';
if (ucType.equals("ABR")) // Bayerische Regiobahn, via JSON API
return 'R';
if (ucType.equals("RTB")) // Rurtalbahn, via JSON API
return 'R';
if (ucType.equals("VIA"))
return 'R';
if (ucType.equals("PCC")) // Polen
return 'R';
if (ucType.equals("KM")) // Polen
return 'R';
if (ucType.equals("SKM")) // Polen
return 'R';
if (ucType.equals("SKW")) // Polen
return 'R';
if (ucType.equals("EX")) // Polen
return 'R';
if (ucType.equals("NEB")) // Kostrzyn-Berlin, via JSON API
return 'R';
if (ucType.equals("E")) // Budapest, Ungarn
return 'R';
if (ucType.equals("IP")) // Ozd, Ungarn
return 'R';
if (ucType.equals("ZR")) // Bratislava, Slovakai
return 'R';
if (ucType.equals("CAT")) // Stockholm-Arlanda, Arlanda Express
return 'R';
if (ucType.equals("N")) // Frankreich, Tours
return 'R';
if (ucType.equals("DPF")) // VX=Vogtland Express, Connections only?
return 'R';
if (ucType.equals("VBG")) // Vogtlandbahn, via JSON API
return 'R';
if (ucType.equals("SBE")) // Zittau-Seifhennersdorf, via JSON API
return 'R';
if (ucType.equals("UBB")) // Usedomer Bäderbahn, via JSON API
return 'R';
if (ucType.equals("HZL")) // Hohenzollerische Landesbahn, via JSON API
return 'R';
if (ucType.equals("ME")) // metronom Eisenbahngesellschaft, via JSON API
return 'R';
if (ucType.equals("MER")) // metronom regional, via JSON API
return 'R';
if (ucType.equals("PEG")) // Prignitzer Eisenbahn, via JSON API
return 'R';
if (ucType.equals("HLB")) // Hessische Landesbahn, via JSON API
return 'R';
if (ucType.equals("NWB")) // NordWestBahn, via JSON API
return 'R';
if (ucType.equals("VEN")) // Rhenus Veniro, via JSON API
return 'R';
if (ucType.equals("BOB")) // Bayerische Oberlandbahn, via JSON API
return 'R';
if (ucType.equals("SBB")) // Schweizerische Bundesbahnen, via JSON API
return 'R';
if (ucType.equals("ERB")) // eurobahn (Keolis Deutschland), via JSON API
return 'R';
if (ucType.equals("CAN")) // cantus Verkehrsgesellschaft, via JSON API
return 'R';
if (ucType.equals("ALX")) // Arriva-Länderbahn-Express, via JSON API
return 'R';
if (ucType.equals("VEC")) // vectus Verkehrsgesellschaft, via JSON API
return 'R';
if (ucType.equals("OSB")) // Ortenau-S-Bahn, via JSON API
return 'R';
if (ucType.equals("NOB")) // Nord-Ostsee-Bahn, via JSON API
return 'R';
if (ucType.equals("MRB")) // Mitteldeutsche Regiobahn, via JSON API
return 'R';
if (ucType.equals("ARR")) // Ostfriesland, via JSON API
return 'R';
if (ucType.equals("SHB")) // Schleswig-Holstein-Bahn, via JSON API
return 'R';
if (ucType.equals("BLB")) // Berchtesgadener Land Bahn, via JSON API
return 'R';
if (ucType.equals("AKN")) // AKN Eisenbahn AG, via JSON API
return 'R';
if (ucType.equals("EVB")) // Eisenbahnen und Verkehrsbetriebe Elbe-Weser, via JSON API
return 'R';
if (ucType.equals("EB")) // Erfurter Bahn, via JSON API
return 'R';
if (ucType.equals("HTB")) // Hörseltalbahn, via JSON API
return 'R';
if (ucType.equals("NBE")) // nordbahn, via JSON API
return 'R';
if (ucType.equals("DAB")) // Daadetalbahn, via JSON API
return 'R';
if (ucType.equals("WEG")) // Württembergische Eisenbahn-Gesellschaft, via JSON API
return 'R';
if (ucType.equals("RBG")) // Regental Bahnbetriebs GmbH, via JSON API
return 'R';
if (ucType.equals("OE")) // Ostdeutsche Eisenbahn, via JSON API
return 'R';
if (ucType.equals("CB")) // City Bahn Chemnitz, via JSON API
return 'R';
if (ucType.equals("MR")) // Märkische Regionalbahn, via JSON API
return 'R';
if (ucType.equals("OLA")) // Ostseeland Verkehr, via JSON API
return 'R';
if (ucType.equals("VX")) // Vogtland Express, via JSON API
return 'R';
if (ucType.equals("STB")) // Süd-Thüringen-Bahn, via JSON API
return 'R';
if (ucType.equals("RNV")) // Rhein-Neckar-Verkehr GmbH, via JSON API
return 'R';
if (ucType.equals("MBB")) // Mecklenburgische Bäderbahn Molli, via JSON API
return 'R';
if (ucType.equals("HSB")) // Harzer Schmalspurbahnen, via JSON API
return 'R';
if (ucType.equals("VE")) // Lutherstadt Wittenberg, via JSON API
return 'R';
if (ucType.equals("PRE")) // Pressnitztalbahn, via JSON API
return 'R';
if (ucType.equals("SDG")) // Sächsische Dampfeisenbahngesellschaft, via JSON API
return 'R';
if (ucType.equals("SOE")) // Sächsisch-Oberlausitzer Eisenbahngesellschaft, via JSON API
return 'R';
if (ucType.equals("FEG")) // Freiberger Eisenbahngesellschaft, via JSON API
return 'R';
if (ucType.equals("NEG")) // Norddeutsche Eisenbahngesellschaft Niebüll, via JSON API
return 'R';
if (ucType.equals("P")) // Kasbachtalbahn, via JSON API
return 'R';
if (ucType.equals("RSB")) // Schnellbahn Wien
return 'S';
if (ucType.equals("BSB")) // Breisgau S-Bahn, via JSON API
return 'S';
if (ucType.equals("DPN")) // S3 Bad Reichenhall-Freilassing, via JSON API
return 'S';
if (ucType.equals("SWE")) // Südwestdeutsche Verkehrs-AG, Ortenau-S-Bahn, via JSON API
return 'S';
if (ucType.equals("RER")) // Réseau Express Régional, Frankreich
return 'S';
if (ucType.equals("WKD")) // Warszawska Kolej Dojazdowa (Warsaw Suburban Railway)
return 'S';
if (ucType.equals("LKB")) // Connections only?
return 'T';
if (ucType.equals("WLB")) // via JSON API
return 'T';
if (ucType.equals("RFB"))
return 'B';
if (ucType.equals("OBU")) // Connections only?
return 'B';
if (ucType.equals("ASTSV")) // via JSON API
return 'B';
if (ucType.equals("ICB")) // ÖBB ICBus
return 'B';
if (ucType.equals("BSV")) // Deutschland, Connections only?
return 'B';
if (ucType.equals("LT")) // Linien-Taxi, Connections only?
return 'B';
if (ucType.equals("BUSS")) // via JSON API
return 'B';
if (ucType.equals("BUSSV")) // via JSON API
return 'B';
if (ucType.equals("BUSLEOBE")) // Rufbus, via JSON API
return 'B';
if (ucType.equals("BUSTN/TW")) // via JSON API
return 'B';
if (ucType.equals("BUSMKK")) // via JSON API
return 'B';
if (ucType.equals("O-B")) // Stadtbus, via JSON API
return 'B';
if (ucType.equals("SCH")) // Connections only?
return 'F';
if (ucType.equals("SCHIFF")) // via JSON API
return 'F';
if (ucType.equals("F")) // Fähre
return 'F';
if (ucType.equals("SB")) // Connections only?
return 'C';
if (ucType.equals("LIF"))
return 'C';
if (ucType.equals("SEILBAHN")) // via JSON API
return 'C';
if (ucType.equals("SSB")) // Graz Schlossbergbahn
return 'C';
if (ucType.equals("FLUG")) // via JSON API
return 'I';
if (ucType.equals("U70")) // U.K., Connections only?
return '?';
if (ucType.equals("R84")) // U.K., Connections only?
return '?';
if (ucType.equals("S84")) // U.K., Connections only?
return '?';
if (ucType.equals("T84")) // U.K., Connections only?
return '?';
return 0;
}
}