.*?input=(\\d+).*?" // locationId
+ + "(?:
|(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(".*?" //
+ + "
(.*?)<.*?" // location
+ + "\n(\\d{2}\\.\\d{2}\\.\\d{2}),\n" // date
+ + "Abfahrt (\\d{1,2}:\\d{2}).*?" // time
+ , Pattern.DOTALL);
+ private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("(.*?)
", Pattern.DOTALL);
+ private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?" //
+ + "(\\d{1,2}:\\d{2}) | \n" // plannedTime
+ + "(?:
\n" //
+ + "(?: |(pünktlich|\\d{1,2}:\\d{2}))\n | \n" // predictedTime
+ + ")?.*?" //
+ + "
_pic\\.gif\"[^)
]*>\\s*(.*?)\\s*\n" //
+ + "
]*>" // destinationId
+ + "\\s*(.*?)\\s*\n" // destination
+ + ".*?" //
+ + "(?:
\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 mHeadCoarse = P_DEPARTURES_HEAD_COARSE.matcher(page);
+ if (mHeadCoarse.matches())
+ {
+ // messages
+ if (mHeadCoarse.group(4) != null)
+ return new QueryDeparturesResult(new Location(LocationType.STATION, Integer.parseInt(stationId)),
+ Collections. emptyList(), null);
+ else if (mHeadCoarse.group(5) != null)
+ return new QueryDeparturesResult(Status.INVALID_STATION, Integer.parseInt(stationId));
+ else if (mHeadCoarse.group(6) != null)
+ return new QueryDeparturesResult(Status.SERVICE_DOWN, Integer.parseInt(stationId));
+
+ final int locationId = Integer.parseInt(mHeadCoarse.group(2));
+
+ 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.parseDate(mHeadFine.group(2)),
+ ParserUtils.parseTime(mHeadFine.group(3)));
+ final List departures = new ArrayList(8);
+ String oldZebra = null;
+
+ final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mHeadCoarse.group(3));
+ 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.parseTime(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.parseTime(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);
+ }
+ }
+
+ final String[] nameAndPlace = splitNameAndPlace(location);
+ return new QueryDeparturesResult(new Location(LocationType.STATION, locationId, nameAndPlace[0], nameAndPlace[1]), departures, null);
+ }
+ else
+ {
+ throw new IllegalArgumentException("cannot parse '" + mHeadCoarse.group(1) + "' on " + stationId);
+ }
+ }
+ else
+ {
+ throw new IllegalArgumentException("cannot parse '" + page + "' on " + stationId);
+ }
+ }
+}
diff --git a/test/de/schildbach/pte/live/InvgProviderLiveTest.java b/test/de/schildbach/pte/live/InvgProviderLiveTest.java
new file mode 100644
index 00000000..72165816
--- /dev/null
+++ b/test/de/schildbach/pte/live/InvgProviderLiveTest.java
@@ -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 .
+ */
+
+package de.schildbach.pte.live;
+
+import org.junit.Test;
+
+import de.schildbach.pte.InvgProvider;
+import de.schildbach.pte.dto.NearbyStationsResult;
+import de.schildbach.pte.dto.QueryDeparturesResult;
+
+/**
+ * @author Andreas Schildbach
+ */
+public class InvgProviderLiveTest
+{
+ private final InvgProvider provider = new InvgProvider();
+
+ @Test
+ public void nearbyStation() throws Exception
+ {
+ final NearbyStationsResult result = provider.nearbyStations("80301", 0, 0, 0, 0);
+
+ System.out.println(result.stations.size() + " " + result.stations);
+ }
+
+ @Test
+ public void queryDepartures() throws Exception
+ {
+ final QueryDeparturesResult result = provider.queryDepartures("80301", 0);
+
+ System.out.println(result.departures.size() + " " + result.departures);
+ }
+}
|