AirportDelays to asynchronous

This commit is contained in:
April 2022-11-13 15:56:01 -07:00
parent 1f116850ce
commit 704c33201f
No known key found for this signature in database
GPG Key ID: 17A9A017FAA4DE5E

View File

@ -4,6 +4,7 @@ import os
import shutil import shutil
import xml.dom.minidom import xml.dom.minidom
import logging,coloredlogs import logging,coloredlogs
import aiohttp, aiofiles
import sys import sys
sys.path.append("./py2lib") sys.path.append("./py2lib")
@ -29,56 +30,56 @@ l.debug(airports)
apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' apiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
def getData(airport): async def getData(airport):
url = f"https://api.weather.com/v1/airportcode/{airport}/airport/delays.xml?language=en-US&apiKey={apiKey}" url = f"https://api.weather.com/v1/airportcode/{airport}/airport/delays.xml?language=en-US&apiKey={apiKey}"
data = ""
res = requests.get(url=url) async with aiohttp.ClientSession() as s:
async with s.get(url) as r:
data = await r.text()
data = res.text
newData = data[48:-11] newData = data[48:-11]
# Write to i2doc file # Write to i2doc file
i2Doc = f'<AirportDelays id="000000000" locationKey="{airport}" isWxScan="0">' + '' + newData + f'<clientKey>{airport}</clientKey></AirportDelays>' i2Doc = f'<AirportDelays id="000000000" locationKey="{airport}" isWxScan="0">' + '' + newData + f'<clientKey>{airport}</clientKey></AirportDelays>'
f = open("./.temp/AirportDelays.i2m", 'a') async with aiofiles.open("./.temp/AirportDelays.i2m", 'a') as f:
f.write(i2Doc) await f.write(i2Doc)
f.close() await f.close()
def writeData(): async def writeData():
useData = False useData = False
airportsWithDelays = [] airportsWithDelays = []
for x in airports: for x in airports:
# Do a quick check to see if the airport in question has a delay or not async with aiohttp.ClientSession() as s:
res = requests.get(f"https://api.weather.com/v1/airportcode/{x}/airport/delays.xml?language=en-US&apiKey={apiKey}") async with s.get(f"https://api.weather.com/v1/airportcode/{x}/airport/delays.xml?language=en-US&apiKey={apiKey}") as r:
if r.status != 200:
if (res.status_code != 200): l.debug(f"No delay for {x} found, skipping..")
l.debug(f"[AIRPORT DELAYS] No delays for {x} found, skipping..") else:
else: airportsWithDelays.append(x)
airportsWithDelays.append(x) useData = True
l.debug(f"[AIRPORT DELAYS] {x} has a delay! Writing a file..")
useData = True
if (useData): if (useData):
l.info("Writing an AirportDelays record.") l.info("Writing an AirportDelays record.")
header = '<Data type="AirportDelays">' header = '<Data type="AirportDelays">'
footer = "</Data>" footer = "</Data>"
with open("./.temp/AirportDelays.i2m", 'w') as doc: async with aiofiles.open("./.temp/AirportDelays.i2m", 'w') as doc:
doc.write(header) await doc.write(header)
for x in airportsWithDelays: for x in airportsWithDelays:
getData(x) await getData(x)
with open("./.temp/AirportDelays.i2m", 'a') as end: async with aiofiles.open("./.temp/AirportDelays.i2m", 'a') as end:
end.write(footer) await end.write(footer)
dom = xml.dom.minidom.parse("./.temp/AirportDelays.i2m") dom = xml.dom.minidom.parse("./.temp/AirportDelays.i2m")
prettyXml = dom.toprettyxml(indent=" ") prettyXml = dom.toprettyxml(indent=" ")
with open("./.temp/AirportDelays.i2m", 'w') as g: async with aiofiles.open("./.temp/AirportDelays.i2m", 'w') as g:
g.write(prettyXml) await g.write(prettyXml)
g.close() await g.close()
files = [] files = []
commands = [] commands = []