Git Product home page Git Product logo

onboardapis's Introduction

onboardapis

Python versions PyPI version License
Build package Deploy documentation

Warning: Version 2.0.0 introduces breaking changes! Existing code will definitely not work anymore.

Description

onboardapis allows you to interact with different on-board APIs. You can connect to the Wi-Fi of a supported transportation provider and access information about your journey, the vehicle you are travelling in and much more.

Note: The only vehicle type covered by this package currently is train. See the supported APIs for more information.


Installation

Install the latest stable version of onboardapis from PyPI using pip:

$ python -m pip install onboardapis

Version

or the latest prerelease version of onboardapis from PyPI using pip:

$ python -m pip install --pre onboardapis

Version


Install the latest development version of onboardapis from GitHub using pip:

$ python -m pip install git+https://github.com/felix-zenk/onboardapis.git

Quickstart

To begin with development you will need to know a few things first:

  • What vehicle type you want to use
  • Who operates the vehicle
  • What country is the operator from

With this information you can get the needed module from the package onboardapis.<type>.<country>.<operator> and import the API class from it. For more specific information on finding the API you are looking for, see Finding your API.

Example: Let's say you want to use the on-board API called ICE Portal of Deutsche Bahn trains in Germany:

from onboardapis.train.de.db import ICEPortal

Every vehicle has an init-method that needs to be called to initialize the connection to the API. When using a vehicle as a context manager the init-method will automatically be called.

from onboardapis.train.de.db import ICEPortal
from onboardapis.units import kilometers, kmh


# init is automatically called
with ICEPortal() as train:
    print(
        f"Distance to {train.current_station.name}:",
        f"{kilometers(meters=train.calculate_distance(train.current_station)):.1f} km"
    )


# init has to be explicitly called
train = ICEPortal()
train.init()  # Explicit call to init method to initialize API connection

print(
    f"Travelling at {train.speed} m/s",
    f"({kmh(ms=train.speed):.2f} km/h)",
    f"with a delay of {train.delay.total_seconds():.0f} seconds"
)

And there you go!
You can read more information about available attributes in the onboardapis.train.Train and onboardapis.mixins documentation and the respective train's documentation.

Note: As you may have noticed by now, the package always returns datetime or timedelta objects for time based values and other values like distances, velocity, etc. in SI units, so you have to convert to other units if you want to use values different from the SI units. For convenience the onboardapis.units module provides functions to convert between units.

The name of a conversion function is the unit which will be the result of the conversion. Different units can be passed to a conversion function as keywords. Keywords can be combined to return the sum of the input units.


Documentation

GitHub-Pages

Access the documentation on GitHub-Pages.

Supported APIs

API API scope Type Country Operator
RailnetRegio geo train at (Austria) obb (Österreichische Bundesbahnen)
ICEPortal full, internet-access* train de (Germany) db (Deutsche Bahn / DB AG)
FlixTainment geo train de (Germany) flix (Flix Train GmbH)
MetronomCaptivePortal internet-access* train de (Germany) me (metronom Eisenbahngesellschaft mbH)

Notes:

  • * Managing internet access is not yet supported.

Experimental APIs

API API scope Type Country Operator
PortalINOUI full train fr (France) sncf (SNCF Voyageurs)
ZugPortal full train de (Germany) db (Deutsche Bahn / DB AG)
PortaleRegionale journey-simple train it (Italy) ti (Trenitalia S.p.A.)
SBahnHannover journey-simple, internet-access* train de (Germany) tdh (Transdev Hannover GmbH)

Notes:

  • * Managing internet access is not yet supported.

APIs in development

API API scope Type Country Operator
...

Find your API

1. Determine vehicle type: train, plane, bus, ship, other.
2. Find the ISO 3166-2 country code of the operators country
3. Operator code

The operator code is vehicle type specific. The following IDs are used:

Vehicle type Region Register
plane global ICAO
train europe VKM

Combine these three values to onboardapis.<type>.<country>.<operator>. This is the module that contains the API.

Hint: You can also get the module path by looking at Supported APIs / Experimental APIs and taking the three values from there.

API scope

The API scope defines what information can be accessed through the API and is a general indicator of the API's capabilities.
The currently possible API scopes are:

  • basic: Only basic information is available such as connection status to the API.
  • vehicle: The API supplies information about the vehicle such as the train ID, line number, etc.
  • geo: The API supplies information about the current location, speed, etc. of the vehicle.
  • journey-simple: The API supplies simple journey information including the current station and the destination station.
  • journey: The API supplies detailed journey information including all the stations and possibly connecting services.
  • full: All of the above.
  • internet-access: The API provides internet access to the user.

onboardapis's People

Contributors

felix-zenk avatar totto16 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

totto16

onboardapis's Issues

DB BAP service status API regularly fails with 503 Service unavailable

Hi there,

it seems to me the DB BAP service status API quite regularly fails with HTTP 503. With ICEPortal().init() calling the BAP API this exception leaves all other APIs unusable as well. Have you seen this issue, too? I will attach the exact exception below.

Thanks and best regards

WARNING:root:Request to https://iceportal.de/bap/api/bap-service-status returned status code 503
Traceback (most recent call last):
  File "onboard\.venv\Lib\site-packages\requests\models.py", line 971, in json
    return complexjson.loads(self.text, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "AppData\Local\Programs\Python\Python311\Lib\json\__init__.py", line 346, in loads
    return _default_decoder.decode(s)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "AppData\Local\Programs\Python\Python311\Lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "onboard\.venv\Lib\site-packages\onboardapis\utils\data.py", line 459, in _get
    return super(JSONDataConnector, self)._get(endpoint, *args, base_url=base_url, **kwargs).json()
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "onboard\.venv\Lib\site-packages\requests\models.py", line 975, in json
    raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "onboard\.venv\Lib\site-packages\onboardapis\trains\__init__.py", line 251, in init
    self._static_data.refresh()
  File "onboard\.venv\Lib\site-packages\onboardapis\trains\germany\db.py", line 31, in refresh
    self._get("/bap/api/bap-service-status")
  File "onboard\.venv\Lib\site-packages\onboardapis\utils\data.py", line 462, in _get
    raise DataInvalidError() from e
onboardapis.exceptions.DataInvalidError: The API returned an invalid response

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "onboard\status.py", line 5, in <module>
    train.init()  # Explicit call to init method to initialize API connection
    ^^^^^^^^^^^^
  File "onboard\.venv\Lib\site-packages\onboardapis\trains\__init__.py", line 254, in init
    raise InitialConnectionError() from e
onboardapis.exceptions.InitialConnectionError: Unable to connect to the API, are you connected to the on-board WI-FI?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.