From 68140eb0b2d89cbfdbfe348417ecc384c8b746d5 Mon Sep 17 00:00:00 2001 From: April Date: Sun, 13 Nov 2022 16:05:06 -0700 Subject: [PATCH] PollenForecast to asynchronous --- recordGenerators/PollenForecast.py | 36 +++++++++++++++--------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/recordGenerators/PollenForecast.py b/recordGenerators/PollenForecast.py index 11a8eed..7c3f475 100644 --- a/recordGenerators/PollenForecast.py +++ b/recordGenerators/PollenForecast.py @@ -6,6 +6,7 @@ import os import shutil import xml.dom.minidom import logging, coloredlogs +import aiohttp, aiofiles sys.path.append("./py2lib") sys.path.append("./Util") @@ -31,14 +32,13 @@ l.debug(pollenIds, geocodes) 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}" - + data = "" #Fetch data - - response = requests.get(fetchUrl) - - data = response.text + async with aiohttp.ClientSession() as s: + async with s.get(fetchUrl) as r: + data = await r.text() newData = data[63:-26] @@ -46,32 +46,32 @@ def getData(pollenId, geocode): #Write to .i2m file i2Doc = '' + '' + newData + '' + str(pollenId) + '' - f = open("./.temp/PollenForecast.i2m", "a") - f.write(i2Doc) - f.close() + async with aiofiles.open("./.temp/PollenForecast.i2m", "a") as f: + await f.write(i2Doc) + await f.close() -def makeDataFile(): +async def makeDataFile(): l.info("Writing a PollenForecast record.") header = '' footer = '' - with open("./.temp/PollenForecast.i2m", 'w') as doc: - doc.write(header) + async with aiofiles.open("./.temp/PollenForecast.i2m", 'w') as doc: + await doc.write(header) for x, y in zip(pollenIds, geocodes): - getData(x, y) + await getData(x, y) - with open("./.temp/PollenForecast.i2m", 'a') as end: - end.write(footer) + async with aiofiles.open("./.temp/PollenForecast.i2m", 'a') as end: + await end.write(footer) dom = xml.dom.minidom.parse("./.temp/PollenForecast.i2m") pretty_xml_as_string = dom.toprettyxml(indent = " ") - with open("./.temp/PollenForecast.i2m", "w") as g: - g.write(pretty_xml_as_string[23:]) - g.close() + async with aiofiles.open("./.temp/PollenForecast.i2m", "w") as g: + await g.write(pretty_xml_as_string[23:]) + await g.close() files = [] commands = []