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:

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.transport import TransportClient
from tsl.clients.journey import JourneyPlannerClient, SearchLeg
from tsl.models.common import TransportMode
from tsl.utils import global_id_to_site_id
from tsl.tools.journey import SimpleJourneyInterpreter, leg_display_str


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

        # perform stop lookup to get the site id for Stockholm Central
        journey_client = JourneyPlannerClient(session)
        stops = await journey_client.find_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)

        # get step by step journey from Stockholm Central to a specific location
        params = journey_client.build_request_params(
            origin=SearchLeg.from_stop_finder(central_station),
            destination=SearchLeg.from_coordinates("59.274695", "18.033901"),
            calc_number_of_trips=1
        )
        ways_to_get_there = await journey_client.search_trip(params)
        # use `SimpleJourneyInterpreter` to interpret the journey data in a human-readable way

    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']})"
        )

    print(f"\nNumber of ways to get from Stockholm Central to 59.274695, 18.033901 - {len(ways_to_get_there)}")
    interpreter = SimpleJourneyInterpreter(ways_to_get_there[0])

    print("Itinerary of the first way:")
    for leg in interpreter.get_itinerary():
        print("  - " + leg_display_str(leg))


asyncio.run(main())

Contributing

Both bug reports and pull requests are appreciated.

Development

pre-commit hooks

To help ensure code style and import order are consistent across the project, this project uses pre-commit to automatically check and format code before it reaches the repository.

This project uses pre-commit to automatically check and format code before it reaches the repository.

To set up pre-commit hooks locally:

pip install pre-commit
pre-commit install

After this, every time you commit, formatting tools will be run automatically on your code.

You can also run all hooks manually on all files with:

pre-commit run --all-files

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.2.0.tar.gz (35.8 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.2.0-py3-none-any.whl (33.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for trafiklab_sl-2.2.0.tar.gz
Algorithm Hash digest
SHA256 eecf0e84d000a792bc29dfb83dc4668e51b22e5086596b73324f5708eb989169
MD5 3508da78c954db1baeafcfce351d0c36
BLAKE2b-256 c234de871057cc965ad571d5466114983530744b26f25c5e9c46f45c3ed606a5

See more details on using hashes here.

Provenance

The following attestation bundles were made for trafiklab_sl-2.2.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.2.0-py3-none-any.whl.

File metadata

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

File hashes

Hashes for trafiklab_sl-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d90d1a9a562177095a679f12ac01c73b4e86ba0b98e29fb5a0f2dd3e02c54692
MD5 247c3fa8e04b5230238bfcdd7cc55f55
BLAKE2b-256 1c15dd88f25556680c6253c65286f3609d18750a1cf0e1f7ff1275718c793c8b

See more details on using hashes here.

Provenance

The following attestation bundles were made for trafiklab_sl-2.2.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