oebb_py/classes/hafas.py

59 lines
1.5 KiB
Python
Raw Permalink Normal View History

2022-04-19 12:58:44 +00:00
from datetime import datetime
from json import dumps
from random import SystemRandom
from string import ascii_lowercase, digits
from classes import HTTPClient
2022-04-19 12:58:44 +00:00
class Hafas:
API_URL = "https://fahrplan.oebb.at/bin/mgate.exe?rnd=%i"
BASE_CONTENT = {
2022-04-20 10:23:47 +00:00
"id": None,
2022-04-19 12:58:44 +00:00
"ver": "1.32",
"lang": "deu",
"auth": {
"type": "AID",
"aid": "5vHavmuWPWIfetEe"
},
"client": {
"id": "OEBB",
2022-04-19 12:58:44 +00:00
"type": "WEB",
2022-04-20 10:23:47 +00:00
"name": "bahnapi.eu",
"l": "oebb_py"
2022-04-19 12:58:44 +00:00
},
"formatted": False,
"ext": "OEBB.11",
}
def __init__(self):
self.timestamp = int(datetime.now().timestamp() * 1000)
@classmethod
def generateID(self, len=16, chars=ascii_lowercase + digits):
r = SystemRandom()
return "".join([r.choice(chars) for _ in range(len)])
def buildRequest(self, request_type, request_data, **kwargs):
client = HTTPClient(self.API_URL % int(
datetime.now().timestamp() * 1000))
data = self.BASE_CONTENT
data[request_type] = request_data
if "language" in kwargs.items():
data["lang"] = kwargs["language"]
if "id" in kwargs.items():
data["id"] = kwargs["id"]
if not data["id"]:
data["id"] = self.__class__.generateID()
client.data = dumps(data)
return client
def request(self, request_type, request_data):
client = self.buildRequest(request_type, request_data)
return client.execute()