From b8d1d5b6d7d3422cb87fb6f8425def2a14a6221a Mon Sep 17 00:00:00 2001 From: Andreas Schildbach Date: Wed, 16 Oct 2019 10:36:16 +0200 Subject: [PATCH] BART: New provider for the Bay Area Rapid Transit (San Francisco, USA). --- .../schildbach/pte/AbstractHafasProvider.java | 1 + src/de/schildbach/pte/BartProvider.java | 72 ++++++++++++ src/de/schildbach/pte/NetworkId.java | 2 +- .../pte/live/BartProviderLiveTest.java | 110 ++++++++++++++++++ .../pte/live/secrets.properties.template | 1 + 5 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 src/de/schildbach/pte/BartProvider.java create mode 100644 test/de/schildbach/pte/live/BartProviderLiveTest.java diff --git a/src/de/schildbach/pte/AbstractHafasProvider.java b/src/de/schildbach/pte/AbstractHafasProvider.java index 000090a1..99a67fb0 100644 --- a/src/de/schildbach/pte/AbstractHafasProvider.java +++ b/src/de/schildbach/pte/AbstractHafasProvider.java @@ -123,6 +123,7 @@ public abstract class AbstractHafasProvider extends AbstractNetworkProvider { protected static final Pattern P_SPLIT_NAME_FIRST_COMMA = Pattern.compile("([^,]*), (.*)"); protected static final Pattern P_SPLIT_NAME_LAST_COMMA = Pattern.compile("(.*), ([^,]*)"); + protected static final Pattern P_SPLIT_NAME_NEXT_TO_LAST_COMMA = Pattern.compile("(.*), ([^,]*, [^,]*)"); protected static final Pattern P_SPLIT_NAME_PAREN = Pattern.compile("(.*) \\((.{3,}?)\\)"); protected String[] splitStationName(final String name) { diff --git a/src/de/schildbach/pte/BartProvider.java b/src/de/schildbach/pte/BartProvider.java new file mode 100644 index 00000000..cbd7cea6 --- /dev/null +++ b/src/de/schildbach/pte/BartProvider.java @@ -0,0 +1,72 @@ +/* + * Copyright 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; + +import de.schildbach.pte.dto.Product; +import okhttp3.HttpUrl; + +import java.util.regex.Matcher; + +/** + * Provider implementation for the Bay Area Rapid Transit (San Francisco, USA). + * + * @author Andreas Schildbach + */ +public class BartProvider extends AbstractHafasClientInterfaceProvider { + private static final HttpUrl API_BASE = HttpUrl.parse("https://planner.bart.gov/bin/"); + private static final Product[] PRODUCTS_MAP = { null, null, Product.CABLECAR, Product.REGIONAL_TRAIN, null, + Product.BUS, Product.FERRY, Product.SUBURBAN_TRAIN, Product.TRAM }; + private static final String DEFAULT_API_CLIENT = "{\"id\":\"BART\",\"type\":\"WEB\"}"; + + public BartProvider(final String apiAuthorization) { + this(DEFAULT_API_CLIENT, apiAuthorization); + } + + public BartProvider(final String apiClient, final String apiAuthorization) { + super(NetworkId.BART, API_BASE, PRODUCTS_MAP); + setTimeZone("America/Los_Angeles"); + setApiVersion("1.18"); + setApiClient(apiClient); + setApiAuthorization(apiAuthorization); + httpClient.setTrustAllCertificates(true); + } + + @Override + protected String[] splitStationName(final String name) { + final Matcher m = P_SPLIT_NAME_LAST_COMMA.matcher(name); + if (m.matches()) + return new String[] { m.group(2), m.group(1) }; + return super.splitStationName(name); + } + + @Override + protected String[] splitPOI(final String name) { + final Matcher m = P_SPLIT_NAME_LAST_COMMA.matcher(name); + if (m.matches()) + return new String[] { m.group(2), m.group(1) }; + return super.splitPOI(name); + } + + @Override + protected String[] splitAddress(final String address) { + final Matcher m = P_SPLIT_NAME_NEXT_TO_LAST_COMMA.matcher(address); + if (m.matches()) + return new String[] { m.group(2), m.group(1) }; + return super.splitAddress(address); + } +} diff --git a/src/de/schildbach/pte/NetworkId.java b/src/de/schildbach/pte/NetworkId.java index ca7b6b04..2dbedd35 100644 --- a/src/de/schildbach/pte/NetworkId.java +++ b/src/de/schildbach/pte/NetworkId.java @@ -76,7 +76,7 @@ public enum NetworkId { DUB, // United States - RTACHICAGO, OREGON, MASSACHUSETTS, CMTA, + BART, RTACHICAGO, OREGON, MASSACHUSETTS, CMTA, // Canada ONTARIO, QUEBEC, BRITISHCOLUMBIA, diff --git a/test/de/schildbach/pte/live/BartProviderLiveTest.java b/test/de/schildbach/pte/live/BartProviderLiveTest.java new file mode 100644 index 00000000..efad8d3d --- /dev/null +++ b/test/de/schildbach/pte/live/BartProviderLiveTest.java @@ -0,0 +1,110 @@ +/* + * Copyright 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 de.schildbach.pte.BartProvider; +import de.schildbach.pte.dto.Location; +import de.schildbach.pte.dto.LocationType; +import de.schildbach.pte.dto.NearbyLocationsResult; +import de.schildbach.pte.dto.Point; +import de.schildbach.pte.dto.QueryDeparturesResult; +import de.schildbach.pte.dto.QueryTripsResult; +import de.schildbach.pte.dto.SuggestLocationsResult; +import org.junit.Test; + +import java.util.Date; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +/** + * @author Andreas Schildbach + */ +public class BartProviderLiveTest extends AbstractProviderLiveTest { + public BartProviderLiveTest() { + super(new BartProvider(secretProperty("bart.api_authorization"))); + } + + @Test + public void nearbyStationsByCoordinate() throws Exception { + final NearbyLocationsResult result = queryNearbyStations(Location.coord(Point.fromDouble(37.7928550, -122.3968986))); + print(result); + } + + @Test + public void queryDepartures() throws Exception { + final QueryDeparturesResult result = queryDepartures("100013295", false); + print(result); + } + + @Test + public void queryDeparturesInvalidStation() throws Exception { + final QueryDeparturesResult result = queryDepartures("999999", false); + assertEquals(QueryDeparturesResult.Status.INVALID_STATION, result.status); + } + + @Test + public void suggestLocations() throws Exception { + final SuggestLocationsResult result = suggestLocations("Airport"); + print(result); + } + + @Test + public void suggestLocationsIdentified() throws Exception { + final SuggestLocationsResult result = suggestLocations("Embarcadero BART Station, San Francisco"); + print(result); + } + + @Test + public void shortTrip() throws Exception { + final Location from = new Location(LocationType.STATION, "100028458", null, "Airport Plaza, Concord"); + final Location to = new Location(LocationType.STATION, "100013295", null, "Embarcadero BART Station, San Francisco"); + final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null); + print(result); + assertEquals(QueryTripsResult.Status.OK, result.status); + assertTrue(result.trips.size() > 0); + final QueryTripsResult laterResult = queryMoreTrips(result.context, true); + print(laterResult); + final QueryTripsResult later2Result = queryMoreTrips(laterResult.context, true); + print(later2Result); + final QueryTripsResult later3Result = queryMoreTrips(later2Result.context, true); + print(later3Result); + final QueryTripsResult later4Result = queryMoreTrips(later3Result.context, true); + print(later4Result); + final QueryTripsResult later5Result = queryMoreTrips(later4Result.context, true); + print(later5Result); + final QueryTripsResult later6Result = queryMoreTrips(later5Result.context, true); + print(later6Result); + final QueryTripsResult earlierResult = queryMoreTrips(result.context, false); + print(earlierResult); + final QueryTripsResult earlier2Result = queryMoreTrips(earlierResult.context, false); + print(earlier2Result); + final QueryTripsResult earlier3Result = queryMoreTrips(earlier2Result.context, false); + print(earlier3Result); + final QueryTripsResult earlier4Result = queryMoreTrips(earlier3Result.context, false); + print(earlier4Result); + } + + @Test + public void tripBetweenCoordinates() throws Exception { + final Location from = Location.coord(Point.fromDouble(37.7927820, -122.3969430)); // Embarcadero BART Station, San Francisco + final Location to = Location.coord(Point.fromDouble(37.9793260, -122.0541840)); // Airport Plaza, Concord + final QueryTripsResult result = queryTrips(from, null, to, new Date(), true, null); + print(result); + } +} diff --git a/test/de/schildbach/pte/live/secrets.properties.template b/test/de/schildbach/pte/live/secrets.properties.template index 7e3c3ee9..5866be77 100644 --- a/test/de/schildbach/pte/live/secrets.properties.template +++ b/test/de/schildbach/pte/live/secrets.properties.template @@ -30,3 +30,4 @@ sncb.api_authorization = dsb.api_authorization = se.api_authorization = lu.api_authorization = +bart.api_authorization =