An unofficial python client for Barcelona public transport (TMB) API.
Project description
PyTMB
Unofficial Python client for the Barcelona public transport (TMB) API.
This library is not affiliated with TMB. Use responsibly and respect the API Terms of Service.
Features
- Bus ETA lookup (
IBusClient.get_eta) - Metro ETA lookup (
IMetroClient.get_eta) - Planner routing with summary or full detail (
PlannerClient.calculate_routes) - Summary-by-default overloads (
detail="full"for raw typed responses) - Pydantic models for structured responses; simple sync API using
requests
Requirements
- Python >= 3.10
app_idandapp_keyfrom TMB's developer portal.- Register at: https://developer.tmb.cat/
Installation
PyPI:
pip install pytmb
From source (clone this repository):
pip install .
Quick Start
from pytmb import IBusClient, IMetroClient, PlannerClient
BUS_STOP_CODE = "1234" # Replace with a real stop code
METRO_STATIONS = [123, 456] # Replace with real station codes
app_id = "YOUR_APP_ID"
app_key = "YOUR_APP_KEY"
# Bus ETA
bus_client = IBusClient(app_id=app_id, app_key=app_key)
bus_etas = bus_client.get_eta(BUS_STOP_CODE, line="H10") # default: summarized objects (list[BusEtaInfo])
for eta in bus_etas:
print(f"Bus {eta.line_name} to {eta.destination} arriving in {eta.eta_minutes} min (ramp={eta.ramp_status})")
# Or get the full raw response (typed)
bus_raw = bus_client.get_eta(BUS_STOP_CODE, detail="full") # -> BusEtaResponse
print(bus_raw.timestamp, len(bus_raw.stops))
# Metro ETA
metro_client = IMetroClient(app_id=app_id, app_key=app_key)
metro_etas = metro_client.get_eta(METRO_STATIONS, line="L1") # summarized by default (list[MetroEtaInfo])
for eta in metro_etas:
print(f"Metro {eta.line_name} to {eta.destination} arriving in {eta.eta_minutes} min at station {eta.station_code}")
# Or full raw response
metro_raw = metro_client.get_eta(METRO_STATIONS, detail="full") # -> MetroEtaResponse
print(metro_raw.timestamp, len(metro_raw.lines))
# Planner (summary by default)
planner = PlannerClient(app_id=app_id, app_key=app_key)
itins = planner.calculate_routes((41.40, 2.18), (41.41, 2.16)) # list[PlannerItineraryInfo]
for i in itins[:3]: # show first few
print(i.overview, i.duration_in_minutes, "min")
# Full planner response
full_plan = planner.calculate_routes((41.40, 2.18), (41.41, 2.16), detail="full") # -> PlannerResponse
print(len(full_plan.plan.itineraries) if full_plan.plan else 0)
Roadmap / Ideas
- Transit lines & routes re-enabled once completed
- Additional endpoints (stops, alerts)
License
See LICENSE file.
Disclaimer
All trademarks and data belong to TMB. This is a community project.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pytmb-0.1.1b1.tar.gz.
File metadata
- Download URL: pytmb-0.1.1b1.tar.gz
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9097ba2b13929fe1a81bb8a915793e60dfc0fb9cb072d45a666d5a85346154f2
|
|
| MD5 |
7cabfa5f7bd3cf09e067121bfc99469f
|
|
| BLAKE2b-256 |
bb5ab2f939d0221b64552e554dc2b92c0f8b681e8740e70fd0aa40ec9c949adb
|
File details
Details for the file pytmb-0.1.1b1-py3-none-any.whl.
File metadata
- Download URL: pytmb-0.1.1b1-py3-none-any.whl
- Upload date:
- Size: 18.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.9 {"installer":{"name":"uv","version":"0.9.9"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ba21ca46d14881117832077fe2c630e332947b8c1165fc398ab9fcdcaf1e96d9
|
|
| MD5 |
d367424c9b082ed73bf20f7d168b2c61
|
|
| BLAKE2b-256 |
962d71af1c07efb8863d1aa40341444405c21333f866c8ae6a595494a518377d
|