From 8b9488451e95b27c9ac180df1e4eeb25ff7ce952 Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 20 Apr 2022 12:02:59 +0200 Subject: [PATCH] Migrating first request types to new HTTPClient --- classes/hafas.py | 2 +- classes/httpclient.py | 6 +++++- workers/closest.py | 7 ++++++- workers/val.py | 41 +++++++++++++++++++++++++++-------------- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/classes/hafas.py b/classes/hafas.py index 3f0631c..7db92e0 100644 --- a/classes/hafas.py +++ b/classes/hafas.py @@ -18,7 +18,7 @@ class Hafas: "aid": "5vHavmuWPWIfetEe" }, "client": { - "id": "OEBB", # OEBB + "id": "OEBB", "type": "WEB", "name": "bahnapi.eu", # webapp "l": "oebb_py" # vs_webapp diff --git a/classes/httpclient.py b/classes/httpclient.py index beede3d..03b5e26 100644 --- a/classes/httpclient.py +++ b/classes/httpclient.py @@ -1,5 +1,7 @@ from urllib.request import Request, urlopen +from json import dumps + class HTTPClient: def __init__(self, url, payload=None, headers=None): @@ -13,9 +15,11 @@ class HTTPClient: @data.setter def data(self, value): + if isinstance(value, dict): + value = dumps(value) if isinstance(value, str): value = value.encode() - + self.request.data = value def execute(self): diff --git a/workers/closest.py b/workers/closest.py index d1301ca..697b693 100644 --- a/workers/closest.py +++ b/workers/closest.py @@ -1,6 +1,8 @@ import overpy import math + import workers.val + from classes.httpclient import HTTPClient @@ -24,7 +26,10 @@ out; endpoint = "https://overpass.kumi.systems/api/interpreter" - json = HTTPClient().post(endpoint, data={"data": query}).text + http = HTTPClient(endpoint) + http.data = f"data={query}" + + json = http.execute() res = overpy.Overpass().parse_json(json) return res.nodes diff --git a/workers/val.py b/workers/val.py index 97157e4..f2ceea0 100644 --- a/workers/val.py +++ b/workers/val.py @@ -1,26 +1,39 @@ import json import urllib.parse -from classes import * +from classes import Hafas, Station def getValidator(name): - data = HTTPClient().get("http://scotty.oebb.at/bin/ajax-getstop.exe/dn?REQ0JourneyStopsS0A=255&REQ0JourneyStopsB=12&S='%s'?&js=true&" % - urllib.parse.quote(name.encode("latin-1"))).text - return "=".join(data.split("=")[1:]).split(";")[0] + hafas = Hafas() + data = hafas.request("svcReqL", [ + { + "req": { + "input": { + "field": "S", + "loc": { + "name": f"{name}?", + "type": "ALL", + "dist": 1000 + }, + "maxLoc": 7 + } + }, + "meth": "LocMatch", + "id": "1|1|" + } + ]) + return json.loads(data) def validateName(name): - stations = json.loads(getValidator(name)) - for station in stations["suggestions"]: - name = station["value"] + stations = getValidator(name) + for station in stations["svcResL"][0]["res"]["match"]["locL"]: + name = station["name"] sttype = station["type"] - try: - extid = station["extId"] - except: - extid = None - xcoord = station["xcoord"] - ycoord = station["ycoord"] - prodclass = station["prodClass"] + extid = station.get("extId") + xcoord = station["crd"]["x"] + ycoord = station["crd"]["y"] + prodclass = station.get("pCls") yield Station(name=name, sttype=sttype, extid=extid, xcoord=xcoord, ycoord=ycoord, prodclass=prodclass)