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"
},
"client": {
"id": "OEBB", # OEBB
"id": "OEBB",
"type": "WEB",
"name": "bahnapi.eu", # webapp
"l": "oebb_py" # vs_webapp

View file

@ -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):

View file

@ -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

View file

@ -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)