PollenForecast to asynchronous

This commit is contained in:
April 2022-11-13 16:05:06 -07:00
parent f18b52bcf5
commit 68140eb0b2
No known key found for this signature in database
GPG Key ID: 17A9A017FAA4DE5E

View File

@ -6,6 +6,7 @@ import os
import shutil import shutil
import xml.dom.minidom import xml.dom.minidom
import logging, coloredlogs import logging, coloredlogs
import aiohttp, aiofiles
sys.path.append("./py2lib") sys.path.append("./py2lib")
sys.path.append("./Util") sys.path.append("./Util")
@ -31,14 +32,13 @@ l.debug(pollenIds, geocodes)
apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' apiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
def getData(pollenId, geocode): async def getData(pollenId, geocode):
fetchUrl = f"https://api.weather.com/v2/indices/pollen/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}" fetchUrl = f"https://api.weather.com/v2/indices/pollen/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}"
data = ""
#Fetch data #Fetch data
async with aiohttp.ClientSession() as s:
response = requests.get(fetchUrl) async with s.get(fetchUrl) as r:
data = await r.text()
data = response.text
newData = data[63:-26] newData = data[63:-26]
@ -46,32 +46,32 @@ def getData(pollenId, geocode):
#Write to .i2m file #Write to .i2m file
i2Doc = '<PollenForecast id="000000000" locationKey="' + str(pollenId) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(pollenId) + '</clientKey></PollenForecast>' i2Doc = '<PollenForecast id="000000000" locationKey="' + str(pollenId) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(pollenId) + '</clientKey></PollenForecast>'
f = open("./.temp/PollenForecast.i2m", "a") async with aiofiles.open("./.temp/PollenForecast.i2m", "a") as f:
f.write(i2Doc) await f.write(i2Doc)
f.close() await f.close()
def makeDataFile(): async def makeDataFile():
l.info("Writing a PollenForecast record.") l.info("Writing a PollenForecast record.")
header = '<Data type="PollenForecast">' header = '<Data type="PollenForecast">'
footer = '</Data>' footer = '</Data>'
with open("./.temp/PollenForecast.i2m", 'w') as doc: async with aiofiles.open("./.temp/PollenForecast.i2m", 'w') as doc:
doc.write(header) await doc.write(header)
for x, y in zip(pollenIds, geocodes): for x, y in zip(pollenIds, geocodes):
getData(x, y) await getData(x, y)
with open("./.temp/PollenForecast.i2m", 'a') as end: async with aiofiles.open("./.temp/PollenForecast.i2m", 'a') as end:
end.write(footer) await end.write(footer)
dom = xml.dom.minidom.parse("./.temp/PollenForecast.i2m") dom = xml.dom.minidom.parse("./.temp/PollenForecast.i2m")
pretty_xml_as_string = dom.toprettyxml(indent = " ") pretty_xml_as_string = dom.toprettyxml(indent = " ")
with open("./.temp/PollenForecast.i2m", "w") as g: async with aiofiles.open("./.temp/PollenForecast.i2m", "w") as g:
g.write(pretty_xml_as_string[23:]) await g.write(pretty_xml_as_string[23:])
g.close() await g.close()
files = [] files = []
commands = [] commands = []