mirror of
https://github.com/mewtek/i2ME-Legacy.git
synced 2025-05-11 17:00:23 -05:00
HourlyForecast to asynchronous
This commit is contained in:
parent
73dbba3a65
commit
900cad0b9b
@ -5,6 +5,7 @@ import os
|
|||||||
import shutil
|
import shutil
|
||||||
import xml.dom.minidom
|
import xml.dom.minidom
|
||||||
import logging,coloredlogs
|
import logging,coloredlogs
|
||||||
|
import aiofiles, aiohttp
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
sys.path.append("./py2lib")
|
sys.path.append("./py2lib")
|
||||||
@ -32,46 +33,47 @@ for i in MPC.getMetroCities():
|
|||||||
|
|
||||||
apiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
|
apiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
|
||||||
|
|
||||||
def getData(tecci, zipCode):
|
async def getData(tecci, zipCode):
|
||||||
|
l.debug('Gathering data for location id ' + tecci)
|
||||||
fetchUrl = 'https://api.weather.com/v1/location/' + zipCode + ':4:US/forecast/hourly/360hour.xml?language=en-US&units=e&apiKey=' + apiKey
|
fetchUrl = 'https://api.weather.com/v1/location/' + zipCode + ':4:US/forecast/hourly/360hour.xml?language=en-US&units=e&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[48:-11]
|
newData = data[48:-11]
|
||||||
|
|
||||||
l.debug('Gathering data for location id ' + tecci)
|
|
||||||
#Write to .i2m file
|
#Write to .i2m file
|
||||||
i2Doc = '<HourlyForecast id="000000000" locationKey="' + str(tecci) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(tecci) + '</clientKey></HourlyForecast>'
|
i2Doc = '<HourlyForecast id="000000000" locationKey="' + str(tecci) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(tecci) + '</clientKey></HourlyForecast>'
|
||||||
|
|
||||||
f = open("./.temp/HourlyForecast.i2m", "a")
|
async with aiofiles.open('./.temp/HourlyForecast.i2m', 'w') as f:
|
||||||
f.write(i2Doc)
|
await f.write(i2Doc)
|
||||||
f.close()
|
await f.close()
|
||||||
|
|
||||||
def makeDataFile():
|
|
||||||
|
async def makeDataFile():
|
||||||
l.info("Writing an HourlyForecast record.")
|
l.info("Writing an HourlyForecast record.")
|
||||||
header = '<Data type="HourlyForecast">'
|
header = '<Data type="HourlyForecast">'
|
||||||
footer = '</Data>'
|
footer = '</Data>'
|
||||||
|
|
||||||
with open("./.temp/HourlyForecast.i2m", 'w') as doc:
|
async with aiofiles.open("./.temp/HourlyForecast.i2m", 'w') as doc:
|
||||||
doc.write(header)
|
await doc.write(header)
|
||||||
|
|
||||||
for x, y in zip(tecciId, zipCodes):
|
for x, y in zip(tecciId, zipCodes):
|
||||||
getData(x, y)
|
await getData(x, y)
|
||||||
|
|
||||||
with open("./.temp/HourlyForecast.i2m", 'a') as end:
|
async with aiofiles.open("./.temp/HourlyForecast.i2m", 'a') as end:
|
||||||
end.write(footer)
|
await end.write(footer)
|
||||||
|
|
||||||
|
|
||||||
dom = xml.dom.minidom.parse("./.temp/HourlyForecast.i2m")
|
dom = xml.dom.minidom.parse("./.temp/HourlyForecast.i2m")
|
||||||
pretty_xml_as_string = dom.toprettyxml(indent = " ")
|
pretty_xml_as_string = dom.toprettyxml(indent = " ")
|
||||||
|
|
||||||
with open("./.temp/HourlyForecast.i2m", "w") as g:
|
async with aiofiles.open("./.temp/HourlyForecast.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 = []
|
||||||
|
Loading…
x
Reference in New Issue
Block a user