File: example.py

package info (click to toggle)
python-opendata-transport 0.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 112 kB
  • sloc: python: 320; makefile: 7
file content (62 lines) | stat: -rw-r--r-- 1,859 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Example to get the details for a connection or a station."""
import asyncio

import aiohttp

from opendata_transport import OpendataTransport
from opendata_transport import OpendataTransportStationboard
from opendata_transport import OpendataTransportLocation


async def main():
    """Example for getting the data."""
    async with aiohttp.ClientSession() as session:
        # Search a station by query
        locations = OpendataTransportLocation(session, query="Stettb")
        await locations.async_get_data()

        # Print the locations data
        print(locations.locations)

        # Print as list
        print(list(map(lambda x: x["name"], locations.locations)))

        # Search a station by coordinates
        locations = OpendataTransportLocation(session, x=47.2, y=8.7)
        await locations.async_get_data()

        # Print the locations data
        print(locations.locations)

        # Print as list
        print(list(map(lambda x: x["name"], locations.locations)))

        print()

        # Get the connection for a defined route
        connection = OpendataTransport(
            "Zürich, Blumenfeldstrasse", "Zürich Oerlikon, Bahnhof", session, 4
        )
        await connection.async_get_data()

        # Print the start and the destination name
        print("Train connections:", connection.from_name, "->", connection.to_name)

        # Print the next three connections
        print(connection.connections)

        # Print the details of the next connection
        print(connection.connections[0])

        print()

        # Get all connections of a station
        stationboard = OpendataTransportStationboard("8591355", session, 4)
        await stationboard.async_get_data()

        # Print the journey data
        print(stationboard.journeys)


if __name__ == "__main__":
    asyncio.run(main())