RapidAPI
This commit is contained in:
parent
cc7a584ac8
commit
c7d11b95f4
82
__main__.py
82
__main__.py
|
|
@ -1,5 +1,4 @@
|
||||||
import configparser
|
import configparser
|
||||||
from logging import DEBUG
|
|
||||||
import time
|
import time
|
||||||
from colorama import Fore, Back, Style
|
from colorama import Fore, Back, Style
|
||||||
import platform
|
import platform
|
||||||
|
|
@ -49,11 +48,11 @@ main_config.read('./configs/mainconf.ini')
|
||||||
source = main_config.get('DATA', 'SOURCE')
|
source = main_config.get('DATA', 'SOURCE')
|
||||||
if main_config.getboolean('DISCORD', 'ENABLE'):
|
if main_config.getboolean('DISCORD', 'ENABLE'):
|
||||||
from defDiscord import sendDis
|
from defDiscord import sendDis
|
||||||
sendDis("Started", main_config, role_id = main_config.get('DISCORD', 'ROLE_ID'))
|
sendDis("Started", main_config)
|
||||||
def service_exit(signum, frame):
|
def service_exit(signum, frame):
|
||||||
if main_config.getboolean('DISCORD', 'ENABLE'):
|
if main_config.getboolean('DISCORD', 'ENABLE'):
|
||||||
from defDiscord import sendDis
|
from defDiscord import sendDis
|
||||||
sendDis("Service Stop", main_config, role_id = main_config.get('DISCORD', 'ROLE_ID'))
|
sendDis("Service Stop", main_config)
|
||||||
raise SystemExit("Service Stop")
|
raise SystemExit("Service Stop")
|
||||||
signal.signal(signal.SIGTERM, service_exit)
|
signal.signal(signal.SIGTERM, service_exit)
|
||||||
if os.path.isfile("lookup_route.py"):
|
if os.path.isfile("lookup_route.py"):
|
||||||
|
|
@ -83,7 +82,6 @@ try:
|
||||||
except pytz.exceptions.UnknownTimeZoneError:
|
except pytz.exceptions.UnknownTimeZoneError:
|
||||||
tz = pytz.UTC
|
tz = pytz.UTC
|
||||||
last_ra_count = None
|
last_ra_count = None
|
||||||
print(len(planes), "Planes configured")
|
|
||||||
while True:
|
while True:
|
||||||
datetime_tz = datetime.now(tz)
|
datetime_tz = datetime.now(tz)
|
||||||
if datetime_tz.hour == 0 and datetime_tz.minute == 0:
|
if datetime_tz.hour == 0 and datetime_tz.minute == 0:
|
||||||
|
|
@ -140,12 +138,75 @@ try:
|
||||||
for planeData in data['ac']:
|
for planeData in data['ac']:
|
||||||
data_indexed[planeData[icao_key].upper()] = planeData
|
data_indexed[planeData[icao_key].upper()] = planeData
|
||||||
for key, obj in planes.items():
|
for key, obj in planes.items():
|
||||||
if key in data_indexed.keys():
|
try:
|
||||||
if api_version == 1:
|
if api_version == 1:
|
||||||
obj.run_adsbx_v1(data_indexed[key.upper()])
|
obj.run_adsbx_v1(data_indexed[key.upper()])
|
||||||
elif api_version == 2:
|
elif api_version == 2:
|
||||||
obj.run_adsbx_v2(data_indexed[key.upper()])
|
obj.run_adsbx_v2(data_indexed[key.upper()])
|
||||||
else:
|
except KeyError:
|
||||||
|
obj.run_empty()
|
||||||
|
else:
|
||||||
|
for obj in planes.values():
|
||||||
|
obj.run_empty()
|
||||||
|
else:
|
||||||
|
failed_count += 1
|
||||||
|
elif source == "RpdADSBX":
|
||||||
|
#ACAS data
|
||||||
|
from defADSBX import pull_date_ras
|
||||||
|
import ast
|
||||||
|
today = datetime.utcnow()
|
||||||
|
date = today.strftime("%Y/%m/%d")
|
||||||
|
ras = pull_date_ras(date)
|
||||||
|
sorted_ras = {}
|
||||||
|
if ras is not None:
|
||||||
|
#Testing RAs
|
||||||
|
#if last_ra_count is not None:
|
||||||
|
# with open('./testing/acastest.json') as f:
|
||||||
|
# data = f.readlines()
|
||||||
|
# ras += data
|
||||||
|
ra_count = len(ras)
|
||||||
|
if last_ra_count is not None and ra_count != last_ra_count:
|
||||||
|
print(abs(ra_count - last_ra_count), "new Resolution Advisories")
|
||||||
|
for ra_num, ra in enumerate(ras[last_ra_count:]):
|
||||||
|
ra = ast.literal_eval(ra)
|
||||||
|
if ra['hex'].upper() in planes.keys():
|
||||||
|
if ra['hex'].upper() not in sorted_ras.keys():
|
||||||
|
sorted_ras[ra['hex'].upper()] = [ra]
|
||||||
|
else:
|
||||||
|
sorted_ras[ra['hex'].upper()].append(ra)
|
||||||
|
else:
|
||||||
|
print("No new Resolution Advisories")
|
||||||
|
last_ra_count = ra_count
|
||||||
|
for key, obj in planes.items():
|
||||||
|
if sorted_ras != {} and key in sorted_ras.keys():
|
||||||
|
print(key, "has", len(sorted_ras[key]), "RAs")
|
||||||
|
obj.check_new_ras(sorted_ras[key])
|
||||||
|
obj.expire_ra_types()
|
||||||
|
icao_key = 'hex'
|
||||||
|
from defRpdADSBX import pull_rpdadsbx
|
||||||
|
# print("Planes list: \n"+str((list(planes.keys()))))
|
||||||
|
# print("\nLen planes:\n"+str(len(planes)))
|
||||||
|
p = 0
|
||||||
|
data = dict.fromkeys(['ac'])
|
||||||
|
# print(data)
|
||||||
|
while p < len(planes):
|
||||||
|
planeInfo = pull_rpdadsbx(str(list(planes.keys())[p]))
|
||||||
|
if p == 0:
|
||||||
|
data['ac'] = (planeInfo)['ac']
|
||||||
|
else:
|
||||||
|
data['ac'].extend((planeInfo)['ac'])
|
||||||
|
# print("p = "+str(p))
|
||||||
|
# print(str(list(planes.keys())[p]) + ": " + str(data))
|
||||||
|
p += 1
|
||||||
|
if data is not None:
|
||||||
|
if data['ac'] is not None:
|
||||||
|
data_indexed = {}
|
||||||
|
for planeData in data['ac']:
|
||||||
|
data_indexed[planeData[icao_key].upper()] = planeData
|
||||||
|
for key, obj in planes.items():
|
||||||
|
try:
|
||||||
|
obj.run_adsbx_v2(data_indexed[key.upper()])
|
||||||
|
except KeyError:
|
||||||
obj.run_empty()
|
obj.run_empty()
|
||||||
else:
|
else:
|
||||||
for obj in planes.values():
|
for obj in planes.values():
|
||||||
|
|
@ -186,7 +247,10 @@ try:
|
||||||
footer = "-------- " + str(running_Count) + " -------- " + str(datetime_tz.strftime("%I:%M:%S %p")) + " ------------------------Elapsed Time- " + str(round(elapsed_calc_time, 3)) + " -------------------------------------"
|
footer = "-------- " + str(running_Count) + " -------- " + str(datetime_tz.strftime("%I:%M:%S %p")) + " ------------------------Elapsed Time- " + str(round(elapsed_calc_time, 3)) + " -------------------------------------"
|
||||||
print (Back.GREEN + Fore.BLACK + footer[0:100] + Style.RESET_ALL)
|
print (Back.GREEN + Fore.BLACK + footer[0:100] + Style.RESET_ALL)
|
||||||
|
|
||||||
sleep_sec = 30
|
if main_config.has_section('SLEEP'):
|
||||||
|
sleep_sec = int(main_config.get('SLEEP', 'SLEEPSEC'))
|
||||||
|
else:
|
||||||
|
sleep_sec = 30
|
||||||
for i in range(sleep_sec,0,-1):
|
for i in range(sleep_sec,0,-1):
|
||||||
if i < 10:
|
if i < 10:
|
||||||
i = " " + str(i)
|
i = " " + str(i)
|
||||||
|
|
@ -208,10 +272,10 @@ except Exception as e:
|
||||||
except OSError:
|
except OSError:
|
||||||
pass
|
pass
|
||||||
import logging
|
import logging
|
||||||
logging.basicConfig(filename='crash_latest.log', filemode='w', format='%(asctime)s - %(message)s',level=logging.DEBUG)
|
logging.basicConfig(filename='crash_latest.log', filemode='w', format='%(asctime)s - %(message)s')
|
||||||
logging.Formatter.converter = time.gmtime
|
logging.Formatter.converter = time.gmtime
|
||||||
logging.error(e)
|
logging.error(e)
|
||||||
logging.error(str(traceback.format_exc()))
|
logging.error(str(traceback.format_exc()))
|
||||||
from defDiscord import sendDis
|
from defDiscord import sendDis
|
||||||
sendDis(str("Error Exiting: " + str(e) + " Failed on " + "https://globe.adsbexchange.com/?icao=" + key), main_config, main_config.get('DISCORD', 'ROLE_ID'), "crash_latest.log")
|
sendDis(str("Error Exiting: " + str(e) + "Failed on " + key), main_config, "crash_latest.log")
|
||||||
raise e
|
raise e
|
||||||
|
|
@ -2,8 +2,8 @@
|
||||||
#Source to pull data from
|
#Source to pull data from
|
||||||
#SHOULD BE ADSBX which is ADS-B Exchange or OPENS which is OpenSky
|
#SHOULD BE ADSBX which is ADS-B Exchange or OPENS which is OpenSky
|
||||||
#By default configured with OpenSky which anyone can use without a login
|
#By default configured with OpenSky which anyone can use without a login
|
||||||
#ADS-B Exchange has better data but is not avalible unless you pay (see: https://www.adsbexchange.com/data/ )
|
#ADS-B Exchange has better data but is not avalible unless you feed their network or pay.
|
||||||
SOURCE = OPENS
|
SOURCE = RpdADSBX
|
||||||
#Default amount of time after data loss to trigger a landing when under 10k ft
|
#Default amount of time after data loss to trigger a landing when under 10k ft
|
||||||
DATA_LOSS_MINS = 5
|
DATA_LOSS_MINS = 5
|
||||||
#Failover from one source to the other, only enable if you have both sources setup.
|
#Failover from one source to the other, only enable if you have both sources setup.
|
||||||
|
|
@ -28,6 +28,15 @@ PROXY_HOST =
|
||||||
USERNAME = None
|
USERNAME = None
|
||||||
PASSWORD = None
|
PASSWORD = None
|
||||||
|
|
||||||
|
#ADS-B Exchange on RapidAPI https://rapidapi.com/adsbx/api/adsbexchange-com1/
|
||||||
|
[RpdADSBX]
|
||||||
|
API_KEY = none
|
||||||
|
API_VERSION = 2
|
||||||
|
|
||||||
|
#Define the delay interval in seconds between each data request. This is usefull if you have limited requests in the API.
|
||||||
|
[SLEEP]
|
||||||
|
SLEEPSEC = 60
|
||||||
|
|
||||||
[GOOGLE]
|
[GOOGLE]
|
||||||
#API KEY for Google Static Maps only if you using this on any of the planes.
|
#API KEY for Google Static Maps only if you using this on any of the planes.
|
||||||
API_KEY = googleapikey
|
API_KEY = googleapikey
|
||||||
|
|
@ -36,13 +45,4 @@ API_KEY = googleapikey
|
||||||
[DISCORD]
|
[DISCORD]
|
||||||
ENABLE = FALSE
|
ENABLE = FALSE
|
||||||
USERNAME = usernamehere
|
USERNAME = usernamehere
|
||||||
URL = webhookurl
|
URL = webhookurl
|
||||||
|
|
||||||
[TFRS]
|
|
||||||
URL = http://127.0.0.1:5000/detailed_all
|
|
||||||
|
|
||||||
[TWITTER]
|
|
||||||
#GLOBAL TWITTER CONSUMER KEY AND CONSUMERS SECRET ONLY NEED TO BE HERE NOW
|
|
||||||
ENABLE = False
|
|
||||||
CONSUMER_KEY = ck
|
|
||||||
CONSUMER_SECRET = cs
|
|
||||||
|
|
@ -54,4 +54,4 @@ ACCESS_TOKEN =
|
||||||
ENABLE = FALSE
|
ENABLE = FALSE
|
||||||
TITLE = Title Of Telegram message
|
TITLE = Title Of Telegram message
|
||||||
ROOM_ID = -100xxxxxxxxxx
|
ROOM_ID = -100xxxxxxxxxx
|
||||||
BOT_TOKEN = xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
BOT_TOKEN = xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||||
|
|
@ -0,0 +1,80 @@
|
||||||
|
import requests
|
||||||
|
import json
|
||||||
|
import configparser
|
||||||
|
from datetime import datetime
|
||||||
|
from http.client import IncompleteRead
|
||||||
|
import http.client as http
|
||||||
|
import urllib3
|
||||||
|
import socket
|
||||||
|
main_config = configparser.ConfigParser()
|
||||||
|
main_config.read('./configs/mainconf.ini')
|
||||||
|
api_version = main_config.get('RpdADSBX', 'API_VERSION')
|
||||||
|
|
||||||
|
# def pull(url, headers):
|
||||||
|
# try:
|
||||||
|
# response = requests.get(url, headers = headers, timeout=30)
|
||||||
|
# print ("HTTP Status Code:", response.status_code)
|
||||||
|
# response.raise_for_status()
|
||||||
|
# except (requests.HTTPError, ConnectionError, requests.Timeout, urllib3.exceptions.ConnectionError) as error_message:
|
||||||
|
# print("Basic Connection Error")
|
||||||
|
# print(error_message)
|
||||||
|
# response = None
|
||||||
|
# except (requests.RequestException, IncompleteRead, ValueError, socket.timeout, socket.gaierror) as error_message:
|
||||||
|
# print("Connection Error")
|
||||||
|
# print(error_message)
|
||||||
|
# response = None
|
||||||
|
# except Exception as error_message:
|
||||||
|
# print("Connection Error uncaught, basic exception for all")
|
||||||
|
# print(error_message)
|
||||||
|
# response = None
|
||||||
|
# return response
|
||||||
|
|
||||||
|
def pull_rpdadsbx(planes):
|
||||||
|
api_version = int(main_config.get('RpdADSBX', 'API_VERSION'))
|
||||||
|
if api_version != 2:
|
||||||
|
raise ValueError("Bad RapidAPI ADSBX API Version")
|
||||||
|
url = "https://adsbexchange-com1.p.rapidapi.com/v2/icao/" + planes + "/"
|
||||||
|
headers = {
|
||||||
|
"X-RapidAPI-Host": "adsbexchange-com1.p.rapidapi.com",
|
||||||
|
"X-RapidAPI-Key": main_config.get('RpdADSBX', 'API_KEY')
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
response = requests.get(url, headers = headers, timeout=30)
|
||||||
|
except Exception as error:
|
||||||
|
print('err.args:' + str(error.args))
|
||||||
|
response = None
|
||||||
|
if response is not None:
|
||||||
|
try:
|
||||||
|
data = json.loads(response.text)
|
||||||
|
except (json.decoder.JSONDecodeError, ValueError) as error_message:
|
||||||
|
print("Error with JSON")
|
||||||
|
print(error_message)
|
||||||
|
data = None
|
||||||
|
except TypeError as error_message:
|
||||||
|
print("Type Error", error_message)
|
||||||
|
data = None
|
||||||
|
else:
|
||||||
|
if "msg" in data.keys() and data['msg'] != "No error":
|
||||||
|
raise ValueError("Error from ADSBX: msg = ", data['msg'])
|
||||||
|
if "ctime" in data.keys():
|
||||||
|
data_ctime = float(data['ctime']) / 1000.0
|
||||||
|
print("Data ctime:",datetime.utcfromtimestamp(data_ctime))
|
||||||
|
if "now" in data.keys():
|
||||||
|
data_now = float(data['now']) / 1000.0
|
||||||
|
print("Data now time:",datetime.utcfromtimestamp(data_now))
|
||||||
|
print("Current UTC:", datetime.utcnow())
|
||||||
|
else:
|
||||||
|
data = None
|
||||||
|
return data
|
||||||
|
|
||||||
|
# def pull_date_ras(date):
|
||||||
|
# url = f"https://globe.adsbexchange.com/globe_history/{date}/acas/acas.json"
|
||||||
|
# headers = {
|
||||||
|
# 'Accept-Encoding': 'gzip'
|
||||||
|
# }
|
||||||
|
# response = pull(url, headers)
|
||||||
|
# if response is not None:
|
||||||
|
# data = response.text.splitlines()
|
||||||
|
# else:
|
||||||
|
# data = None
|
||||||
|
# return data
|
||||||
Loading…
Reference in New Issue