Use logging and coloredlogs instead of printing

This commit is contained in:
April
2022-10-17 22:17:38 -07:00
parent 7a32af77aa
commit ec9268deae
12 changed files with 193 additions and 72 deletions

View File

@ -3,6 +3,10 @@ import gzip
import os
import shutil
import xml.dom.minidom
import logging,coloredlogs
l = logging.getLogger(__name__)
coloredlogs.install()
import sys
sys.path.append("./py2lib")
@ -44,9 +48,9 @@ def writeData():
for i in epaIds:
if i == None:
print(f"No EPA ID found for location -- Skipping.")
l.debug(f"No EPA ID found for location -- Skipping.")
else:
print(f"EPA ID found for location! Writing data for Air Quality.")
l.debug(f"EPA ID found for location! Writing data for Air Quality.")
workingEpaIds.append(i)
useData = True
@ -54,6 +58,7 @@ def writeData():
# Check to see if we even have EPA ids, as some areas don't have air quality reports
if (useData):
try:
l.info("Writing an AirQuality record.")
header = '<Data type="AirQuality">'
footer = "</Data>"
@ -90,9 +95,10 @@ def writeData():
os.remove("D:\\AirQuality.i2m")
os.remove("D:\\AirQuality.gz")
except Exception as e:
print("AirQuality failed to write, problably was expired and was sticking around in the IBM api.")
l.error("DO NOT REPORT THE ERROR BELOW")
l.error("Failed to write an AirQuality record.")
else:
print("Ignoring AirQuality data collection -- No working EPA Ids.")
l.info("Not writing an AirQuality record due to a lack of working EPA ids.")

View File

@ -3,6 +3,7 @@ import gzip
import os
import shutil
import xml.dom.minidom
import logging,coloredlogs
import sys
sys.path.append("./py2lib")
@ -12,6 +13,9 @@ import bit
import MachineProductCfg as MPC
import LFRecord as LFR
l = logging.getLogger(__name__)
coloredlogs.install()
locationIds = []
zipCodes = []
airports = []
@ -35,7 +39,6 @@ def getData(airport):
# Write to i2doc file
i2Doc = f'<AirportDelays id="000000000" locationKey="{airport}" isWxScan="0">' + '' + newData + f'<clientKey>{airport}</clientKey></AirportDelays>'
print(f"[AIRPORT DELAYS] Writing airport delay data for {airport}")
f = open("D:\\AirportDelays.i2m", 'a')
f.write(i2Doc)
@ -50,13 +53,14 @@ def writeData():
res = requests.get(f"https://api.weather.com/v1/airportcode/{x}/airport/delays.xml?language=en-US&apiKey={apiKey}")
if (res.status_code != 200):
print(f"[AIRPORT DELAYS] No delays for {x} found, skipping..")
l.debug(f"[AIRPORT DELAYS] No delays for {x} found, skipping..")
else:
airportsWithDelays.append(x)
print(f"[AIRPORT DELAYS] {x} has a delay! Writing a file..")
l.debug(f"[AIRPORT DELAYS] {x} has a delay! Writing a file..")
useData = True
if (useData):
l.info("Writing an AirportDelays record.")
header = '<Data type="AirportDelays">'
footer = "</Data>"
@ -93,4 +97,4 @@ def writeData():
os.remove("D:\\AirportDelays.i2m")
os.remove("D:\\AirportDelays.gz")
else:
print("[AIRPORT DELAYS] Not writing AirportDelays -- Either no delays found, or the API is broken.")
l.info("No airport delays found.")

View File

@ -2,6 +2,7 @@ import requests
import json
import os
from datetime import datetime,timedelta
from Util.MachineProductCfg import getZones
import time
import pytz
import xml.dom.minidom
@ -10,7 +11,7 @@ import gzip
import py2Lib.bit as bit
#Zones/Counties to fetch alerts for
interestList = ['FLZ151', 'FLC057', 'FLZ149', 'FLZ249', 'FLC101'] # TODO: Grab these automatically from MachineProductCfg.xml
zones = getZones()
#You can safely edit the API key here. Make sure to include the ' before and after the key
headlineApiKey = '21d8a80b3d6b444998a80b3d6b1449d3'
@ -312,40 +313,43 @@ def getAlerts(location):
# TODO: This should be converted into a function so it works better with async, that way we're not getting hung up on that time.sleep() call.
n = 0
while n==0:
#Start our XML File
with open(path + '\\Output\\BERecord.xml', "w") as e:
e.write('<Data type="BERecord">')
e.close()
for i in interestList:
getAlerts(i)
#Close our XML File
with open(path + '\\Output\\BERecord.xml', "a") as d:
d.write('</Data>')
d.close()
dom = xml.dom.minidom.parse(path + '\\Output\\BERecord.xml')
pretty_xml_as_string = dom.toprettyxml(indent = " ")
# n = 0
# while n==0:
# #Start our XML File
# with open(path + '\\Output\\BERecord.xml', "w") as e:
# e.write('<Data type="BERecord">')
# e.close()
# for i in interestList:
# getAlerts(i)
# #Close our XML File
# with open(path + '\\Output\\BERecord.xml', "a") as d:
# d.write('</Data>')
# d.close()
# dom = xml.dom.minidom.parse(path + '\\Output\\BERecord.xml')
# pretty_xml_as_string = dom.toprettyxml(indent = " ")
with open(path + '\\Output\\BERecord.i2m', "w") as h:
h.write(pretty_xml_as_string[23:])
h.close()
if k > 0:
with open(path + '\\Output\\BERecord.i2m', 'rb') as f_in:
with gzip.open(path + '\\Output\\BERecord.gz', 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
# with open(path + '\\Output\\BERecord.i2m', "w") as h:
# h.write(pretty_xml_as_string[23:])
# h.close()
# if k > 0:
# with open(path + '\\Output\\BERecord.i2m', 'rb') as f_in:
# with gzip.open(path + '\\Output\\BERecord.gz', 'wb') as f_out:
# shutil.copyfileobj(f_in, f_out)
files = []
commands = []
gZipFile = path + '\\Output\\BERecord.gz'
files.append(gZipFile)
command = commands.append('<MSG><Exec workRequest="storeData(File={0},QGROUP=__BERecord__,Feed=BERecord)" /><GzipCompressedMsg fname="BERecord" /></MSG>')
bit.sendFile(files, commands, 1, 0)
os.remove(gZipFile)
k = 0
os.remove(path + '\\Output\\BERecord.xml')
#os.remove(path + '\\Output\\BERecord.i2m')
# files = []
# commands = []
# gZipFile = path + '\\Output\\BERecord.gz'
# files.append(gZipFile)
# command = commands.append('<MSG><Exec workRequest="storeData(File={0},QGROUP=__BERecord__,Feed=BERecord)" /><GzipCompressedMsg fname="BERecord" /></MSG>')
# bit.sendFile(files, commands, 1, 0)
# os.remove(gZipFile)
# k = 0
# os.remove(path + '\\Output\\BERecord.xml')
# #os.remove(path + '\\Output\\BERecord.i2m')
print('Will sleep for 60 seconds...\n')
time.sleep(60)
# print('Will sleep for 60 seconds...\n')
# time.sleep(60)
def makeBERecord():
pass

View File

@ -5,6 +5,7 @@ import uuid
import os
import shutil
import xml.dom.minidom
import logging,coloredlogs
sys.path.append("./py2lib")
sys.path.append("./Util")
@ -13,6 +14,8 @@ import bit
import MachineProductCfg as MPC
import LFRecord as LFR
l = logging.getLogger(__name__)
coloredlogs.install()
coopIds = []
geocodes = []
@ -38,7 +41,7 @@ def getData(coopId, geocode):
newData = data[63:-26]
print('[BREATHING] Gathering data for location id ' + coopId)
l.debug('Gathering data for location id ' + coopId)
#Write to .i2m file
i2Doc = '<Breathing id="000000000" locationKey="' + str(coopId) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(coopId) + '</clientKey></Breathing>'
@ -48,6 +51,7 @@ def getData(coopId, geocode):
def makeDataFile():
l.info("Writing a Breathing forecast record.")
header = '<Data type="Breathing">'
footer = '</Data>'

View File

@ -5,6 +5,7 @@ import uuid
import os
import shutil
import xml.dom.minidom
import logging,coloredlogs
import sys
sys.path.append("./py2lib")
@ -14,6 +15,8 @@ import bit
import MachineProductCfg as MPC
import LFRecord as LFR
l = logging.getLogger(__name__)
coloredlogs.install()
tecciId = []
zipCodes = []
@ -37,7 +40,7 @@ def getData(tecci, zipCode):
newData = data[67:-30]
print('[CURRENT CONDITIONS] Gathering data for location id ' + tecci)
l.debug('Gathering data for location id ' + tecci)
#Write to .i2m file
i2Doc = '<CurrentObservations id="000000000" locationKey="' + str(tecci) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(tecci) + '</clientKey></CurrentObservations>'
@ -47,6 +50,7 @@ def getData(tecci, zipCode):
f.close()
def makeDataFile():
l.info("Writing a CurrentObservations record.")
header = '<Data type="CurrentObservations">'
footer = '</Data>'

View File

@ -5,6 +5,7 @@ import uuid
import os
import shutil
import xml.dom.minidom
import logging,coloredlogs
sys.path.append("./py2lib")
sys.path.append("./Util")
@ -13,6 +14,8 @@ import bit
import MachineProductCfg as MPC
import LFRecord as LFR
l = logging.getLogger(__name__)
coloredlogs.install()
tecciId = []
zipCodes = []
@ -35,7 +38,7 @@ def getData(tecci, zipCode):
newData = data[61:-24]
print('[DAILY FORECAST] Gathering data for location id ' + tecci)
l.debug('Gathering data for location id ' + tecci)
#Write to .i2m file
i2Doc = '<DailyForecast id="000000000" locationKey="' + str(tecci) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(tecci) + '</clientKey></DailyForecast>'
@ -45,6 +48,7 @@ def getData(tecci, zipCode):
def makeDataFile():
l.info("Writing a DailyForecast record.")
header = '<Data type="DailyForecast">'
footer = '</Data>'

View File

@ -4,6 +4,7 @@ import uuid
import os
import shutil
import xml.dom.minidom
import logging,coloredlogs
import sys
sys.path.append("./py2lib")
@ -13,6 +14,8 @@ import bit
import MachineProductCfg as MPC
import LFRecord as LFR
l = logging.getLogger(__name__)
coloredlogs.install()
tecciId = []
zipCodes = []
@ -35,7 +38,7 @@ def getData(tecci, zipCode):
newData = data[48:-11]
print('[HOURLY FORECAST] Gathering data for location id ' + tecci)
l.debug('Gathering data for location id ' + tecci)
#Write to .i2m file
i2Doc = '<HourlyForecast id="000000000" locationKey="' + str(tecci) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(tecci) + '</clientKey></HourlyForecast>'
@ -44,6 +47,7 @@ def getData(tecci, zipCode):
f.close()
def makeDataFile():
l.info("Writing an HourlyForecast record.")
header = '<Data type="HourlyForecast">'
footer = '</Data>'

View File

@ -5,6 +5,7 @@ import uuid
import os
import shutil
import xml.dom.minidom
import logging, coloredlogs
sys.path.append("./py2lib")
sys.path.append("./Util")
@ -14,6 +15,9 @@ import MachineProductCfg as MPC
import LFRecord as LFR
l = logging.getLogger(__name__)
coloredlogs.install()
pollenIds = []
geocodes = []
@ -38,7 +42,7 @@ def getData(pollenId, geocode):
newData = data[63:-26]
print('[POLLEN FORECAST] Gathering data for location id ' + pollenId)
l.debug('Gathering data for location id ' + pollenId)
#Write to .i2m file
i2Doc = '<PollenForecast id="000000000" locationKey="' + str(pollenId) + '" isWxscan="0">' + '' + newData + '<clientKey>' + str(pollenId) + '</clientKey></PollenForecast>'
@ -48,6 +52,7 @@ def getData(pollenId, geocode):
def makeDataFile():
l.info("Writing a PollenForecast record.")
header = '<Data type="PollenForecast">'
footer = '</Data>'