mirror of
https://gitlab.com/oeffi/public-transport-enabler.git
synced 2025-07-07 17:08:49 +00:00
auto-complete addresses for Austria
git-svn-id: https://public-transport-enabler.googlecode.com/svn/trunk@200 0924bc21-9374-b0fa-ee44-9ff1593b38f0
This commit is contained in:
parent
72d27105bd
commit
8f85e8ce86
9 changed files with 90 additions and 28 deletions
|
@ -16,16 +16,20 @@
|
||||||
*/
|
*/
|
||||||
package de.schildbach.pte;
|
package de.schildbach.pte;
|
||||||
|
|
||||||
|
import de.schildbach.pte.NetworkProvider.LocationType;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Andreas Schildbach
|
* @author Andreas Schildbach
|
||||||
*/
|
*/
|
||||||
public final class Autocomplete
|
public final class Autocomplete
|
||||||
{
|
{
|
||||||
|
public final LocationType locationType;
|
||||||
public final int locationId;
|
public final int locationId;
|
||||||
public final String location;
|
public final String location;
|
||||||
|
|
||||||
public Autocomplete(final int locationId, final String location)
|
public Autocomplete(final LocationType locationType, final int locationId, final String location)
|
||||||
{
|
{
|
||||||
|
this.locationType = locationType;
|
||||||
this.locationId = locationId;
|
this.locationId = locationId;
|
||||||
this.location = location;
|
this.location = location;
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,13 +64,13 @@ public final class BahnProvider implements NetworkProvider
|
||||||
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
||||||
if (mSingle.matches())
|
if (mSingle.matches())
|
||||||
{
|
{
|
||||||
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
|
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
||||||
while (mMulti.find())
|
while (mMulti.find())
|
||||||
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|
|
@ -72,12 +72,12 @@ public class LinzProvider implements NetworkProvider
|
||||||
// final double sLon = latLonToDouble(Integer.parseInt(mAutocomplete.group(2)));
|
// final double sLon = latLonToDouble(Integer.parseInt(mAutocomplete.group(2)));
|
||||||
// final double sLat = latLonToDouble(Integer.parseInt(mAutocomplete.group(3)));
|
// final double sLat = latLonToDouble(Integer.parseInt(mAutocomplete.group(3)));
|
||||||
final String sName = m.group(4).trim();
|
final String sName = m.group(4).trim();
|
||||||
results.add(new Autocomplete(sId, sName));
|
results.add(new Autocomplete(LocationType.STATION, sId, sName));
|
||||||
}
|
}
|
||||||
else if (m.group(5) != null)
|
else if (m.group(5) != null)
|
||||||
{
|
{
|
||||||
final String sName = m.group(5).trim();
|
final String sName = m.group(5).trim();
|
||||||
results.add(new Autocomplete(0, sName));
|
results.add(new Autocomplete(LocationType.ANY, 0, sName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -78,7 +78,7 @@ public class MvvProvider implements NetworkProvider
|
||||||
|
|
||||||
if (type.equals("stop"))
|
if (type.equals("stop"))
|
||||||
{
|
{
|
||||||
final Autocomplete result = new Autocomplete(locationId, city + ", " + location);
|
final Autocomplete result = new Autocomplete(LocationType.STATION, locationId, city + ", " + location);
|
||||||
results.add(result);
|
results.add(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ import de.schildbach.pte.QueryDeparturesResult.Status;
|
||||||
public class OebbProvider implements NetworkProvider
|
public class OebbProvider implements NetworkProvider
|
||||||
{
|
{
|
||||||
public static final String NETWORK_ID = "fahrplan.oebb.at";
|
public static final String NETWORK_ID = "fahrplan.oebb.at";
|
||||||
|
public static final String API_BASE = "http://fahrplan.oebb.at/bin/";
|
||||||
|
|
||||||
public boolean hasCapabilities(final Capability... capabilities)
|
public boolean hasCapabilities(final Capability... capabilities)
|
||||||
{
|
{
|
||||||
|
@ -30,30 +31,77 @@ public class OebbProvider implements NetworkProvider
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String NAME_URL = "http://fahrplan.oebb.at/bin/stboard.exe/dn?input=";
|
private static final String AUTOCOMPLETE_URI = API_BASE
|
||||||
private static final Pattern P_SINGLE_NAME = Pattern
|
+ "ajax-getstop.exe/dny?start=1&tpl=suggest2json&REQ0JourneyStopsS0A=255&REQ0JourneyStopsB=12&S=%s?&js=true&";
|
||||||
.compile(".*?<input type=\"hidden\" name=\"input\" value=\"(.+?)#(\\d+)\">.*", Pattern.DOTALL);
|
private static final String ENCODING = "ISO-8859-1";
|
||||||
private static final Pattern P_MULTI_NAME = Pattern.compile("<option value=\".+?#(\\d+)\">(.+?)</option>", Pattern.DOTALL);
|
private static final Pattern P_AUTOCOMPLETE_JSON = Pattern.compile("SLs\\.sls=(.*?);SLs\\.showSuggestion\\(\\);", Pattern.DOTALL);
|
||||||
|
private static final Pattern P_AUTOCOMPLETE_ID = Pattern.compile(".*?@L=(\\d+)@.*?");
|
||||||
|
|
||||||
public List<Autocomplete> autocompleteStations(final CharSequence constraint) throws IOException
|
public List<Autocomplete> autocompleteStations(final CharSequence constraint) throws IOException
|
||||||
{
|
{
|
||||||
final CharSequence page = ParserUtils.scrape(NAME_URL + ParserUtils.urlEncode(constraint.toString()));
|
final String uri = String.format(AUTOCOMPLETE_URI, ParserUtils.urlEncode(constraint.toString(), ENCODING));
|
||||||
|
final CharSequence page = ParserUtils.scrape(uri);
|
||||||
|
|
||||||
final List<Autocomplete> results = new ArrayList<Autocomplete>();
|
final Matcher mJson = P_AUTOCOMPLETE_JSON.matcher(page);
|
||||||
|
if (mJson.matches())
|
||||||
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
|
||||||
if (mSingle.matches())
|
|
||||||
{
|
{
|
||||||
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
|
final String json = mJson.group(1);
|
||||||
|
final List<Autocomplete> results = new ArrayList<Autocomplete>();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
final JSONObject head = new JSONObject(json);
|
||||||
|
final JSONArray aSuggestions = head.getJSONArray("suggestions");
|
||||||
|
|
||||||
|
for (int i = 0; i < aSuggestions.length(); i++)
|
||||||
|
{
|
||||||
|
final JSONObject suggestion = aSuggestions.optJSONObject(i);
|
||||||
|
if (suggestion != null)
|
||||||
|
{
|
||||||
|
final int type = suggestion.getInt("type");
|
||||||
|
final String value = suggestion.getString("value");
|
||||||
|
|
||||||
|
if (type == 1) // station
|
||||||
|
{
|
||||||
|
final String id = suggestion.getString("id");
|
||||||
|
final Matcher m = P_AUTOCOMPLETE_ID.matcher(id);
|
||||||
|
if (m.matches())
|
||||||
|
{
|
||||||
|
final int localId = Integer.parseInt(m.group(1));
|
||||||
|
results.add(new Autocomplete(LocationType.STATION, localId, value));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("id does not match: " + id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (type == 2) // address
|
||||||
|
{
|
||||||
|
results.add(new Autocomplete(LocationType.ADDRESS, 0, value));
|
||||||
|
}
|
||||||
|
else if (type == 4) // poi
|
||||||
|
{
|
||||||
|
results.add(new Autocomplete(LocationType.ANY, 0, value));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("unknown type " + type + " on " + uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
catch (final JSONException x)
|
||||||
|
{
|
||||||
|
x.printStackTrace();
|
||||||
|
throw new RuntimeException("cannot parse: '" + json + "' on " + uri, x);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
throw new RuntimeException("cannot parse: '" + page + "' on " + uri);
|
||||||
while (mMulti.find())
|
|
||||||
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String NEARBY_URI = "http://fahrplan.oebb.at/bin/stboard.exe/dn?distance=50&near=Suchen&input=%d";
|
private final String NEARBY_URI = "http://fahrplan.oebb.at/bin/stboard.exe/dn?distance=50&near=Suchen&input=%d";
|
||||||
|
|
|
@ -66,13 +66,13 @@ public class RmvProvider implements NetworkProvider
|
||||||
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
||||||
if (mSingle.matches())
|
if (mSingle.matches())
|
||||||
{
|
{
|
||||||
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
|
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
||||||
while (mMulti.find())
|
while (mMulti.find())
|
||||||
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|
|
@ -66,13 +66,13 @@ public class SbbProvider implements NetworkProvider
|
||||||
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
||||||
if (mSingle.matches())
|
if (mSingle.matches())
|
||||||
{
|
{
|
||||||
results.add(new Autocomplete(Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
|
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mSingle.group(2)), ParserUtils.resolveEntities(mSingle.group(1))));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
||||||
while (mMulti.find())
|
while (mMulti.find())
|
||||||
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
|
|
|
@ -73,7 +73,7 @@ public final class VbbProvider implements NetworkProvider
|
||||||
final Matcher mSingle = P_SINGLE_MASTID.matcher(page);
|
final Matcher mSingle = P_SINGLE_MASTID.matcher(page);
|
||||||
if (mSingle.matches())
|
if (mSingle.matches())
|
||||||
{
|
{
|
||||||
results.add(new Autocomplete(0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
|
results.add(new Autocomplete(LocationType.ANY, 0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -83,13 +83,14 @@ public final class VbbProvider implements NetworkProvider
|
||||||
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
final Matcher mSingle = P_SINGLE_NAME.matcher(page);
|
||||||
if (mSingle.matches())
|
if (mSingle.matches())
|
||||||
{
|
{
|
||||||
results.add(new Autocomplete(0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
|
results.add(new Autocomplete(LocationType.ANY, 0 /* TODO */, ParserUtils.resolveEntities(mSingle.group(1))));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
final Matcher mMulti = P_MULTI_NAME.matcher(page);
|
||||||
while (mMulti.find())
|
while (mMulti.find())
|
||||||
results.add(new Autocomplete(Integer.parseInt(mMulti.group(1)), ParserUtils.resolveEntities(mMulti.group(2))));
|
results.add(new Autocomplete(LocationType.STATION, Integer.parseInt(mMulti.group(1)), ParserUtils
|
||||||
|
.resolveEntities(mMulti.group(2))));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -18,9 +18,11 @@
|
||||||
package de.schildbach.pte.live;
|
package de.schildbach.pte.live;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import de.schildbach.pte.Autocomplete;
|
||||||
import de.schildbach.pte.QueryConnectionsResult;
|
import de.schildbach.pte.QueryConnectionsResult;
|
||||||
import de.schildbach.pte.SbbProvider;
|
import de.schildbach.pte.SbbProvider;
|
||||||
import de.schildbach.pte.NetworkProvider.LocationType;
|
import de.schildbach.pte.NetworkProvider.LocationType;
|
||||||
|
@ -62,4 +64,11 @@ public class SbbProviderLiveTest
|
||||||
final QueryConnectionsResult moreResult = provider.queryMoreConnections(result.linkLater);
|
final QueryConnectionsResult moreResult = provider.queryMoreConnections(result.linkLater);
|
||||||
System.out.println(moreResult);
|
System.out.println(moreResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void autoComplete() throws Exception
|
||||||
|
{
|
||||||
|
final List<Autocomplete> result = provider.autocompleteStations("haupt");
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue