diff --git a/recordGenerators/AirQuality.py b/recordGenerators/AirQuality.py index 16f6f34..23bbe67 100644 --- a/recordGenerators/AirQuality.py +++ b/recordGenerators/AirQuality.py @@ -32,39 +32,50 @@ def getData(epaId, zipcode): newData = data[57:-11] # Write to i2doc file - i2Doc = f'' + '' + newData + f'' + i2Doc = f'' + '' + newData + f'{epaId}' - f = open("D:\\AirQuality.xml", 'a') + f = open("D:\\AirQuality.i2m", 'a') f.write(i2Doc) f.close() def writeData(): + useData = False + workingEpaIds = [] + + for i in epaIds: + if i == None: + print(f"No EPA ID found for location -- Skipping.") + else: + print(f"EPA ID found for location! Writing data for Air Quality.") + workingEpaIds.append(i) + useData = True + # Check to see if we even have EPA ids, as some areas don't have air quality reports - if (epaIds != None or epaIds != ['']): + if (useData): header = '' footer = "" with open("D:\\AirQuality.i2m", 'w') as doc: doc.write(header) - for (x, y) in zip(epaIds, zipCodes): + for (x, y) in zip(workingEpaIds, zipCodes): getData(x, y) with open("D:\\AirQuality.i2m", 'a') as end: end.write(footer) dom = xml.dom.minidom.parse("D:\\AirQuality.i2m") - xml = dom.toprettyxml(indent = " ") + xmlPretty = dom.toprettyxml(indent = " ") with open("D:\\AirQuality.i2m", 'w') as g: - g.write(xml) + g.write(xmlPretty[23:]) g.close() files = [] commands = [] with open("D:\\AirQuality.i2m", 'rb') as f_in: - with gzip.open("D:\\AirQuality.xml", 'wb') as f_out: + with gzip.open("D:\\AirQuality.gz", 'wb') as f_out: shutil.copyfileobj(f_in, f_out) gZipFile = "D:\\AirQuality.gz" @@ -78,7 +89,7 @@ def writeData(): os.remove("D:\\AirQuality.i2m") os.remove("D:\\AirQuality.gz") else: - print("Ignoring AirQuality data collection -- No epaIds for primary locations.") + print("Ignoring AirQuality data collection -- No working EPA Ids.") - \ No newline at end of file + diff --git a/recordGenerators/AirportDelays.py b/recordGenerators/AirportDelays.py new file mode 100644 index 0000000..6e625ef --- /dev/null +++ b/recordGenerators/AirportDelays.py @@ -0,0 +1,96 @@ +import requests +import gzip +import os +import shutil +import xml.dom.minidom + +import sys +sys.path.append("./py2lib") +sys.path.append("./Util") +sys.path.append("./records") +import bit +import MachineProductCfg as MPC +import LFRecord as LFR + +locationIds = [] +zipCodes = [] +airports = [] + +for i in MPC.getPrimaryLocations(): + locationIds.append(LFR.getCoopId(i)) + zipCodes.append(LFR.getZip(i)) + +airports = MPC.getAirportCodes() +print(airports) + +apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' + +def getData(airport): + url = f"https://api.weather.com/v1/airportcode/{airport}/airport/delays.xml?language=en-US&apiKey={apiKey}" + + res = requests.get(url=url) + + data = res.text + newData = data[48:-11] + + # Write to i2doc file + i2Doc = f'' + '' + newData + f'{airport}' + print(f"[AIRPORT DELAYS] Writing airport delay data for {airport}") + + f = open("D:\\AirportDelays.i2m", 'a') + f.write(i2Doc) + f.close() + +def writeData(): + useData = False + airportsWithDelays = [] + + for x in airports: + # Do a quick check to see if the airport in question has a delay or not + 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..") + else: + airportsWithDelays.append(x) + print(f"[AIRPORT DELAYS] {x} has a delay! Writing a file..") + useData = True + + if (useData): + header = '' + footer = "" + + with open("D:\\AirportDelays.i2m", 'w') as doc: + doc.write(header) + + for x in airportsWithDelays: + getData(x) + + with open("D:\\AirportDelays.i2m", 'a') as end: + end.write(footer) + + dom = xml.dom.minidom.parse("D:\\AirportDelays.i2m") + prettyXml = dom.toprettyxml(indent=" ") + + with open("D:\\AirportDelays.i2m", 'w') as g: + g.write(prettyXml) + g.close() + + files = [] + commands = [] + with open("D:\\AirportDelays.i2m", 'rb') as f_in: + with gzip.open("D:\\AirportDelays.gz", 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) + + gZipFile = "D:\\AirportDelays.gz" + + files.append(gZipFile) + comand = commands.append('') + numFiles = len(files) + + bit.sendFile(files, commands, numFiles, 0) + + 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.") diff --git a/recordGenerators/Breathing.py b/recordGenerators/Breathing.py new file mode 100644 index 0000000..fa6cf97 --- /dev/null +++ b/recordGenerators/Breathing.py @@ -0,0 +1,86 @@ +import requests +import sys +import gzip +import uuid +import os +import shutil +import xml.dom.minidom + +sys.path.append("./py2lib") +sys.path.append("./Util") +sys.path.append("./records") +import bit +import MachineProductCfg as MPC +import LFRecord as LFR + + +coopIds = [] +geocodes = [] + + +# Auto-grab the tecci and zip codes +for i in MPC.getPrimaryLocations(): + coopIds.append(LFR.getCoopId(i)) + geocodes.append(LFR.getLatLong(i).replace('/', ',')) + +print(coopIds, geocodes) + +apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' + +def getData(coopId, geocode): + fetchUrl = f"https://api.weather.com/v2/indices/breathing/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}" + + #Fetch data + + response = requests.get(fetchUrl) + + data = response.text + + newData = data[63:-26] + + print('[BREATHING] Gathering data for location id ' + coopId) + #Write to .i2m file + i2Doc = '' + '' + newData + '' + str(coopId) + '' + + f = open("D:\\Breathing.i2m", "a") + f.write(i2Doc) + f.close() + + +def makeDataFile(): + header = '' + footer = '' + + with open("D:\\Breathing.i2m", 'w') as doc: + doc.write(header) + + for x, y in zip(coopIds, geocodes): + getData(x, y) + + with open("D:\\Breathing.i2m", 'a') as end: + end.write(footer) + + + dom = xml.dom.minidom.parse("D:\\Breathing.i2m") + pretty_xml_as_string = dom.toprettyxml(indent = " ") + + with open("D:\\Breathing.i2m", "w") as g: + g.write(pretty_xml_as_string[23:]) + g.close() + + files = [] + commands = [] + with open("D:\\Breathing.i2m", 'rb') as f_in: + with gzip.open("D:\\Breathing.gz", 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) + + gZipFile = "D:\\Breathing.gz" + + files.append(gZipFile) + command = commands.append('') + numFiles = len(files) + + bit.sendFile(files, commands, numFiles, 0) + + os.remove("D:\\Breathing.i2m") + os.remove("D:\\Breathing.gz") \ No newline at end of file diff --git a/recordGenerators/CurrentObservations.py b/recordGenerators/CurrentObservations.py index f04b80a..7f34139 100644 --- a/recordGenerators/CurrentObservations.py +++ b/recordGenerators/CurrentObservations.py @@ -1,13 +1,27 @@ import requests -import bit +import py2Lib.bit as bit import gzip import uuid import os import shutil import xml.dom.minidom -tecciId = ['T72462058'] -zipCodes = ['81242'] +import sys +sys.path.append("./py2lib") +sys.path.append("./Util") +sys.path.append("./records") +import bit +import MachineProductCfg as MPC +import LFRecord as LFR + + +tecciId = [] +zipCodes = [] + +# Auto-grab the tecci and zip codes +for i in MPC.getPrimaryLocations(): + tecciId.append("T" + LFR.getCoopId(i)) + zipCodes.append(LFR.getZip(i)) apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' @@ -23,7 +37,7 @@ def getData(tecci, zipCode): newData = data[67:-30] - print('Gathering data for location id ' + tecci) + print('[CURRENT CONDITIONS] Gathering data for location id ' + tecci) #Write to .i2m file i2Doc = '' + '' + newData + '' + str(tecci) + '' @@ -32,39 +46,40 @@ def getData(tecci, zipCode): f.write(i2Doc) f.close() -header = '' -footer = '' +def makeDataFile(): + header = '' + footer = '' -with open("D:\\CurrentObservations.i2m", 'w') as doc: - doc.write(header) + with open("D:\\CurrentObservations.i2m", 'w') as doc: + doc.write(header) -for x, y in zip(tecciId, zipCodes): - getData(x, y) - -with open("D:\\CurrentObservations.i2m", 'a') as end: - end.write(footer) + for x, y in zip(tecciId, zipCodes): + getData(x, y) + + with open("D:\\CurrentObservations.i2m", 'a') as end: + end.write(footer) -dom = xml.dom.minidom.parse("D:\\CurrentObservations.i2m") -pretty_xml_as_string = dom.toprettyxml(indent = " ") + dom = xml.dom.minidom.parse("D:\\CurrentObservations.i2m") + pretty_xml_as_string = dom.toprettyxml(indent = " ") -with open("D:\\CurrentObservations.i2m", "w") as g: - g.write(pretty_xml_as_string[23:]) - g.close() + with open("D:\\CurrentObservations.i2m", "w") as g: + g.write(pretty_xml_as_string[23:]) + g.close() -files = [] -commands = [] + files = [] + commands = [] -with open("D:\\CurrentObservations.i2m", 'rb') as f_in: - with gzip.open("D:\\CurrentObservations.gz", 'wb') as f_out: - shutil.copyfileobj(f_in, f_out) + with open("D:\\CurrentObservations.i2m", 'rb') as f_in: + with gzip.open("D:\\CurrentObservations.gz", 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) -gZipFile = "D:\\CurrentObservations.gz" + gZipFile = "D:\\CurrentObservations.gz" -files.append(gZipFile) -command = commands.append('') -numFiles = len(files) + files.append(gZipFile) + command = commands.append('') + numFiles = len(files) -bit.sendFile(files, commands, numFiles, 0) + bit.sendFile(files, commands, numFiles, 0) -os.remove("D:\\CurrentObservations.i2m") -os.remove("D:\\CurrentObservations.gz") + os.remove("D:\\CurrentObservations.i2m") + os.remove("D:\\CurrentObservations.gz") diff --git a/recordGenerators/DailyForecast.py b/recordGenerators/DailyForecast.py index b39c839..0440c2d 100644 --- a/recordGenerators/DailyForecast.py +++ b/recordGenerators/DailyForecast.py @@ -1,13 +1,26 @@ import requests -import bit +import sys import gzip import uuid import os import shutil import xml.dom.minidom -tecciId = ['72462058'] -zipCodes = ['81242'] +sys.path.append("./py2lib") +sys.path.append("./Util") +sys.path.append("./records") +import bit +import MachineProductCfg as MPC +import LFRecord as LFR + + +tecciId = [] +zipCodes = [] + +# Auto-grab the tecci and zip codes +for i in MPC.getPrimaryLocations(): + tecciId.append(LFR.getCoopId(i)) + zipCodes.append(LFR.getZip(i)) apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' @@ -22,7 +35,7 @@ def getData(tecci, zipCode): newData = data[61:-24] - print('Gathering data for location id ' + tecci) + print('[DAILY FORECAST] Gathering data for location id ' + tecci) #Write to .i2m file i2Doc = '' + '' + newData + '' + str(tecci) + '' @@ -30,39 +43,41 @@ def getData(tecci, zipCode): f.write(i2Doc) f.close() -header = '' -footer = '' -with open("D:\\DailyForecast.i2m", 'w') as doc: - doc.write(header) +def makeDataFile(): + header = '' + footer = '' -for x, y in zip(tecciId, zipCodes): - getData(x, y) - -with open("D:\\DailyForecast.i2m", 'a') as end: - end.write(footer) + with open("D:\\DailyForecast.i2m", 'w') as doc: + doc.write(header) + + for x, y in zip(tecciId, zipCodes): + getData(x, y) + + with open("D:\\DailyForecast.i2m", 'a') as end: + end.write(footer) -dom = xml.dom.minidom.parse("D:\\DailyForecast.i2m") -pretty_xml_as_string = dom.toprettyxml(indent = " ") + dom = xml.dom.minidom.parse("D:\\DailyForecast.i2m") + pretty_xml_as_string = dom.toprettyxml(indent = " ") -with open("D:\\DailyForecast.i2m", "w") as g: - g.write(pretty_xml_as_string[23:]) - g.close() + with open("D:\\DailyForecast.i2m", "w") as g: + g.write(pretty_xml_as_string[23:]) + g.close() -files = [] -commands = [] -with open("D:\\DailyForecast.i2m", 'rb') as f_in: - with gzip.open("D:\\DailyForecast.gz", 'wb') as f_out: - shutil.copyfileobj(f_in, f_out) + files = [] + commands = [] + with open("D:\\DailyForecast.i2m", 'rb') as f_in: + with gzip.open("D:\\DailyForecast.gz", 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) -gZipFile = "D:\\DailyForecast.gz" + gZipFile = "D:\\DailyForecast.gz" -files.append(gZipFile) -command = commands.append('') -numFiles = len(files) + files.append(gZipFile) + command = commands.append('') + numFiles = len(files) -bit.sendFile(files, commands, numFiles, 0) + bit.sendFile(files, commands, numFiles, 0) -os.remove("D:\\DailyForecast.i2m") -os.remove("D:\\DailyForecast.gz") \ No newline at end of file + os.remove("D:\\DailyForecast.i2m") + os.remove("D:\\DailyForecast.gz") \ No newline at end of file diff --git a/recordGenerators/HourlyForecast.py b/recordGenerators/HourlyForecast.py index d54c9c8..040a3ea 100644 --- a/recordGenerators/HourlyForecast.py +++ b/recordGenerators/HourlyForecast.py @@ -1,13 +1,26 @@ import requests -import bit import gzip import uuid import os import shutil import xml.dom.minidom -tecciId = ['72462058'] -zipCodes = ['81242'] +import sys +sys.path.append("./py2lib") +sys.path.append("./Util") +sys.path.append("./records") +import bit +import MachineProductCfg as MPC +import LFRecord as LFR + + +tecciId = [] +zipCodes = [] + +# Auto-grab the tecci and zip codes +for i in MPC.getPrimaryLocations(): + tecciId.append(LFR.getCoopId(i)) + zipCodes.append(LFR.getZip(i)) apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' @@ -22,7 +35,7 @@ def getData(tecci, zipCode): newData = data[48:-11] - print('Gathering data for location id ' + tecci) + print('[HOURLY FORECAST] Gathering data for location id ' + tecci) #Write to .i2m file i2Doc = '' + '' + newData + '' + str(tecci) + '' @@ -30,39 +43,40 @@ def getData(tecci, zipCode): f.write(i2Doc) f.close() -header = '' -footer = '' +def makeDataFile(): + header = '' + footer = '' -with open("D:\\HourlyForecast.i2m", 'w') as doc: - doc.write(header) + with open("D:\\HourlyForecast.i2m", 'w') as doc: + doc.write(header) -for x, y in zip(tecciId, zipCodes): - getData(x, y) - -with open("D:\\HourlyForecast.i2m", 'a') as end: - end.write(footer) + for x, y in zip(tecciId, zipCodes): + getData(x, y) + + with open("D:\\HourlyForecast.i2m", 'a') as end: + end.write(footer) -dom = xml.dom.minidom.parse("D:\\HourlyForecast.i2m") -pretty_xml_as_string = dom.toprettyxml(indent = " ") + dom = xml.dom.minidom.parse("D:\\HourlyForecast.i2m") + pretty_xml_as_string = dom.toprettyxml(indent = " ") -with open("D:\\HourlyForecast.i2m", "w") as g: - g.write(pretty_xml_as_string[23:]) - g.close() + with open("D:\\HourlyForecast.i2m", "w") as g: + g.write(pretty_xml_as_string[23:]) + g.close() -files = [] -commands = [] -with open("D:\\HourlyForecast.i2m", 'rb') as f_in: - with gzip.open("D:\\HourlyForecast.gz", 'wb') as f_out: - shutil.copyfileobj(f_in, f_out) + files = [] + commands = [] + with open("D:\\HourlyForecast.i2m", 'rb') as f_in: + with gzip.open("D:\\HourlyForecast.gz", 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) -gZipFile = "D:\\HourlyForecast.gz" + gZipFile = "D:\\HourlyForecast.gz" -files.append(gZipFile) -command = commands.append('') -numFiles = len(files) + files.append(gZipFile) + command = commands.append('') + numFiles = len(files) -bit.sendFile(files, commands, numFiles, 0) + bit.sendFile(files, commands, numFiles, 0) -os.remove("D:\\HourlyForecast.i2m") -os.remove("D:\\HourlyForecast.gz") \ No newline at end of file + os.remove("D:\\HourlyForecast.i2m") + os.remove("D:\\HourlyForecast.gz") \ No newline at end of file diff --git a/recordGenerators/PollenForecast.py b/recordGenerators/PollenForecast.py new file mode 100644 index 0000000..0f99aed --- /dev/null +++ b/recordGenerators/PollenForecast.py @@ -0,0 +1,86 @@ +import requests +import sys +import gzip +import uuid +import os +import shutil +import xml.dom.minidom + +sys.path.append("./py2lib") +sys.path.append("./Util") +sys.path.append("./records") +import bit +import MachineProductCfg as MPC +import LFRecord as LFR + + +pollenIds = [] +geocodes = [] + + +# Auto-grab the tecci and zip codes +for i in MPC.getPrimaryLocations(): + pollenIds.append(LFR.getPollenInfo(i)) + geocodes.append(LFR.getLatLong(i).replace('/', ',')) + +print(pollenIds, geocodes) + +apiKey = '21d8a80b3d6b444998a80b3d6b1449d3' + +def getData(pollenId, geocode): + fetchUrl = f"https://api.weather.com/v2/indices/pollen/daypart/7day?geocode={geocode}&language=en-US&format=xml&apiKey={apiKey}" + + #Fetch data + + response = requests.get(fetchUrl) + + data = response.text + + newData = data[63:-26] + + print('[POLLEN FORECAST] Gathering data for location id ' + pollenId) + #Write to .i2m file + i2Doc = '' + '' + newData + '' + str(pollenId) + '' + + f = open("D:\\PollenForecast.i2m", "a") + f.write(i2Doc) + f.close() + + +def makeDataFile(): + header = '' + footer = '' + + with open("D:\\PollenForecast.i2m", 'w') as doc: + doc.write(header) + + for x, y in zip(pollenIds, geocodes): + getData(x, y) + + with open("D:\\PollenForecast.i2m", 'a') as end: + end.write(footer) + + + dom = xml.dom.minidom.parse("D:\\PollenForecast.i2m") + pretty_xml_as_string = dom.toprettyxml(indent = " ") + + with open("D:\\PollenForecast.i2m", "w") as g: + g.write(pretty_xml_as_string[23:]) + g.close() + + files = [] + commands = [] + with open("D:\\PollenForecast.i2m", 'rb') as f_in: + with gzip.open("D:\\PollenForecast.gz", 'wb') as f_out: + shutil.copyfileobj(f_in, f_out) + + gZipFile = "D:\\PollenForecast.gz" + + files.append(gZipFile) + command = commands.append('') + numFiles = len(files) + + bit.sendFile(files, commands, numFiles, 0) + + os.remove("D:\\PollenForecast.i2m") + os.remove("D:\\PollenForecast.gz") \ No newline at end of file