Giter Club home page Giter Club logo

onemta's Introduction

icon

OneMTA

Java wrapper for the MTA Bus and Subway API

⚠️ The MTA API Terms and Conditions prohibits developers from giving users direct access to MTA servers. Any realtime data that is retrieved in this library must be served to users on your own servers.

OneMTA is a Java wrapper for the MTA SIRI REST API and MTA Realtime GTFS API.

Installation

OneMTA requires at least Java 8.

OneMTA also requires the protobuf-java library to be installed. Make sure you are using the same version as specified in the pom.xml file.

Compiled binaries can be installed from:

Authentication

  1. Request a bus token at https://bt.mta.info/wiki/Developers/Index.

  2. OneMTA requires static data from the MTA for most route and stop information. Latest static data for the MTA is available at https://new.mta.info/developers.

    Static data is only required for the endpoints you are using. All boroughs are required for buses, including bus company.

    static datafeeds

  3. Initialize OneMTA

    MTA mta = MTA.create(
        busToken,
        DataResource.create(DataResourceType.Bus_Bronx, new File("google_transit_bronx.zip")),
        DataResource.create(DataResourceType.Bus_Brooklyn, new File("google_transit_brooklyn.zip")),
        DataResource.create(DataResourceType.Bus_Manhattan, new File("google_transit_manhattan.zip")),
        DataResource.create(DataResourceType.Bus_Queens, new File("google_transit_queens.zip")),
        DataResource.create(DataResourceType.Bus_StatenIsland, new File("google_transit_staten_island.zip")),
        DataResource.create(DataResourceType.Bus_Company, new File("google_transit_bus_company.zip")),
        DataResource.create(DataResourceType.Subway, new File("google_transit_subway.zip")),
        DataResource.create(DataResourceType.LongIslandRailroad, new File("google_transit_lirr.zip")),
        DataResource.create(DataResourceType.MetroNorthRailroad, new File("google_transit_mnr.zip"))
    );

Features

Routes

Retrieve routes along with alerts and vehicles on the route.

Bus.Route M1    = mta.getBusRoute("M1", DataResourceType.Bus_Manhattan);
Subway.Route SI = mta.getSubwayRoute("SI");
LIRR.Route PW   = mta.getLIRRRoute(9);
MNR.Route HM    = mta.getMNRRoute(2);

Stops

Retrieve stops with alerts and vehicles en route.

Bus.Stop stop   = mta.getBusStop(400561);
Subway.Stop GCT = mta.getSubwayStop("631");
LIRR.Stop FLS   = mta.getLIRRStop("FLS");
MNR.Stop WLN    = mta.getMNRStop("1WN");

Vehicles

Retrieve live vehicle information from stops and routes.

Bus.Vehicle[] bus       = stop.getVehicles();
Subway.Vehicle[] subway = SI.getVehicles();
LIRR.Vehicle[] lirr     = PW.getVehicles();
MNR.Vehicle[] mnr       = WLN.getVehicles();

Alerts

Retrieve alerts for all stops and routes.

Bus.Alert[] busAlerts       = mta.getBusAlerts();
Subway.Alert[] subwayAlerts = SI.getAlerts();
LIRR.Alert[] lirrAlerts     = FLS.getAlerts();
MNR.Alert[] mnrAlerts       = mta.getMNRAlerts();

Contributing

Sample response data available on the reference branch.

Updating Protobuf Files

  1. Run install.sh

    or

    Install protobuf and gtfs realtime proto manually

  2. Run protobuf.sh

Notice About Tests

  • Tests are most reliable around rush hour.
  • Tests may not work during overnight hours.
  • Route tests may not work if the selected routes are out of service.
  • Stop tests may not work it the selected stops are out of service.
  • Alert tests may not work if no alerts are active.

Running Tests Locally

For local tests you can use Java 8+, however only methods in the Java 8 API may be used. The src/main/java9 folder should not be marked as a source root.

  • You must run install.sh to initialize test resources.
  • (Bus) Run tests locally by adding a text file named bus.txt that contains the bus token in the src/test/java/resources directory.

Running Tests using GitHub Actions

Developers running remote tests through GitHub Actions may do so by running the MTA CI workflow manually in the actions tab of your fork. Note that this requires a single secret, a BUS_TOKEN which contains the bus token.

 

This library is released under the GNU General Public License (GPL) v2.0.

  • @Katsute and @KatsuteDev are not affiliated with the MTA.

  • By using the MTA API you are subject to their Terms and Conditions.

    In developing your app, you will provide that the MTA data feed is available to others only from a non-MTA server. Accordingly, you will download and store the MTA data feed on a non-MTA server which users of your App will access in order to obtain data. MTA prohibits the development of an app that would make the data available to others directly from MTA's server(s).

onemta's People

Contributors

dependabot[bot] avatar katsute avatar kdevbot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

onemta's Issues

Failing to `resolveSubwayFeed` on express routes

Missing ~X routes

FeedMessage resolveSubwayFeed(final String route_id){
final String route = Objects.requireNonNull(MTASchema_Subway.resolveSubwayLine(route_id), "Subway route with ID '" + route_id + "' not found");
switch(route){
case "A":
case "C":
case "E":
case "FS":
case "SF":
case "SR":
return service.subway.getACE(subwayToken);
case "B":
case "D":
case "F":
case "M":
return service.subway.getBDFM(subwayToken);
case "G":
return service.subway.getG(subwayToken);
case "J":
case "Z":
return service.subway.getJZ(subwayToken);
case "N":
case "Q":
case "R":
case "W":
return service.subway.getNQRW(subwayToken);
case "L":
return service.subway.getL(subwayToken);
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "GS":
return service.subway.get1234567(subwayToken);
case "SI":
return service.subway.getSI(subwayToken);
default:
return null;

Use all bus data

Feature

Anytime a bus is requested:

  1. Check the cache
  2. Pull data for all busses
  3. Parse
  4. Fulfill the request

Reason

Current implementation runs a new request for every bus which is not efficient for enterprise. Requesting all busses then parsing is more efficient when submitting multiple requests.

Utility methods

Prerequisites

  • I have checked that no other similar feature request already exists.
  • I have checked discussions.
  • This feature request makes sense for the project.
  • I have checked that this feature does not already exist.
  • I am running the latest release version.

Feature

Provide utility methods for:

  • Checking if stops are exactly the same
  • Checking if bi-directional stops are the same main stop
  • Checking if express and non-express routes are the same route
  • Utility method for stop/route formatting for N/S/Express

Should be an interface class with:

  • .isExactStop() || .isExactRoute()
  • .isSameStop() || .isSameRoute()

Reason

Simplify equality checking.

Alert start/end periods are incorrect

Prerequisites

  • I have checked that no other similar issue already exists.
  • I have checked that this issue is actually a bug and not a feature.
  • I have checked discussions.
  • I have checked the documentation.

Operating System

Windows 10

Java Version

Java 17

Release Version

1.0.2

Issue

Same issue as #23, multiply this time number by 1000 to fix it.

private final Long start = requireNonNull(timeRange::getStart);
private final Long end = requireNonNull(timeRange::getEnd);

Code

No response

(internal) zip changes for MTA bus static data

This issue ticket is for internal tracking, developers please refer to issue #61.


Bus data is served in a folder within the zip, rather than the base directory. The below section needs to be modified to reflect this change.

final ZipEntry entry = entries.nextElement();
final String name = entry.getName();
switch(name){
case "agency.txt":
case "routes.txt":
case "stops.txt":
case "transfers.txt":
try(final BufferedReader IN = new BufferedReader(new InputStreamReader(zip.getInputStream(entry)))){
data.put(entry.getName(), new CSV(IN.lines().collect(Collectors.joining("\n"))));
}catch(final IOException e){
throw new DataResourceException("Failed to read zip entry: " + entry.getName(), e);
}
}

Add support for station transfer data

Prerequisites

  • I have checked that no other similar feature request already exists.
  • I have checked discussions.
  • This feature request makes sense for the project.
  • I have checked that this feature does not already exist.
  • I am running the latest release version.

Feature

Add support for station transfer information. Keys are non-unique, csv parser may need to be altered.

Sample csv:

from_stop_id,to_stop_id,transfer_type,min_transfer_time
101,101,2,180
103,103,2,180
104,104,2,180
106,106,2,180
107,107,2,180

This method might need to be updated:

public final List<String> getRow(final String keyHeader, final String key){
final int index = getHeaderIndex(keyHeader);
if(index == -1) return null;
for(final List<String> row : rows)
if(row.get(index).equals(key))
return new ArrayList<>(row);
return null;
}

Reason

Developers may want transfer information.

Alert description is null

Prerequisites

  • I have checked that no other similar issue already exists.
  • I have checked that this issue is actually a bug and not a feature.
  • I have checked discussions.
  • I have checked the documentation.

Operating System

ubuntu-latest

Java Version

Java 17

Release Version

1.0.1

Issue

Possibly caused by the change in the service status proto: OneBusAway/onebusaway-gtfs-realtime-api@9d31123

private final String descriptionText = requireNonNull(() -> alert.getDescriptionText().getTranslation(0).getText());

private final String descriptionText = requireNonNull(() -> alert.getDescriptionText().getTranslation(0).getText());

private final String descriptionText = requireNonNull(() -> alert.getDescriptionText().getTranslation(0).getText());

private final String descriptionText = requireNonNull(() -> alert.getDescriptionText().getTranslation(0).getText());

Code

No response

Arrival/departure time is incorrect

Prerequisites

  • I have checked that no other similar issue already exists.
  • I have checked that this issue is actually a bug and not a feature.
  • I have checked discussions.
  • I have checked the documentation.

Operating System

Windows 10

Java Version

Java 17

Release Version

1.0.1

Issue

Arrival/departure times are incorrect, code is expecting epoch to be in milliseconds while gtfs returns epoch in seconds. Fix this by multiplying the time by 1000.

private final Long arrival = requireNonNull(() -> stopTimeUpdate.getArrival().getTime());
private final Long departure = requireNonNull(() -> stopTimeUpdate.getDeparture().getTime());

private final Long arrival = requireNonNull(() -> stopTimeUpdate.getArrival().getTime());
private final Long departure = requireNonNull(() -> stopTimeUpdate.getDeparture().getTime());

private final Long arrival = requireNonNull(() -> stopTimeUpdate.getArrival().getTime());
private final Long departure = requireNonNull(() -> stopTimeUpdate.getDeparture().getTime());

Code

No response

Update tests causing issues

Prerequisites

  • I have checked that no other similar issue already exists.
  • I have checked that this issue is actually a bug and not a feature.
  • I have checked discussions.
  • I have checked the documentation.

Operating System

ubuntu-latest

Java Version

Java 8, 11, 17

Release Version

1.1.0-SNAPSHOT

Issue

Using the update method causes some tests to fail, particularly vehicles. Change the update tests so it updates a new instance, rather than the one pregenerated for the test case.

Code

No response

Document enums

Prerequisites

  • I have checked that no other similar feature request already exists.
  • I have checked discussions.
  • This feature request makes sense for the project.
  • I have checked that this feature does not already exist.
  • I am running the latest release version.

Feature

For methods that return enums as strings, use @see pointing towards the enum in the GTFS library.

Reason

Allow developers to easily see what string values can be returned.

Add method to refresh realtime data

Prerequisites

  • I have checked that no other similar feature request already exists.
  • I have checked discussions.
  • This feature request makes sense for the project.
  • I have checked that this feature does not already exist.
  • I am running the latest release version.

Feature

Add a new method .update(inplace?) to stop, route, and vehicle objects; to allow developers to refresh realtime data without having to make new calls.

The inplace parameter should be false by default and return a new object to prevent developers from accidentally changing objects that are in use by other concurrent operations.

Reason

Prevent redundant calls to the base object and to reduce confusion for developers expecting the realtime data to update everytime they make a call.

Cancelled Trip API

Prerequisites

  • I have checked that no other similar feature request already exists.
  • I have checked discussions.
  • This feature request makes sense for the project.
  • I have checked that this feature does not already exist.
  • I am running the latest release version.

Feature

The MTA SIRI Bus API has received an update: https://bustime.mta.info/wiki/Developers/CancelledTripAPIFeatures

Without Cancelled Trip Information With Cancelled Trip Information
v1 /api/siri/stop-monitoring.xml /api/2/siri/stop-monitoring.xml
v2 /api/siri/stop-monitoring.xml?version=2 /api/2/siri/stop-monitoring.xml?version=2

Stop-monitoring output

<MonitoredVehicleJourney>
  <MonitoredStopVisit>
--    <MonitoredCall>
--      <ArrivalStatus>cancelled</ArrivalStatus>
--      <DepartureStatus>cancelled</DepartureStatus>
--    </MonitoredCall>
++  <MonitoredVehicleJourney>
++      <VehicleStatus>cancelled</VehicleStatus>
++  </MonitoredVehicleJourney>
  </MonitoredStopVisit>
</MonitoredVehicleJourney>

Alert Support

 entity {
  id: "MTA NYCT_GH_A2-Weekday-SDon-063200_BX39_765"
  alert {
    informed_entity {
      agency_id: "MTA NYCT"
      trip {
        route_id: "BX39"
        direction_id: 0
      }
    }
    informed_entity {
      agency_id: "MTA NYCT"
      trip {
        route_id: "BX39"
        direction_id: 1
      }
    }
    effect: REDUCED_SERVICE
    description_text {
      translation {
        text: "The 10:32am BX39 to SOUNDVIEW AV/CORNELL AV is canceled"
        language: "EN"
      }
    }
  }
}

Reason

Required to keep up to date with API.

Add method to check if subway is express

Prerequisites

  • I have checked that no other similar feature request already exists.
  • I have checked discussions.
  • This feature request makes sense for the project.
  • I have checked that this feature does not already exist.
  • I am running the latest release version.

Feature

Add method to subway vehicles to check if they are express.

Achieved by checking the route ID of the trip, express routes will end with an x.

Local example: 7
Express example: 7X

Reason

Subway express statuses are a commonly requested field

Invalid LineRef for Bus Company

Prerequisites

  • I have checked that no other similar issue already exists.
  • I have checked that this issue is actually a bug and not a feature.
  • I have checked discussions.
  • I have checked the documentation.

Operating System

Windows 10

Java Version

Java 17

Release Version

1.1.0-SNAPSHOT

Issue

Busses under Bus Company use the LineRef: MTABC_ not MTA NYCT_

if(line != null) put("LineRef", "MTA%20NYCT_" + encodeUTF8(line));

private String routeID = requireNonNull(() -> monitoredVehicleJourney.getString("LineRef").substring(9));

The code needs to internally run a check if the Bus Company static data is used, and adjust the LineRef accordingly.

Code

No response

zip files for MTA busses have changed

This issue has been resolved in #63. Please update your library to 1.2.1^.


Static data for MTA busses have recently changed so that data is saved inside a folder rather than the base directory in the zip file; because of this change the zip files are currently incompatible with this library.

image

In order to use the data with this library you must:

  1. Extract the zip file

    image

  2. Open the google transit folder

    image

  3. Select all the files

    image

  4. Right click and send to a compressed zip

    image

  5. Rename the zip file to the correct google transit zip

    image

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.