mirror of
https://github.com/mewtek/i2ME-Legacy.git
synced 2025-05-11 17:00:23 -05:00
AirQuality to asynchronous
This commit is contained in:
parent
e2554e11b9
commit
1f116850ce
@ -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
|
||||||
|
|
||||||
l = logging.getLogger(__name__)
|
l = logging.getLogger(__name__)
|
||||||
coloredlogs.install()
|
coloredlogs.install()
|
||||||
@ -27,22 +28,24 @@ for i in MPC.getPrimaryLocations():
|
|||||||
|
|
||||||
apiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
|
apiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
|
||||||
|
|
||||||
def getData(epaId, zipcode):
|
async def getData(epaId, zipcode):
|
||||||
url = f"https://api.weather.com/v1/location/{zipcode}:4:US/airquality.xml?language=en-US&apiKey={apiKey}"
|
url = f"https://api.weather.com/v1/location/{zipcode}:4:US/airquality.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 = res.text
|
data = await r.text()
|
||||||
|
|
||||||
newData = data[57:-11]
|
newData = data[57:-11]
|
||||||
|
|
||||||
# Write to i2doc file
|
# Write to i2doc file
|
||||||
i2Doc = f'<AirQuality id="000000000" locationKey="{epaId}" isWxScan="0">' + '' + newData + f'<clientKey>{epaId}</clientKey></AirQuality>'
|
i2Doc = f'<AirQuality id="000000000" locationKey="{epaId}" isWxScan="0">' + '' + newData + f'<clientKey>{epaId}</clientKey></AirQuality>'
|
||||||
|
|
||||||
f = open("./.temp/AirQuality.i2m", 'a')
|
async with aiofiles.open("./.temp/AirQuality.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
|
||||||
workingEpaIds = []
|
workingEpaIds = []
|
||||||
|
|
||||||
@ -62,21 +65,21 @@ def writeData():
|
|||||||
header = '<Data type="AirQuality">'
|
header = '<Data type="AirQuality">'
|
||||||
footer = "</Data>"
|
footer = "</Data>"
|
||||||
|
|
||||||
with open("./.temp/AirQuality.i2m", 'w') as doc:
|
async with aiofiles.open("./.temp/AirQuality.i2m", 'w') as doc:
|
||||||
doc.write(header)
|
await doc.write(header)
|
||||||
|
|
||||||
for (x, y) in zip(workingEpaIds, zipCodes):
|
for (x, y) in zip(workingEpaIds, zipCodes):
|
||||||
getData(x, y)
|
await getData(x, y)
|
||||||
|
|
||||||
with open("./.temp/AirQuality.i2m", 'a') as end:
|
async with aiofiles.open("./.temp/AirQuality.i2m", 'a') as end:
|
||||||
end.write(footer)
|
await end.write(footer)
|
||||||
|
|
||||||
dom = xml.dom.minidom.parse("./.temp/AirQuality.i2m")
|
dom = xml.dom.minidom.parse("./.temp/AirQuality.i2m")
|
||||||
xmlPretty = dom.toprettyxml(indent = " ")
|
xmlPretty = dom.toprettyxml(indent = " ")
|
||||||
|
|
||||||
with open("./.temp/AirQuality.i2m", 'w') as g:
|
async with aiofiles.open("./.temp/AirQuality.i2m", 'w') as g:
|
||||||
g.write(xmlPretty[23:])
|
await g.write(xmlPretty[23:])
|
||||||
g.close()
|
await g.close()
|
||||||
|
|
||||||
files = []
|
files = []
|
||||||
commands = []
|
commands = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user