From ae19c7b6b8962708f0f2e47234e23cb8fecfcd2e Mon Sep 17 00:00:00 2001 From: Jxck-S Date: Sat, 19 Sep 2020 17:32:33 -0400 Subject: [PATCH] Move ADSBX pull to function --- NotifyBotMulti.py | 27 +++---------------- defADSBX.py | 67 +++++++++++++++++++---------------------------- plane1.ini | 2 +- planeClass.py | 2 -- 4 files changed, 31 insertions(+), 67 deletions(-) diff --git a/NotifyBotMulti.py b/NotifyBotMulti.py index a76501e..3289f8b 100644 --- a/NotifyBotMulti.py +++ b/NotifyBotMulti.py @@ -2,12 +2,13 @@ import requests import configparser import json import time +from defADSBX import pullADSBX from colorama import Fore, Back, Style from planeClass import Plane main_config = configparser.ConfigParser() main_config.read('mainconf.ini') import os -#Set ADSBX URL Based off amount of Conf files +#Setup Plane Objects off of Plane configs if main_config.get('DATA', 'SOURCE') == "ADSBX": planes = {} for filename in os.listdir(os. getcwd()): @@ -15,15 +16,7 @@ if main_config.get('DATA', 'SOURCE') == "ADSBX": plane_config = configparser.ConfigParser() plane_config.read(filename) planes[plane_config.get('DATA', 'ICAO').upper()] = Plane(plane_config.get('DATA', 'ICAO'), filename) - if len(planes) > 1: - url = "https://adsbexchange.com/api/aircraft/json/" - elif len(planes) == 1: - url = "https://adsbexchange.com/api/aircraft/icao/" + str(list(planes.keys())[0]) + "/" - headers = { - 'api-auth': main_config.get('ADSBX', 'API_KEY'), - 'Content-Encoding': 'gzip' - } elif main_config.get('DATA', 'SOURCE') == "OPENS": raise NotImplementedError running_Count = 0 @@ -32,21 +25,7 @@ while True: start_time = time.time() print (Back.GREEN, Fore.BLACK, "--------", running_Count, "-------------------------------------------------------", Style.RESET_ALL) if main_config.get('DATA', 'SOURCE') == "ADSBX": - try: - response = requests.get(url, headers = headers) - data = response.text - data = json.loads(data) - failed = False - except (requests.HTTPError, requests.ConnectionError, requests.Timeout) as error_message: - print("ADSBX Connection Error") - print(error_message) - failed = True - except json.decoder.JSONDecodeError as error_message: - print("Error with JSON") - print (json.dumps(data, indent = 2)) - print(error_message) - failed = True - + data, failed = pullADSBX(planes) if failed == False: if data['ac'] != None: for key, obj in planes.items(): diff --git a/defADSBX.py b/defADSBX.py index 50f5758..90d38cf 100644 --- a/defADSBX.py +++ b/defADSBX.py @@ -1,47 +1,34 @@ import requests import json import configparser -config = configparser.ConfigParser() -config.read('config.ini') -def pullADSBX(icao): - url = 'https://adsbexchange.com/api/aircraft/icao/' + icao + "/" - headers = { - 'api-auth': config.get('ADSBX', 'API_KEY') - } - failed = False - try: - response = requests.get(url, headers = headers) - data = response.text - data = json.loads(data) - #print (json.dumps(data, indent=4)) - except (requests.HTTPError, requests.ConnectionError, requests.Timeout) as error_message: - print("ADSBX Connection Error") - print(error_message) - failed = True - plane_Dict = None - except json.decoder.JSONDecodeError as error_message: - print("Error with JSON") - print(error_message) - failed = True - plane_Dict = None - if failed is False: - ac = data['ac'] - if ac != None: - ac_dict = ac[0] - try: - plane_Dict = {'icao' : ac_dict['icao'], 'callsign' : ac_dict['call'], 'reg' : ac_dict['reg'], 'latitude' : float(ac_dict['lat']), 'longitude' : float(ac_dict['lon']), 'geo_alt_ft' : int(ac_dict['galt']), 'on_ground' : bool(int(ac_dict["gnd"]))} - if plane_Dict['on_ground']: - plane_Dict['geo_alt_ft'] = 0 - except ValueError as e: - plane_Dict = None +main_config = configparser.ConfigParser() +main_config.read('mainconf.ini') +def pullADSBX(planes): + if len(planes) > 1: + url = "https://adsbexchange.com/api/aircraft/json/" + elif len(planes) == 1: + url = "https://adsbexchange.com/api/aircraft/icao/" + str(list(planes.keys())[0]) + "/" + + headers = { + 'api-auth': main_config.get('ADSBX', 'API_KEY'), + 'Content-Encoding': 'gzip' + } + try: + response = requests.get(url, headers = headers) + data = response.text + data = json.loads(data) + failed = False + except (requests.HTTPError, requests.ConnectionError, requests.Timeout) as error_message: + print("ADSBX Connection Error") + print(error_message) failed = True - print("Got data but some data is invalid!") - print(e) - else: - plane_Dict = None - - return plane_Dict, failed - + except json.decoder.JSONDecodeError as error_message: + print("Error with JSON") + print (json.dumps(data, indent = 2)) + print(error_message) + failed = True + print("Failed:", failed) + return data, failed diff --git a/plane1.ini b/plane1.ini index 3dcf095..fd374dc 100644 --- a/plane1.ini +++ b/plane1.ini @@ -22,7 +22,7 @@ API_KEY = apikey CHANNEL_TAG = channeltag [DISCORD] -ENABLE = TRUE +ENABLE = FALSE #WEBHOOK URL https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks URL = webhookurl Title = title diff --git a/planeClass.py b/planeClass.py index 84f56c4..445afe6 100644 --- a/planeClass.py +++ b/planeClass.py @@ -103,8 +103,6 @@ class Plane: print (Fore.CYAN) if main_config.get('DATA', 'SOURCE') == "ADSBX": print("Registration: ", self.reg) - else: - print("Registration: ", "Only shows when using ADSBX!") print ("Callsign: ", self.callsign) print ("On Ground: ", self.on_ground) print ("Latitude: ", self.latitude)