Breathing to asynchronous

This commit is contained in:
April 2022-11-13 15:49:25 -07:00
parent a9aa0174d4
commit e2554e11b9
No known key found for this signature in database
GPG Key ID: 17A9A017FAA4DE5E

View File

@ -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")
@ -30,14 +31,14 @@ l.debug(coopIds, geocodes)
apiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
def getData(coopId, geocode):
async def getData(coopId, geocode):
fetchUrl = f"https://api.weather.com/v2/indices/breathing/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]
@ -45,32 +46,32 @@ def getData(coopId, geocode):
#Write to .i2m file
i2Doc = '<Breathing id="000000000" locationKey="' + str(coopId) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(coopId) + '</clientKey></Breathing>'
f = open("./.temp/Breathing.i2m", "a")
f.write(i2Doc)
f.close()
async with aiofiles.open("./.temp/Breathing.i2m", "a") as f:
await f.write(i2Doc)
await f.close()
def makeDataFile():
async def makeDataFile():
l.info("Writing a Breathing forecast record.")
header = '<Data type="Breathing">'
footer = '</Data>'
with open("./.temp/Breathing.i2m", 'w') as doc:
doc.write(header)
async with aiofiles.open("./.temp/Breathing.i2m", 'w') as doc:
await doc.write(header)
for x, y in zip(coopIds, geocodes):
getData(x, y)
await getData(x, y)
with open("./.temp/Breathing.i2m", 'a') as end:
end.write(footer)
async with aiofiles.open("./.temp/Breathing.i2m", 'a') as end:
await end.write(footer)
dom = xml.dom.minidom.parse("./.temp/Breathing.i2m")
pretty_xml_as_string = dom.toprettyxml(indent = " ")
with open("./.temp/Breathing.i2m", "w") as g:
g.write(pretty_xml_as_string[23:])
g.close()
async with aiofiles.open("./.temp/Breathing.i2m", "w") as g:
await g.write(pretty_xml_as_string[23:])
await g.close()
files = []
commands = []