mirror of
https://github.com/mewtek/i2ME-Legacy.git
synced 2025-05-12 01:10:24 -05:00
TideForecast to asynchronous
This commit is contained in:
parent
68140eb0b2
commit
639db76723
@ -1,6 +1,4 @@
|
|||||||
import shutil
|
import shutil
|
||||||
from xmlrpc.client import DateTime
|
|
||||||
import requests
|
|
||||||
import logging,coloredlogs
|
import logging,coloredlogs
|
||||||
import datetime
|
import datetime
|
||||||
from py2Lib import bit
|
from py2Lib import bit
|
||||||
@ -9,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()
|
||||||
@ -22,31 +21,33 @@ for i in MPC.getTideStations():
|
|||||||
|
|
||||||
apiKey = "21d8a80b3d6b444998a80b3d6b1449d3"
|
apiKey = "21d8a80b3d6b444998a80b3d6b1449d3"
|
||||||
|
|
||||||
def getData(tideStation, geocode):
|
async def getData(tideStation, geocode):
|
||||||
today = datetime.date.today()
|
today = datetime.date.today()
|
||||||
startDate = today.strftime('%Y%m%d')
|
startDate = today.strftime('%Y%m%d')
|
||||||
endDate_unformatted = datetime.datetime.strptime(startDate, '%Y%m%d') + datetime.timedelta(days=5)
|
endDate_unformatted = datetime.datetime.strptime(startDate, '%Y%m%d') + datetime.timedelta(days=5)
|
||||||
endDate = endDate_unformatted.strftime('%Y%m%d')
|
endDate = endDate_unformatted.strftime('%Y%m%d')
|
||||||
|
data = ""
|
||||||
|
|
||||||
fetchUrl = f"https://api.weather.com/v1/geocode/{geocode}/forecast/tides.xml?language=en-US&units=e&startDate={startDate}&endDate={endDate}&apiKey={apiKey}"
|
fetchUrl = f"https://api.weather.com/v1/geocode/{geocode}/forecast/tides.xml?language=en-US&units=e&startDate={startDate}&endDate={endDate}&apiKey={apiKey}"
|
||||||
|
|
||||||
res = requests.get(fetchUrl)
|
async with aiohttp.ClientSession() as s:
|
||||||
|
async with s.get(fetchUrl) as r:
|
||||||
if res.status_code != 200:
|
if r.status != 200:
|
||||||
l.error("DO NOT REPORT THE ERROR BELOW")
|
l.error(f"Failed to write TideForecast -- status code {r.status}")
|
||||||
l.error(f"Failed to write TidesForecast record -- Status code {res.status_code}")
|
|
||||||
return
|
return
|
||||||
|
|
||||||
data = res.text
|
data = await r.text()
|
||||||
|
|
||||||
|
|
||||||
newData = data[53:-16]
|
newData = data[53:-16]
|
||||||
|
|
||||||
i2Doc = f'\n <TidesForecast id="000000000" locationKey="{tideStation}" isWxScan="0">\n {newData}\n <clientKey>{tideStation}</clientKey>\n </TidesForecast>'
|
i2Doc = f'\n <TidesForecast id="000000000" locationKey="{tideStation}" isWxScan="0">\n {newData}\n <clientKey>{tideStation}</clientKey>\n </TidesForecast>'
|
||||||
|
|
||||||
f = open('./.temp/TidesForecast.i2m', 'a')
|
async with aiofiles.open('./.temp/TidesForecast.i2m', 'a') as f:
|
||||||
f.write(i2Doc)
|
await f.write(i2Doc)
|
||||||
f.close()
|
await f.close()
|
||||||
|
|
||||||
def makeRecord():
|
async def makeRecord():
|
||||||
if len(tideStations) < 1:
|
if len(tideStations) < 1:
|
||||||
l.debug("Skipping TidesForecast -- No locations.")
|
l.debug("Skipping TidesForecast -- No locations.")
|
||||||
return
|
return
|
||||||
@ -56,21 +57,21 @@ def makeRecord():
|
|||||||
header = '<Data type="TidesForecast">'
|
header = '<Data type="TidesForecast">'
|
||||||
footer = '</Data>'
|
footer = '</Data>'
|
||||||
|
|
||||||
with open('./.temp/TidesForecast.i2m', 'a') as doc:
|
async with aiofiles.open('./.temp/TidesForecast.i2m', 'a') as doc:
|
||||||
doc.write(header)
|
await doc.write(header)
|
||||||
|
|
||||||
for (x, y) in zip(tideStations, geocodes):
|
for (x, y) in zip(tideStations, geocodes):
|
||||||
getData(x,y)
|
await getData(x,y)
|
||||||
|
|
||||||
with open('./.temp/TidesForecast.i2m', 'a') as end:
|
async with aiofiles.open('./.temp/TidesForecast.i2m', 'a') as end:
|
||||||
end.write(footer)
|
await end.write(footer)
|
||||||
|
|
||||||
dom = xml.dom.minidom.parse('./.temp/TidesForecast.i2m')
|
dom = xml.dom.minidom.parse('./.temp/TidesForecast.i2m')
|
||||||
xmlPretty = dom.toprettyxml(indent= " ")
|
xmlPretty = dom.toprettyxml(indent= " ")
|
||||||
|
|
||||||
with open('./.temp/TidesForecast.i2m', 'w') as g:
|
async with aiofiles.open('./.temp/TidesForecast.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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user