mirror of
				https://github.com/mewtek/i2ME-Legacy.git
				synced 2025-06-10 05:30:00 -05:00 
			
		
		
		
	HeatingAndCooling to asynchronous
This commit is contained in:
		| @@ -7,6 +7,7 @@ import records.LFRecord as LFR | |||||||
| import gzip | import gzip | ||||||
| from os import remove | from os import remove | ||||||
| import xml.dom.minidom | import xml.dom.minidom | ||||||
|  | import aiohttp, aiofiles | ||||||
|  |  | ||||||
| l = logging.getLogger(__name__) | l = logging.getLogger(__name__) | ||||||
| coloredlogs.install() | coloredlogs.install() | ||||||
| @@ -20,46 +21,48 @@ for i in MPC.getPrimaryLocations(): | |||||||
|  |  | ||||||
| apiKey = "21d8a80b3d6b444998a80b3d6b1449d3" | apiKey = "21d8a80b3d6b444998a80b3d6b1449d3" | ||||||
|  |  | ||||||
| def getData(coopId, geocode): | async def getData(coopId, geocode): | ||||||
|     fetchUrl = f"https://api.weather.com/v2/indices/heatCool/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}" |     fetchUrl = f"https://api.weather.com/v2/indices/heatCool/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}" | ||||||
|  |     data = "" | ||||||
|     res = requests.get(fetchUrl) |  | ||||||
|  |  | ||||||
|     if res.status_code != 200: |  | ||||||
|         l.error("DO NOT REPORT THE ERROR BELOW") |  | ||||||
|         l.error(f"Failed to write HeatingAndCooling record -- Status code {res.status_code}") |  | ||||||
|         return |  | ||||||
|      |      | ||||||
|     data = res.text |     async with aiohttp.ClientSession() as s: | ||||||
|  |         async with s.get(fetchUrl) as r: | ||||||
|  |             if r.status != 200: | ||||||
|  |                 l.error(f"Failed to write HeatingAndCooling record -- Status code {r.status}") | ||||||
|  |                 return | ||||||
|  |  | ||||||
|  |             data = await r.text() | ||||||
|  |      | ||||||
|  |     # data = res.text | ||||||
|     newData = data[63:-26] |     newData = data[63:-26] | ||||||
|  |  | ||||||
|     i2Doc = f'\n  <HeatingAndCooling id="000000000" locationKey="{coopId}" isWxScan="0">\n    {newData}\n    <clientKey>{coopId}</clientKey>\n </HeatingAndCooling>' |     i2Doc = f'\n  <HeatingAndCooling id="000000000" locationKey="{coopId}" isWxScan="0">\n    {newData}\n    <clientKey>{coopId}</clientKey>\n </HeatingAndCooling>' | ||||||
|  |  | ||||||
|     f = open('./.temp/HeatingAndCooling.i2m', 'a') |     async with aiofiles.open('./.temp/HeatingAndCooling.i2m', 'a') as f: | ||||||
|     f.write(i2Doc) |         await f.write(i2Doc) | ||||||
|     f.close() |         await f.close() | ||||||
|  |  | ||||||
| def makeRecord(): | async def makeRecord(): | ||||||
|     l.info("Writing HeatingAndCooling record.") |     l.info("Writing HeatingAndCooling record.") | ||||||
|  |  | ||||||
|     header = '<Data type="HeatingAndCooling">' |     header = '<Data type="HeatingAndCooling">' | ||||||
|     footer = '</Data>' |     footer = '</Data>' | ||||||
|  |  | ||||||
|     with open('./.temp/HeatingAndCooling.i2m', 'a') as doc: |     async with aiofiles.open('./.temp/HeatingAndCooling.i2m', 'a') as doc: | ||||||
|         doc.write(header) |         await doc.write(header) | ||||||
|  |  | ||||||
|     for (x, y) in zip(coopIds, geocodes): |     for (x, y) in zip(coopIds, geocodes): | ||||||
|         getData(x,y) |         await getData(x,y) | ||||||
|  |  | ||||||
|     with open('./.temp/HeatingAndCooling.i2m', 'a') as end: |     async with aiofiles.open('./.temp/HeatingAndCooling.i2m', 'a') as end: | ||||||
|         end.write(footer) |         await end.write(footer) | ||||||
|  |  | ||||||
|     dom = xml.dom.minidom.parse('./.temp/HeatingAndCooling.i2m') |     dom = xml.dom.minidom.parse('./.temp/HeatingAndCooling.i2m') | ||||||
|     xmlPretty = dom.toprettyxml(indent= "  ") |     xmlPretty = dom.toprettyxml(indent= "  ") | ||||||
|  |  | ||||||
|     with open('./.temp/HeatingAndCooling.i2m', 'w') as g: |     async with aiofiles.open('./.temp/HeatingAndCooling.i2m', 'w') as g: | ||||||
|         g.write(xmlPretty[23:]) |         await g.write(xmlPretty[23:]) | ||||||
|         g.close() |         await g.close() | ||||||
|  |  | ||||||
|      |      | ||||||
|     # Compresss i2m to gzip |     # Compresss i2m to gzip | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user