Commit ef37c39f authored by vurpo's avatar vurpo

Filter radat before requesting km from all of them

parent fece55bf
......@@ -76,17 +76,31 @@ async def fetch(url, session):
async with session.get(url) as response:
return await response.read()
def kmOnRoad(ranges, km: int):
km = int(km)
if len(ranges) == 0:
return False
if type(ranges[0]) == int and len(ranges) == 1:
return km == ranges[0]
elif type(ranges[0]) == int and len(ranges) > 1:
return km in range(ranges[0],ranges[1]+1)
elif type(ranges[0]) == list:
return True in [kmOnRoad(r, km) for r in ranges]
return False
async def kilometri(km):
url = "https://rata.digitraffic.fi/infra-api/0.2/radat/{}/{}.json?srsName=crs:84"
tasks = {}
ratanrot = [ratanro for ratanro in radat if kmOnRoad(radat[ratanro]['rata']['ratakilometrit'], km.split('+')[0])]
async with ClientSession() as session:
for ratanumero in radat.keys():
for ratanumero in ratanrot:
task = asyncio.ensure_future(session.get(url.format(ratanumero, km)))
tasks[ratanumero] = task
results = await asyncio.gather(*tasks.values())
responses = dict(zip(tasks.keys(), results))
print(responses)
responses = {ratanumero:(await km.json()) for (ratanumero, km) in responses.items() if km.status == 200}
# WORK AROUND BUG IN DIGITRAFFIC: crs:84 HAS NO EFFECT IN THIS SPECIFIC CASE
......@@ -122,7 +136,7 @@ async def rata_json(request):
return web.Response(content_type='application/json', text=json.dumps({"error":"Not found"}, status=404))
@routes.get('/liikennepaikka/{lyhenne}.json')
async def liikennepaikka_html(request):
async def liikennepaikka_json(request):
if request.match_info['lyhenne'] in rliikennepaikat:
return web.Response(content_type='application/json', text=json.dumps(rliikennepaikat[request.match_info['lyhenne']]))
else:
......@@ -242,7 +256,7 @@ async def liikennepaikka_html(request):
return web.Response(text="Not found", status=404)
@routes.get('/liikennepaikat/{tyyppi}')
async def liikennepaikat_tyyppi(request):
async def liikennepaikat_tyyppi_html(request):
if request.match_info['tyyppi'] in rlptyypit:
return web.Response(
content_type='text/html',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment