from datetime import datetime from json import dumps from random import SystemRandom from string import ascii_lowercase, digits from classes import HTTPClient class Hafas: API_URL = "https://fahrplan.oebb.at/bin/mgate.exe?rnd=%i" BASE_CONTENT = { "id": None, "ver": "1.32", "lang": "deu", "auth": { "type": "AID", "aid": "5vHavmuWPWIfetEe" }, "client": { "id": "OEBB", "type": "WEB", "name": "bahnapi.eu", "l": "oebb_py" }, "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()