Skip to main content

Get detailed per-trip transport information from TransportNSW

Reason this release was yanked:

It's still got the debug messages in it :-(

Project description

TransportNSWv2

Python lib to access Transport NSW information.

How to Use

Get an API Key

An OpenData account and API key is required to request the data. More information on how to create the free account can be found here: https://opendata.transport.nsw.gov.au/user-guide. You need to register an application that needs both the Trip Planner and Realtime Vehicle Positions APIs

Get the stop IDs

The function needs the stop IDs for the source and destination, and optionally how many minutes from now the departure should be, and if you want to filter trips by a specific transport type. The easiest way to get the stop ID is via https://transportnsw.info/stops#/. It provides the option to search for either a location or a specific platform, bus stop or ferry wharf. Regardless of if you specify a general location for the origin or destination, the return information shows the stop_id for the actual arrival and destination platform, bus stop or ferry wharf.

If it's available, the general occupancy level and the latitude and longitude of the selected journey's vehicle (train, bus, etc) will be returned.

API Documentation

The source API details can be found here: https://opendata.transport.nsw.gov.au/sites/default/files/2023-08/Trip%20Planner%20API%20manual-opendataproduction%20v3.2.pdf

Parameters

.get_trip(origin_stop_id, destination_stop_id, api_key, [trip_wait_time = 0], [transport_type = 0])

TransportNSW's trip planner can work better if you use the general location IDs (eg Central Station) rather than a specific Stop ID (eg Central Station, Platform 19) for the destination, depending on the transport type. Forcing a specific end destination sometimes results in much more complicated trips. Also note that the API expects (and returns) the Stop IDs as strings, although so far they all appear to be numeric.

Sample Code

The following example will return the next trip that starts from a bus stop in St. Ives (207537) five minutes from now, to Central Station's general stop ID (10101100):

Code:

from TransportNSWv2 import TransportNSWv2
tnsw = TransportNSWv2()
journey = tnsw.get_trip('207537', '10101100', 'YOUR_API_KEY', 5)
print(journey)

Result:

{"due": 3, "origin_stop_id": "207537", "origin_name": "Mona Vale Rd at Shinfield Ave, St Ives", "departure_time": "2024-05-20T21:59:48Z", "destination_stop_id": "2000338", "destination_name": "Central Station, Platform 18, Sydney", "arrival_time": "2024-05-20T22:47:36Z", "origin_transport_type": "Bus", "origin_transport_name": "Sydney Buses Network", "origin_line_name": "195", "origin_line_name_short": "195", "changes": 1, "occupancy": "MANY_SEATS", "real_time_trip_id": "2096551", "latitude": -33.72665786743164, "longitude": 151.16305541992188}

Fun fact: TransportNSW's raw API output calls itself JSON, but it uses single quotes for strings in defiance of the JSON standards. When using this wrapper the output is formatted such that jq, for example, is happy with it.

  • due: the time (in minutes) before the journey starts
  • origin_stop_id: the specific departure stop id
  • origin_name: the name of the departure location
  • departure_time: the departure time, in UTC
  • destination_stop_id: the specific destination stop id
  • destination_name: the name of the destination location
  • arrival_time: the planned arrival time at the origin, in UTC
  • origin_transport_type: the type of transport, eg train, bus, ferry etc
  • origin_transport_name: the full name of the transport provider
  • origin_line_name & origin_line_name_short: the full and short names of the journey
  • changes: how many transport changes are needed on the journey
  • occupancy: how full the vehicle is, if available
  • real_time_trip_id: the unique TransportNSW id for that specific journey, if available
  • latitude & longitude: The location of the vehicle, if available

Please note that the origin and destination detail is just that - information about the first and last stops on the journey at the time the request was made. We don't return any intermediate steps, transport change types etc other than the total number of changes - the assumption is that you'll know the details of your specified trip, you just want to know when the next departure is. If you need much more detailed information then I recommend that you use the full Transport NSW trip planner website or application. Also note that the 'transport_type' filter, if present, only makes sure that at least one leg of the journey includes that transport type.

Thank you

Thank you Dav0815 for your TransportNSW library that the vast majority of this fork is based on. I couldn't have done it without you! https://github.com/Dav0815/TransportNSW

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyTransportNSWv2-0.5.4.tar.gz (20.6 kB view details)

Uploaded Source

File details

Details for the file PyTransportNSWv2-0.5.4.tar.gz.

File metadata

  • Download URL: PyTransportNSWv2-0.5.4.tar.gz
  • Upload date:
  • Size: 20.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.14

File hashes

Hashes for PyTransportNSWv2-0.5.4.tar.gz
Algorithm Hash digest
SHA256 9139297719f1f919b5a3c06934876d79fb945a878d86ddb9920e47a642128f8a
MD5 c1bf01d108186e304f5eec2afb9747c3
BLAKE2b-256 7cd4138b4aed81c7f330e3db29f436944f985d8a5070e2181f582795ff30619e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page