From f1c633febc296906fdc82b1bd7d5f551c685d7ee Mon Sep 17 00:00:00 2001 From: April Date: Sun, 13 Nov 2022 16:00:38 -0700 Subject: [PATCH] AchesAndPains to asynchronous --- recordGenerators/AchesAndPains.py | 43 +++++++++++++++++-------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/recordGenerators/AchesAndPains.py b/recordGenerators/AchesAndPains.py index 6631ca1..e434125 100644 --- a/recordGenerators/AchesAndPains.py +++ b/recordGenerators/AchesAndPains.py @@ -7,6 +7,7 @@ import records.LFRecord as LFR import gzip from os import remove import xml.dom.minidom +import aiohttp, aiofiles l = logging.getLogger(__name__) coloredlogs.install() @@ -20,46 +21,48 @@ for i in MPC.getPrimaryLocations(): apiKey = "21d8a80b3d6b444998a80b3d6b1449d3" -def getData(coopId, geocode): +async def getData(coopId, geocode): fetchUrl = f"https://api.weather.com/v2/indices/achePain/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}" + data = "" + + async with aiohttp.ClientSession() as s: + async with s.get(fetchUrl) as r: + if r.status != 200: + l.error(f"Failed to write AchesAndPains record -- status code {r.status}") + return + + data = await r.text() - res = requests.get(fetchUrl) - if res.status_code != 200: - l.error("DO NOT REPORT THE ERROR BELOW") - l.error(f"Failed to write AchesAndPains record -- Status code {res.status_code}") - return - - data = res.text newData = data[63:-26] i2Doc = f'\n \n {newData}\n {coopId}\n ' - f = open('./.temp/AchesAndPains.i2m', 'a') - f.write(i2Doc) - f.close() + async with aiofiles.open('./.temp/AchesAndPains.i2m', 'a') as f: + await f.write(i2Doc) + await f.close() -def makeRecord(): +async def makeRecord(): l.info("Writing AchesAndPains record.") header = '' footer = '' - with open('./.temp/AchesAndPains.i2m', 'a') as doc: - doc.write(header) + async with aiofiles.open('./.temp/AchesAndPains.i2m', 'a') as doc: + await doc.write(header) for (x, y) in zip(coopIds, geocodes): - getData(x,y) + await getData(x,y) - with open('./.temp/AchesAndPains.i2m', 'a') as end: - end.write(footer) + async with aiofiles.open('./.temp/AchesAndPains.i2m', 'a') as end: + await end.write(footer) dom = xml.dom.minidom.parse('./.temp/AchesAndPains.i2m') xmlPretty = dom.toprettyxml(indent= " ") - with open('./.temp/AchesAndPains.i2m', 'w') as g: - g.write(xmlPretty[23:]) - g.close() + async with aiofiles.open('./.temp/AchesAndPains.i2m', 'w') as g: + await g.write(xmlPretty[23:]) + await g.close() # Compresss i2m to gzip