Skip to main content

A Python client for the Department for Transport Bus Open Data Service API

Project description

bods-client

Build Status codecov Python Version

A Python client for the Department for Transport Bus Open Data Service API

Installation

pip install bods-client

Example

GTFS RT

All the vehicle locations for vehicles in a geographical location can be obtained using the get_gtfs_rt_data_feed method with a bounding box.

from bods_client.client import BODSClient
from bods_client.models import BoundingBox, GTFSRTParams

# An API key can be obtained by registering with the Bus Open Data Service
# https://data.bus-data.dft.gov.uk/account/signup/
>> API_KEY = "api-key"

>> bods = BODSClient(api_key=API_KEY)
>> bounding_box = BoundingBox(
    **{
        "min_latitude": 51.26,
        "max_latitude": 51.75,
        "min_longitude": -0.54,
        "max_longitude": 0.27,
    }
)
>> params = GTFSRTParams(bounding_box=bounding_box)
>> message = bods.get_gtfs_rt_data_feed(params=params)
>> message.entity[0]
id: "421354378097713049"
vehicle {
  trip {
    trip_id: ""
    route_id: ""
  }
  position {
    latitude: 51.712860107421875
    longitude: -0.38401100039482117
    bearing: 170.0
  }
  timestamp: 1614396229
  vehicle {
    id: "7214"
  }
}

This returns a google.transit.gtfs_realtime_pb2.FeedMessage object. More details about General Transit Feed Specification Realtime Transit (GTFS-RT) can be found here.

SIRI VM

Vehicle locations are also provided in the SIRI-VM XML format using the get_siri_vm_data_feed method. The data can then parsed using an xml parser library such as lxml.

from bods_client.client import BODSClient
from bods_client.models import BoundingBox, Siri, SIRIVMParams


>> API_KEY = "api-key"

>> client = BODSClient(api_key=API_KEY)
>> bounding_box = BoundingBox(
    **{
        "min_latitude": 51.267729,
        "max_latitude": 51.283191,
        "min_longitude": -0.142423,
        "max_longitude": 0.177432,
    }
)

>> params = SIRIVMParams(bounding_box=bounding_box)
>> siri_response = client.get_siri_vm_data_feed(params=params)
>> siri = Siri.from_bytes(siri_response)
>> siri.service_delivery.vehicle_monitoring_delivery.vehicle_activities[0]
VehicleActivity(
    recorded_at_time=datetime.datetime(
        2022, 1, 31, 19, 48, 24, tzinfo=datetime.timezone.utc
    ),
    item_identifier="05fc46f3-9629-4336-9a8d-f397030f5891",
    valid_until_time=datetime.datetime(2022, 1, 31, 21, 5, 21, 997139),
    monitored_vehicle_journey=MonitoredVehicleJourney(
        bearing=135.0,
        block_ref=None,
        framed_vehicle_journey_ref=None,
        vehicle_journey_ref="447183",
        destination_name="BEDDINGTON (ABELLIO LONDON)",
        destination_ref=None,
        orgin_name=None,
        origin_ref="40004410084D",
        origin_aimed_departure_time=datetime.datetime(
            2022, 1, 31, 19, 53, tzinfo=datetime.timezone.utc
        ),
        direction_ref="1",
        published_line_name="407",
        line_ref="296",
        vehicle_location=VehicleLocation(longitude=-0.077464, latitude=51.282658),
        operator_ref="TFLO",
        vehicle_ref="16085",
    ),
)

Details about the SIRI specification can be found here.

License

MIT

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

bods_client-0.14.0.tar.gz (1.0 MB view details)

Uploaded Source

Built Distribution

bods_client-0.14.0-py3-none-any.whl (11.5 kB view details)

Uploaded Python 3

File details

Details for the file bods_client-0.14.0.tar.gz.

File metadata

  • Download URL: bods_client-0.14.0.tar.gz
  • Upload date:
  • Size: 1.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.6.4

File hashes

Hashes for bods_client-0.14.0.tar.gz
Algorithm Hash digest
SHA256 0f315fe6bc2af3549ed8b3cbd26ff3ea92cbabaa659074279c80069908116bdc
MD5 bf4e66c4a814817b44090bc9e565b960
BLAKE2b-256 ec8bafa5f143c33553a13f59bccd24c82aaa916e20636a94ea5ddc8874b12b40

See more details on using hashes here.

File details

Details for the file bods_client-0.14.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bods_client-0.14.0-py3-none-any.whl
Algorithm Hash digest
SHA256 de5e98651687fba4d6892759dc15474c50edb887d27507958430224560a4ba03
MD5 b6af9c1aa3c6fa3225679424d35b7e37
BLAKE2b-256 0d11782e98deb4d13bb4d456a94dc905f5a1b1a1c8cae56f1f5ad9217faf185a

See more details on using hashes here.

Supported by

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