mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-15 00:50:31 +00:00
split head parsing into coarse and fine
git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@96 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
parent
75a7692e7f
commit
4b1f0bb5e7
1 changed files with 76 additions and 62 deletions
|
@ -359,83 +359,97 @@ public class SbbProvider implements NetworkProvider
|
|||
return uri.toString();
|
||||
}
|
||||
|
||||
private static final Pattern P_DEPARTURES_HEAD = Pattern.compile(".*?<p class=\"qs\">\n?" //
|
||||
+ "<b>(.*?)</b><br />\n?"//
|
||||
+ "Abfahrt (\\d+:\\d+)\n?"//
|
||||
+ "Uhr, (\\d+\\.\\d+\\.\\d+)\n?"//
|
||||
+ "</p>.*", Pattern.DOTALL);
|
||||
private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("<p class=\"sq\">(.+?)</p>", Pattern.DOTALL);
|
||||
private static final Pattern P_DEPARTURES_FINE = Pattern.compile(".*?<b>(.*?)</b>\n?" //
|
||||
+ ">>\n?" //
|
||||
+ "(.*?)\n?" //
|
||||
+ "<br />\n?" //
|
||||
+ "<b>(\\d+:\\d+)</b>.*", Pattern.DOTALL);
|
||||
private static final Pattern P_DEPARTURES_URI_STATION_ID = Pattern.compile("input=(\\d+)");
|
||||
private static final Pattern P_DEPARTURES_HEAD_COARSE = Pattern.compile(".*?" //
|
||||
+ "(?:" //
|
||||
+ "<p class=\"qs\">\n(.+?)\n</p>.*?" //
|
||||
+ "(?:(.+)|(an dieser Haltestelle keines)).*?" //
|
||||
+ "<p class=\"links\">\n(.+?)\n</p>" //
|
||||
+ "|(Informationen zu))" //
|
||||
+ ".*?" //
|
||||
, Pattern.DOTALL);
|
||||
private static final Pattern P_DEPARTURES_HEAD_FINE = Pattern.compile("" //
|
||||
+ "<b>(.*?)</b><br />\n" // location
|
||||
+ "Abfahrt (\\d+:\\d+)\n" // time
|
||||
+ "Uhr, (\\d{2}\\.\\d{2}\\.\\d{2}).*?" // date
|
||||
+ "input=(\\d+).*?" // locationId
|
||||
, Pattern.DOTALL);
|
||||
private static final Pattern P_DEPARTURES_COARSE = Pattern.compile("<p class=\"sq\">\n(.+?)\n</p>", Pattern.DOTALL);
|
||||
private static final Pattern P_DEPARTURES_FINE = Pattern.compile("" //
|
||||
+ "<b>(.*?)</b>\n" // line
|
||||
+ ">>\n" //
|
||||
+ "(.*?)\n" // destination
|
||||
+ "<br />\n" //
|
||||
+ "<b>(\\d+:\\d+)</b>.*?" // time
|
||||
, Pattern.DOTALL);
|
||||
|
||||
public QueryDeparturesResult queryDepartures(final String uri) throws IOException
|
||||
{
|
||||
final CharSequence page = ParserUtils.scrape(uri);
|
||||
|
||||
final Matcher mStationId = P_DEPARTURES_URI_STATION_ID.matcher(uri);
|
||||
if (!mStationId.find())
|
||||
throw new IllegalStateException(uri);
|
||||
final int stationId = Integer.parseInt(mStationId.group(1));
|
||||
|
||||
// parse page
|
||||
final Matcher mHead = P_DEPARTURES_HEAD.matcher(page);
|
||||
if (mHead.matches())
|
||||
final Matcher mHeadCoarse = P_DEPARTURES_HEAD_COARSE.matcher(page);
|
||||
if (mHeadCoarse.matches())
|
||||
{
|
||||
final String location = ParserUtils.resolveEntities(mHead.group(1));
|
||||
final Date currentTime = ParserUtils.joinDateTime(ParserUtils.parseDate(mHead.group(3)), ParserUtils.parseTime(mHead.group(2)));
|
||||
final List<Departure> departures = new ArrayList<Departure>(8);
|
||||
// messages
|
||||
if (mHeadCoarse.group(3) != null)
|
||||
return new QueryDeparturesResult(uri, Status.NO_INFO);
|
||||
else if (mHeadCoarse.group(5) != null)
|
||||
return new QueryDeparturesResult(uri, Status.INVALID_STATION);
|
||||
|
||||
// choose matcher
|
||||
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(page);
|
||||
while (mDepCoarse.find())
|
||||
final String c = mHeadCoarse.group(1) + mHeadCoarse.group(4);
|
||||
final Matcher mHeadFine = P_DEPARTURES_HEAD_FINE.matcher(c);
|
||||
if (mHeadFine.matches())
|
||||
{
|
||||
final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(1));
|
||||
if (mDepFine.matches())
|
||||
{
|
||||
final Departure dep = parseDeparture(mDepFine, currentTime);
|
||||
if (!departures.contains(dep))
|
||||
departures.add(dep);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(1) + "' on " + uri);
|
||||
}
|
||||
}
|
||||
final String location = ParserUtils.resolveEntities(mHeadFine.group(1));
|
||||
final Date currentTime = ParserUtils.joinDateTime(ParserUtils.parseDate(mHeadFine.group(3)), ParserUtils
|
||||
.parseTime(mHeadFine.group(2)));
|
||||
final int locationId = Integer.parseInt(mHeadFine.group(4));
|
||||
final List<Departure> departures = new ArrayList<Departure>(8);
|
||||
|
||||
return new QueryDeparturesResult(uri, stationId, location, currentTime, departures);
|
||||
final Matcher mDepCoarse = P_DEPARTURES_COARSE.matcher(mHeadCoarse.group(2));
|
||||
while (mDepCoarse.find())
|
||||
{
|
||||
final Matcher mDepFine = P_DEPARTURES_FINE.matcher(mDepCoarse.group(1));
|
||||
if (mDepFine.matches())
|
||||
{
|
||||
final String line = normalizeLine(ParserUtils.resolveEntities(mDepFine.group(1)));
|
||||
|
||||
final String destination = ParserUtils.resolveEntities(mDepFine.group(2));
|
||||
|
||||
final Calendar current = new GregorianCalendar();
|
||||
current.setTime(currentTime);
|
||||
final Calendar parsed = new GregorianCalendar();
|
||||
parsed.setTime(ParserUtils.parseTime(mDepFine.group(3)));
|
||||
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 Departure dep = new Departure(parsed.getTime(), line, line != null ? LINES.get(line.charAt(0)) : null, 0, destination);
|
||||
|
||||
if (!departures.contains(dep))
|
||||
departures.add(dep);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("cannot parse '" + mDepCoarse.group(1) + "' on " + uri);
|
||||
}
|
||||
}
|
||||
|
||||
return new QueryDeparturesResult(uri, locationId, location, currentTime, departures);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalArgumentException("cannot parse '" + c + "' on " + uri);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return new QueryDeparturesResult(uri, Status.NO_INFO);
|
||||
throw new IllegalArgumentException("cannot parse '" + page + "' on " + uri);
|
||||
}
|
||||
}
|
||||
|
||||
private static Departure parseDeparture(final Matcher mDep, final Date currentTime)
|
||||
{
|
||||
// line
|
||||
final String line = normalizeLine(ParserUtils.resolveEntities(mDep.group(1)));
|
||||
|
||||
// destination
|
||||
final String destination = ParserUtils.resolveEntities(mDep.group(2));
|
||||
|
||||
// time
|
||||
final Calendar current = new GregorianCalendar();
|
||||
current.setTime(currentTime);
|
||||
final Calendar parsed = new GregorianCalendar();
|
||||
parsed.setTime(ParserUtils.parseTime(mDep.group(3)));
|
||||
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);
|
||||
|
||||
return new Departure(parsed.getTime(), line, line != null ? LINES.get(line.charAt(0)) : null, 0, destination);
|
||||
}
|
||||
|
||||
private static final Pattern P_NORMALIZE_LINE = Pattern.compile("([A-Za-zÄÖÜäöüß]+)[\\s-]*(.*)");
|
||||
|
||||
private static String normalizeLine(final String type, final String line)
|
||||
|
@ -471,7 +485,7 @@ public class SbbProvider implements NetworkProvider
|
|||
throw new IllegalStateException("cannot normalize line " + line);
|
||||
}
|
||||
|
||||
private static final Pattern P_NORMALIZE_TYPE_SBAHN = Pattern.compile("S\\d*");
|
||||
private static final Pattern P_NORMALIZE_TYPE_SBAHN = Pattern.compile("SN?\\d*");
|
||||
private static final Pattern P_NORMALIZE_TYPE_BUS = Pattern.compile("BUS\\w*");
|
||||
|
||||
private static char normalizeType(final String type)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue