Skip to main content

Storstockholms Lokaltrafik (SL) data via Trafiklab API

Project description

Trafiklab-Sl

version python version test license

A data model describing 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:

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
from tsl.utils import global_id_to_site_id


async def main():
    async with aiohttp.ClientSession() as session:

        # perform stop lookup to get the site id for Stockholm Central
        lookup_client = StopLookupClient(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 = global_id_to_site_id(central_station['id'])

        # 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['disassembledName']}:")
    for departure in sorted(reponse["departures"], key=lambda d: d.get("expected", "")):
        print(
            f"[{departure['line'].get('designation')}] platform {departure['stop_point'].get('designation')}"
            f" to {departure.get('destination')} ({departure['display']})"
        )


asyncio.run(main())

Contributing

Both bug reports and pull requests are appreciated.

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

trafiklab_sl-2.0.0.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

trafiklab_sl-2.0.0-py3-none-any.whl (13.0 kB view details)

Uploaded Python 3

File details

Details for the file trafiklab_sl-2.0.0.tar.gz.

File metadata

  • Download URL: trafiklab_sl-2.0.0.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for trafiklab_sl-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0defcc6bb6974231d1d250e9dacd75a76afe423bfe3afe2bf0d759929900ed1f
MD5 539848d62df0392e156aea52adbe7c1a
BLAKE2b-256 e5574a0bb3ce6d4deba53dbc702c39087ff241b359fd4148668ecae2c66cded3

See more details on using hashes here.

Provenance

The following attestation bundles were made for trafiklab_sl-2.0.0.tar.gz:

Publisher: publish.yml on NecroKote/trafiklab-sl

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file trafiklab_sl-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: trafiklab_sl-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 13.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for trafiklab_sl-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bd993206f5a569fefd2265295beff06a1fe875ac7b3c615ed560688722baea25
MD5 4a416d9e83105002086ae01d68cf7947
BLAKE2b-256 1aa1f2e0d13f96a1215e25f0c3666b25cdad89bd8d6df05cdfda660b6fddf929

See more details on using hashes here.

Provenance

The following attestation bundles were made for trafiklab_sl-2.0.0-py3-none-any.whl:

Publisher: publish.yml on NecroKote/trafiklab-sl

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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