Skip to main content

Get detailed per-trip transport information from TransportNSW

Project description

TransportNSW

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.

transport_type filters

1: Train
4: Light rail
5: Bus
7: Coach
9: Ferry
11: School bus
99: Walk
100: Walk
107: Cycle

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 TransportNSW import TransportNSW
tnsw = TransportNSW()
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.3.4.tar.gz (19.6 kB view details)

Uploaded Source

Built Distribution

PyTransportNSWv2-0.3.4-py3-none-any.whl (20.1 kB view details)

Uploaded Python 3

File details

Details for the file pytransportnswv2-0.3.4.tar.gz.

File metadata

  • Download URL: pytransportnswv2-0.3.4.tar.gz
  • Upload date:
  • Size: 19.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for pytransportnswv2-0.3.4.tar.gz
Algorithm Hash digest
SHA256 563d7a3268b7707d15eddf86310513af59a63b25dc9d9dc16abac4f1f1d64a7b
MD5 20828abf048ca89a377c18f8743f39d5
BLAKE2b-256 433c58450171312d4062fa2e5d0de8715cbb3a1c32a7fb04db52b77e24462f1c

See more details on using hashes here.

File details

Details for the file PyTransportNSWv2-0.3.4-py3-none-any.whl.

File metadata

File hashes

Hashes for PyTransportNSWv2-0.3.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1bb69906459525c60f1da495ae96f8c06551655eaf789fa693c05f2fcf19fe79
MD5 d310cb9f5588c8718bb3c7b06e479c20
BLAKE2b-256 cb512fb9410b69a887b4a1ef30d8938fc300207f06e5ea831db42943a19335f7

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