Skip to main content

Storstockholms Lokaltrafik (SL) data via Trafiklab API

Project description

Trafiklab-Sl

version python version license

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 two 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

from tsl.clients.stoplookup import StopLookupClient
from tsl.clients.transport import TransportClient
from tsl.models.common import TransportMode


async def main():
    # perform stop lookup to get the site id for Stockholm Central
    lookup_client = StopLookupClient("your-SL-Platsuppslag-api-key")
    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()
    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


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-0.3.0.tar.gz (9.6 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-0.3.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: trafiklab_sl-0.3.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for trafiklab_sl-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c416cc8d95ff6e22a110772ccc626a24afbab54e21f851a0c3b670b1e1d6d07a
MD5 47a66c31944a0278dd585ae76b3c1991
BLAKE2b-256 7ba6733f31cfca6165c40ad5bd77ceba8bfbc599ecee31301229ae0bec6973b0

See more details on using hashes here.

File details

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

File metadata

  • Download URL: trafiklab_sl-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for trafiklab_sl-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c526334503659659ace118f9bbdada9ecbc2eb315053ff9ebbfb0f3afaef6c77
MD5 728133c29f2345af8dc6a024f53fb964
BLAKE2b-256 07da678605c91c23a3f1f239ed6e55b6025c87635c66db074bbef0e1b7b0512e

See more details on using hashes here.

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