Browse Source

simplifying rapidadsbx logic

- raising exception if response not 200
- decode json from resposonse lib
- simplifying iteration of planes
- updating plane data in the same loop as querying api

fixes Jxck-S/plane-notify#84
pull/85/head
Mark Bumiller 2 years ago
parent
commit
4d44d58306
  1. 37
      __main__.py
  2. 22
      defRpdADSBX.py

37
__main__.py

@ -190,36 +190,21 @@ try:
print(key, "has", len(sorted_ras[key]), "RAs") print(key, "has", len(sorted_ras[key]), "RAs")
obj.check_new_ras(sorted_ras[key]) obj.check_new_ras(sorted_ras[key])
obj.expire_ra_types() obj.expire_ra_types()
icao_key = 'hex'
from defRpdADSBX import pull_rpdadsbx 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 = {} data_indexed = {}
for planeData in data['ac']: for icao in planes:
data_indexed[planeData[icao_key].upper()] = planeData plane = planes[icao]
for key, obj in planes.items(): plane_info = pull_rpdadsbx(icao)
try: if plane_info is not None:
obj.run_adsbx_v2(data_indexed[key.upper()]) data_indexed[icao.upper()] = plane_info['ac']
except KeyError: if data_indexed[icao.upper()]:
obj.run_empty() plane.run_adsbx_v2(data_indexed[icao.upper()])
else: else:
for obj in planes.values(): plane.run_empty()
obj.run_empty()
else: else:
print(f"No data for icao {icao}. Skipping...")
plane.run_empty()
if not data_indexed:
failed_count += 1 failed_count += 1
elif source == "OPENS": elif source == "OPENS":
from defOpenSky import pull_opensky from defOpenSky import pull_opensky

22
defRpdADSBX.py

@ -1,5 +1,4 @@
import requests import requests
import json
import configparser import configparser
from datetime import datetime from datetime import datetime
@ -18,20 +17,8 @@ def pull_rpdadsbx(planes):
} }
try: try:
response = requests.get(url, headers = headers, timeout=30) response = requests.get(url, headers = headers, timeout=30)
except Exception as error: response.raise_for_status()
print('err.args:' + str(error.args)) data = response.json()
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": if "msg" in data.keys() and data['msg'] != "No error":
raise ValueError("Error from ADSBX: msg = ", data['msg']) raise ValueError("Error from ADSBX: msg = ", data['msg'])
if "ctime" in data.keys(): if "ctime" in data.keys():
@ -41,6 +28,7 @@ def pull_rpdadsbx(planes):
data_now = float(data['now']) / 1000.0 data_now = float(data['now']) / 1000.0
print("Data now time:",datetime.utcfromtimestamp(data_now)) print("Data now time:",datetime.utcfromtimestamp(data_now))
print("Current UTC:", datetime.utcnow()) print("Current UTC:", datetime.utcnow())
else:
data = None
return data return data
except Exception as e:
print('Error calling RapidAPI', e)
return None

Loading…
Cancel
Save