live departures for Germany

git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@138 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
andreas.schildbach 2010-09-05 20:16:47 +00:00
parent 5f8c8db1e7
commit e9637344b6
2 changed files with 29 additions and 10 deletions

View file

@ -429,6 +429,7 @@ public final class BahnProvider implements NetworkProvider
final StringBuilder uri = new StringBuilder(); final StringBuilder uri = new StringBuilder();
uri.append("http://mobile.bahn.de/bin/mobil/bhftafel.exe/dox"); uri.append("http://mobile.bahn.de/bin/mobil/bhftafel.exe/dox");
uri.append("?start="); uri.append("?start=");
uri.append("&rt=1");
if (maxDepartures != 0) if (maxDepartures != 0)
uri.append("&maxJourneys=").append(maxDepartures); uri.append("&maxJourneys=").append(maxDepartures);
uri.append("&boardType=Abfahrt"); uri.append("&boardType=Abfahrt");
@ -448,7 +449,7 @@ public final class BahnProvider implements NetworkProvider
+ "<span class=\"bold\">(.*?)</span>.*?" // line + "<span class=\"bold\">(.*?)</span>.*?" // line
+ "&gt;&gt;\n\\s*(.+?)\\s*\n<br />\n" // destination + "&gt;&gt;\n\\s*(.+?)\\s*\n<br />\n" // destination
+ "<span class=\"bold\">(\\d{1,2}:\\d{2})</span>" // time + "<span class=\"bold\">(\\d{1,2}:\\d{2})</span>" // time
+ "(?:&nbsp;<span class=\"red\">ca. ([+-]?\\d+)</span>)?" // delay + "(?:&nbsp;<span class=\"[\\w ]*\">(?:(p&#252;nktl\\.)|ca. \\+(\\d+))</span>)?" // ontime, delay
+ "(?:(?:,&nbsp;)?(Gl\\. " + ParserUtils.P_PLATFORM + "))?" // position + "(?:(?:,&nbsp;)?(Gl\\. " + ParserUtils.P_PLATFORM + "))?" // position
, Pattern.DOTALL); , Pattern.DOTALL);
private static final Pattern P_DEPARTURES_URI_STATION_ID = Pattern.compile("input=(\\d+)"); private static final Pattern P_DEPARTURES_URI_STATION_ID = Pattern.compile("input=(\\d+)");
@ -480,13 +481,10 @@ public final class BahnProvider implements NetworkProvider
final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(1)); final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(1));
if (mDepFine.matches()) if (mDepFine.matches())
{ {
// line
final String line = normalizeLine(ParserUtils.resolveEntities(mDepFine.group(1))); final String line = normalizeLine(ParserUtils.resolveEntities(mDepFine.group(1)));
// destination
final String destination = ParserUtils.resolveEntities(mDepFine.group(2)); final String destination = ParserUtils.resolveEntities(mDepFine.group(2));
// time
final Calendar current = new GregorianCalendar(); final Calendar current = new GregorianCalendar();
current.setTime(currentTime); current.setTime(currentTime);
final Calendar parsed = new GregorianCalendar(); final Calendar parsed = new GregorianCalendar();
@ -496,15 +494,24 @@ public final class BahnProvider implements NetworkProvider
parsed.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH)); parsed.set(Calendar.DAY_OF_MONTH, current.get(Calendar.DAY_OF_MONTH));
if (ParserUtils.timeDiff(parsed.getTime(), currentTime) < -PARSER_DAY_ROLLOVER_THRESHOLD_MS) if (ParserUtils.timeDiff(parsed.getTime(), currentTime) < -PARSER_DAY_ROLLOVER_THRESHOLD_MS)
parsed.add(Calendar.DAY_OF_MONTH, 1); parsed.add(Calendar.DAY_OF_MONTH, 1);
final Date plannedTime = parsed.getTime();
// delay Date predictedTime = null;
final int delay = mDepFine.group(4) != null ? Integer.parseInt(mDepFine.group(4)) : 0; if (mDepFine.group(4) != null)
{
predictedTime = plannedTime;
}
else if (mDepFine.group(5) != null)
{
final int delay = Integer.parseInt(mDepFine.group(5));
parsed.add(Calendar.MINUTE, delay);
predictedTime = parsed.getTime();
}
// position final String position = ParserUtils.resolveEntities(mDepFine.group(6));
final String position = ParserUtils.resolveEntities(mDepFine.group(5));
final Departure dep = new Departure(parsed.getTime(), line, line != null ? LINES.get(line.charAt(0)) : null, position, 0, final Departure dep = new Departure(plannedTime, predictedTime, line, line != null ? LINES.get(line.charAt(0)) : null,
destination); position, 0, destination);
if (!departures.contains(dep)) if (!departures.contains(dep))
departures.add(dep); departures.add(dep);
} }

View file

@ -49,6 +49,18 @@ public class BahnProviderTest
+ "<span class=\"bold\">10:42</span>Gl. 1"); + "<span class=\"bold\">10:42</span>Gl. 1");
} }
@Test
public void departureWithOnTime()
{
assertFineDepartures("<a href=\"http://mobile.bahn.de/bin/mobil/traininfo.exe/dox/438441/245165/958/145668/80/si=8011160&amp;bt=dep&amp;ti=21:47&amp;pt=21:47&amp;p=1111101&amp;date=05.09.10&amp;max=10&amp;rt=1&amp;&amp;\">\n" //
+ "<span class=\"bold\">RE 38148</span>\n" //
+ "</a>\n" //
+ "&gt;&gt;\n" //
+ "Rathenow\n" //
+ "<br />\n" //
+ "<span class=\"bold\">21:58</span>&nbsp;<span class=\"green bold\">p&#252;nktl.</span>,&nbsp;Gl. 13");
}
private void assertFineConnectionDetails(String s) private void assertFineConnectionDetails(String s)
{ {
Matcher m = BahnProvider.P_CONNECTION_DETAILS_FINE.matcher(s); Matcher m = BahnProvider.P_CONNECTION_DETAILS_FINE.matcher(s);