Storstockholms Lokaltrafik (SL) data via Trafiklab API
Project description
Trafiklab-Sl
A data model for Storstockholms Lokaltrafik (SL) data.
Also contains an async client for fetching data from the Trafiklab API.
Installation
Install using pip install -U trafiklab-sl
Development
To install the package for development, clone the repository and run:
python -m venv .venv
source .venv/bin/activate
pip install -e '.[dev,test]'
Usage
The client is based on the aiohttp library and is async. It is used to fetch data from the Trafiklab API.
The library supports following SL APIs:
- SL Deviatons API
- SL Transport API
- "Departures from Site"
- "Sites"
- SL Stop lookup API
More APIs will be added in the future.
Example
Here is an example of how to use the client to get upcoming train departures at Stockholm Central (site Id 1002).
import asyncio
import aiohttp
from tsl.clients.stoplookup import StopLookupClient
from tsl.clients.transport import TransportClient
from tsl.models.common import TransportMode
async def main():
async with aiohttp.ClientSession() as session:
# perform stop lookup to get the site id for Stockholm Central
lookup_client = StopLookupClient("your-SL-Platsuppslag-api-key", session)
stops = await lookup_client.get_stops("Stockholm Central")
if (central_station := next(iter(stops), None)) is None:
raise RuntimeError(r"Could not find Stockholm Central. Weird ¯\_(ツ)_/¯")
# get the transport API site id for Stockholm Central
transport_api_siteid = central_station.SiteId.transport_siteid
# get upcoming train departures
client = TransportClient(session)
reponse = await client.get_site_departures(transport_api_siteid, transport=TransportMode.TRAIN)
print(f"Upcoming trains at {central_station.Name}:")
for departure in sorted(reponse.departures, key=lambda d: d.expected):
print(
f"[{departure.line.designation}] platform {departure.stop_point.designation}"
f" to {departure.destination} ({departure.display})"
)
asyncio.run(main())
Contributing
Both bug reports and pull requests are appreciated.
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 trafiklab_sl-1.0.0.tar.gz.
File metadata
- Download URL: trafiklab_sl-1.0.0.tar.gz
- Upload date:
- Size: 9.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
21e6aaae543001b740e205ad4ce263744c76cb449b73968b9dbb1e0462b1a38c
|
|
| MD5 |
d14bfaabcd643301914657fd387bf3cc
|
|
| BLAKE2b-256 |
e4243ec9c67b3e3afa63f3f5d40e8e77eaa329d97cb93aadceed0488ca0ebe68
|
File details
Details for the file trafiklab_sl-1.0.0-py3-none-any.whl.
File metadata
- Download URL: trafiklab_sl-1.0.0-py3-none-any.whl
- Upload date:
- Size: 12.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.12.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3dfbc64b77626f45c11914e698ae740a8d4faa8e21185f09cb7c665e2d15e996
|
|
| MD5 |
49961e76165050abdb53d55224492ace
|
|
| BLAKE2b-256 |
47e908ac608243838af2e22f40493befe97180787891826fecb0f7364fff2745
|