mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-07 05:28:46 +00:00
use API for autocompletes
git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@449 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
parent
53b58f3306
commit
dfa35cbccf
2 changed files with 28 additions and 29 deletions
|
@ -54,7 +54,7 @@ public final class BahnProvider extends AbstractHafasProvider
|
|||
|
||||
public BahnProvider()
|
||||
{
|
||||
super(null, null);
|
||||
super("http://reiseauskunft.bahn.de/bin/extxml.exe", null);
|
||||
}
|
||||
|
||||
public NetworkId id()
|
||||
|
@ -67,33 +67,6 @@ public final class BahnProvider extends AbstractHafasProvider
|
|||
return true;
|
||||
}
|
||||
|
||||
private static final String NAME_URL = API_BASE + "bhftafel.exe/dox?input=";
|
||||
private static final Pattern P_SINGLE_NAME = Pattern.compile(".*<input type=\"hidden\" name=\"input\" value=\"(.+?)#(\\d+)\" />.*",
|
||||
Pattern.DOTALL);
|
||||
private static final Pattern P_MULTI_NAME = Pattern.compile("<option value=\".+?#(\\d+)\">(.+?)</option>", Pattern.DOTALL);
|
||||
|
||||
@Override
|
||||
public List<Location> autocompleteStations(final CharSequence constraint) throws IOException
|
||||
{
|
||||
final CharSequence page = ParserUtils.scrape(NAME_URL + ParserUtils.urlEncode(constraint.toString()));
|
||||
|
||||
final List<Location> results = new ArrayList<Location>();
|
||||
|
||||
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
||||
if (mSingle.matches())
|
||||
{
|
||||
results.add(new Location(LocationType.STATION, Integer.parseInt(mSingle.group(2)), null, ParserUtils.resolveEntities(mSingle.group(1))));
|
||||
}
|
||||
else
|
||||
{
|
||||
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
||||
while (mMulti.find())
|
||||
results.add(new Location(LocationType.STATION, Integer.parseInt(mMulti.group(1)), null, ParserUtils.resolveEntities(mMulti.group(2))));
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private final static Pattern P_NEARBY_STATIONS = Pattern
|
||||
.compile("<a class=\"uLine\" href=\".+?!X=(\\d+)!Y=(\\d+)!id=(\\d+)!dist=(\\d+).*?\">(.+?)</a>");
|
||||
|
||||
|
@ -468,7 +441,8 @@ public final class BahnProvider extends AbstractHafasProvider
|
|||
|
||||
public QueryDeparturesResult queryDepartures(final String stationId, final int maxDepartures) throws IOException
|
||||
{
|
||||
final CharSequence page = ParserUtils.scrape(departuresQueryUri(stationId, maxDepartures));
|
||||
final String uri = departuresQueryUri(stationId, maxDepartures);
|
||||
final CharSequence page = ParserUtils.scrape(uri);
|
||||
|
||||
final Matcher mMessage = P_DEPARTURES_MESSAGES.matcher(page);
|
||||
if (mMessage.find())
|
||||
|
|
|
@ -18,6 +18,7 @@
|
|||
package de.schildbach.pte.live;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -44,6 +45,30 @@ public class BahnProviderLiveTest
|
|||
System.out.println(queryDepartures.departures);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autocompleteIncomplete() throws Exception
|
||||
{
|
||||
final List<Location> autocompletes = provider.autocompleteStations("Be");
|
||||
|
||||
list(autocompletes);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void autocompleteIdentified() throws Exception
|
||||
{
|
||||
final List<Location> autocompletes = provider.autocompleteStations("Berlin");
|
||||
|
||||
list(autocompletes);
|
||||
}
|
||||
|
||||
private void list(final List<Location> autocompletes)
|
||||
{
|
||||
System.out.print(autocompletes.size() + " ");
|
||||
for (final Location autocomplete : autocompletes)
|
||||
System.out.print(autocomplete.toDebugString() + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shortConnection() throws Exception
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue