Browse Source

Move ADSBX pull to function

pull/2/head
Jxck-S 4 years ago
parent
commit
44dec9912f
  1. 27
      NotifyBotMulti.py
  2. 67
      defADSBX.py
  3. 2
      plane1.ini
  4. 2
      planeClass.py

27
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():

67
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
failed = True
print("Got data but some data is invalid!")
print(e)
else:
plane_Dict = None
return plane_Dict, failed
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
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

2
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

2
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)

Loading…
Cancel
Save