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.13.0.tar.gz (10.0 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: bods_client-0.13.0.tar.gz
  • Upload date:
  • Size: 10.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1025-azure

File hashes

Hashes for bods_client-0.13.0.tar.gz
Algorithm Hash digest
SHA256 b2dcf9e8f204a39746350de9246815d61b49c1fba7ed50fde70368c2344a8117
MD5 ac3eba4165182a233d0b10e2a03ee198
BLAKE2b-256 75851edfc1a58459e3a1bfaeb004350701039d4645aa3ca8521a335fa71d8549

See more details on using hashes here.

File details

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

File metadata

  • Download URL: bods_client-0.13.0-py3-none-any.whl
  • Upload date:
  • Size: 11.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.9.19 Linux/6.5.0-1025-azure

File hashes

Hashes for bods_client-0.13.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a39b00187927bc62ddc9de69abc5cf40525a20b95ac6fee48b41dab9a18d1a38
MD5 b26f1cdb9f2d91279e3b9b76e18ae608
BLAKE2b-256 27bb6b113531cdb85dfe5395712427e272dd953413470e19ef60924bd7949598

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