Migrating first request types to new HTTPClient

This commit is contained in:
Kumi 2022-04-20 12:02:59 +02:00
parent 06f83af3d8
commit 8b9488451e
Signed by: kumi
GPG key ID: 5D1CE6AF1805ECA2
4 changed files with 39 additions and 17 deletions

View file

@ -18,7 +18,7 @@ class Hafas:
"aid": "5vHavmuWPWIfetEe" "aid": "5vHavmuWPWIfetEe"
}, },
"client": { "client": {
"id": "OEBB", # OEBB "id": "OEBB",
"type": "WEB", "type": "WEB",
"name": "bahnapi.eu", # webapp "name": "bahnapi.eu", # webapp
"l": "oebb_py" # vs_webapp "l": "oebb_py" # vs_webapp

View file

@ -1,5 +1,7 @@
from urllib.request import Request, urlopen from urllib.request import Request, urlopen
from json import dumps
class HTTPClient: class HTTPClient:
def __init__(self, url, payload=None, headers=None): def __init__(self, url, payload=None, headers=None):
@ -13,9 +15,11 @@ class HTTPClient:
@data.setter @data.setter
def data(self, value): def data(self, value):
if isinstance(value, dict):
value = dumps(value)
if isinstance(value, str): if isinstance(value, str):
value = value.encode() value = value.encode()
self.request.data = value self.request.data = value
def execute(self): def execute(self):

View file

@ -1,6 +1,8 @@
import overpy import overpy
import math import math
import workers.val import workers.val
from classes.httpclient import HTTPClient from classes.httpclient import HTTPClient
@ -24,7 +26,10 @@ out;
endpoint = "https://overpass.kumi.systems/api/interpreter" 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) res = overpy.Overpass().parse_json(json)
return res.nodes return res.nodes

View file

@ -1,26 +1,39 @@
import json import json
import urllib.parse import urllib.parse
from classes import * from classes import Hafas, Station
def getValidator(name): def getValidator(name):
data = HTTPClient().get("http://scotty.oebb.at/bin/ajax-getstop.exe/dn?REQ0JourneyStopsS0A=255&REQ0JourneyStopsB=12&S='%s'?&js=true&" % hafas = Hafas()
urllib.parse.quote(name.encode("latin-1"))).text data = hafas.request("svcReqL", [
return "=".join(data.split("=")[1:]).split(";")[0] {
"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): def validateName(name):
stations = json.loads(getValidator(name)) stations = getValidator(name)
for station in stations["suggestions"]: for station in stations["svcResL"][0]["res"]["match"]["locL"]:
name = station["value"] name = station["name"]
sttype = station["type"] sttype = station["type"]
try: extid = station.get("extId")
extid = station["extId"] xcoord = station["crd"]["x"]
except: ycoord = station["crd"]["y"]
extid = None prodclass = station.get("pCls")
xcoord = station["xcoord"]
ycoord = station["ycoord"]
prodclass = station["prodClass"]
yield Station(name=name, sttype=sttype, extid=extid, xcoord=xcoord, ycoord=ycoord, prodclass=prodclass) yield Station(name=name, sttype=sttype, extid=extid, xcoord=xcoord, ycoord=ycoord, prodclass=prodclass)