Package: underground
Python MTA UtilitiesThis is a set of Python utilities that I use to deal withreal-time NYC subway data.I usually want to know when trains are going to depart a specific stop along a specific train line, so right now the tools are mostly for that. But I tried to write them to support arbitrary functionality.InstallpipinstallundergroundOr if you'd like to live dangerously:pipinstallgit+https://github.com/nolanbconaway/underground.git#egg=undergroundTo request data from the MTA, you'll also need a free API key.Register here.Python APIOnce you have your API key, use the Python API like:importosfromundergroundimportmetadata,SubwayFeedAPI_KEY=os.getenv('MTA_API_KEY')ROUTE='Q'feed=SubwayFeed.get(ROUTE,api_key=API_KEY)# request will read from $MTA_API_KEY if a key is not providedfeed=SubwayFeed.get(ROUTE)# under the hood, the Q route is mapped to a URL. This call is equivalent:URL='https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-nqrw'feed=SubwayFeed.get(URL)# orURL=metadata.resolve_url(ROUTE)feed=SubwayFeed.get(URL)List train stops on each linefeed.extract_stop_dictwill return a dictionary of dictionaries, like:>>>feed.extract_stop_dict(){"route_1":{"stop_1":[datetime.datetime(...),datetime.datetime(...)],"stop_2":[datetime.datetime(...),datetime.datetime(...)],...},"route_2":{"stop_1":[datetime.datetime(...),datetime.datetime(...)],"stop_2":[datetime.datetime(...),datetime.datetime(...)],...}}CLITheundergroundcommand line tool is also installed with the package.feed$ underground feed --help
Usage: underground feed [OPTIONS] ROUTE_OR_URL

  Request an MTA feed via a route or URL.

  ROUTE_OR_URL may be either a feed URL or a route (which will be used to
  look up the feed url).

  Examples (both access the same feed):

      underground feed Q --json > feed_nrqw.json

      URL='https://api-endpoint.mta.info/Dataservice/mtagtfsfeeds/nyct%2Fgtfs-nqrw' &&
      underground feed $URL --json > feed_nrqw.json

Options:
  --api-key TEXT         MTA API key. Will be read from $MTA_API_KEY if not
                         provided.

  --json                 Option to output the feed data as JSON. Otherwise
                         output will be bytes.

  -r, --retries INTEGER  Retry attempts in case of API connection failure.
                         Default 100.

  --help                 Show this message and exit.stops$ underground stops --help
Usage: underground stops [OPTIONS] [H|M|D|1|Z|A|N|GS|SI|J|G|Q|L|B|R|F|E|2|7|W|
                          6|4|C|5|FS]
    
  Print out train departure times for all stops on a subway line.

Options:

  -f, --format TEXT      strftime format for stop times. Use `epoch` for a
                          unix timestamp.
  -r, --retries INTEGER  Retry attempts in case of API connection failure.
                          Default 100.
  --api-key TEXT         MTA API key. Will be read from $MTA_API_KEY if not
                          provided.
  -t, --timezone TEXT    Output timezone. Ignored if --epoch. Default to NYC
                          time.
  -s, --stalled-timeout INTEGER  Number of seconds between the last movement
                                 of a train and the API update before
                                 considering a train stalled. Default is 90 as
                                 recommended by the MTA. Numbers less than 1
                                 disable this check.
  --help                 Show this message and exit.Stops are printed to stdout in the formatstop_id t1 t2 ... tn.$exportMTA_API_KEY='...'$undergroundstopsQ|tail-2
Q05S19:0119:0919:1619:2519:3419:4419:5119:58
Q04S19:0319:1119:1819:2719:3619:4619:5320:00If you know your stop id (stop IDs can be found instops.txt), you can grep the results:$undergroundstopsQ|grepQ05S
Q05S19:0919:1619:2519:3419:4419:5119:58If you don't know your stop, see below for a handy tool!findstops$ underground findstops --help
Usage: underground findstops [OPTIONS] QUERY...

  Find your stop ID.

  Query a location and look for your stop ID, like:

  $ underground findstops parkside av

Options:

  --json  Option to output the data as JSON. Otherwise will be human readable
          table.

  --help  Show this message and exit.Enter the name of your stop and a table of stops with matching names will be returned.$ underground findstops parkside
ID: D27N    Direction: NORTH    Lat/Lon: 40.655292, -73.961495    Name: PARKSIDE AV
ID: D27S    Direction: SOUTH    Lat/Lon: 40.655292, -73.961495    Name: PARKSIDE AVSome names are ambiguous (try "fulton st"), for these you'll have to dig into themetadatamore carefully.