11# SPDX-FileCopyrightText: 2023 DJDevon3
22# SPDX-License-Identifier: MIT
33# Coded for Circuit Python 8.1
4- # DJDevon3 ESP32-S3 OpenSkyNetwork_Private__Area_API_Example
4+ # DJDevon3 ESP32-S3 OpenSkyNetwork_Private_Area_API_Example
55
66import os
77import time
1212import circuitpython_base64 as base64
1313import adafruit_requests
1414
15- # OpenSky-Network.org Login required for this API
15+ # OpenSky-Network.org Website Login required for this API
1616# REST API: https://openskynetwork.github.io/opensky-api/rest.html
17- # All active flights JSON: https://opensky-network.org/api/states/all # PICK ONE! :)
1817
1918# Retrieves all traffic within a geographic area (Orlando example)
2019latmin = "27.22" # east bounding box
4140osn_cred = str (osnu ) + ":" + str (osnp )
4241bytes_to_encode = b" " + str (osn_cred ) + " "
4342base64_string = base64 .encodebytes (bytes_to_encode )
44- basepw = repr (base64_string )[2 :- 1 ]
43+ base64cred = repr (base64_string )[2 :- 1 ]
4544
4645Debug_Auth = False # STREAMER WARNING this will show your credentials!
4746if Debug_Auth :
5049 print (repr (bytes_to_encode ))
5150 base64_string = base64 .encodebytes (bytes_to_encode )
5251 print (repr (base64_string )[2 :- 1 ])
53- basepw = repr (base64_string )[2 :- 1 ]
54- print ("Decoded Bytes:" , str (basepw ))
52+ base64cred = repr (base64_string )[2 :- 1 ]
53+ print ("Decoded Bytes:" , str (base64cred ))
5554
5655# OSN requires your username:password to be base64 encoded
5756# so technically it's not transmitted in the clear but w/e
58- osn_header = {"Authorization" : "Basic " + str (basepw )}
57+ osn_header = {"Authorization" : "Basic " + str (base64cred )}
5958
60- # Example of all traffic over Florida, geographic areas cost less per call.
59+ # Example request of all traffic over Florida, geographic areas cost less per call.
6160# https://opensky-network.org/api/states/all?lamin=25.21&lomin=-84.36&lamax=30.0&lomax=-78.40
6261OPENSKY_SOURCE = (
6362 "https://opensky-network.org/api/states/all?"
7170 + lonmax
7271)
7372
74-
75- def time_calc (input_time ):
73+ # Converts seconds to human readable minutes/hours/days
74+ def time_calc (input_time ): # input_time in seconds
7675 if input_time < 60 :
7776 sleep_int = input_time
7877 time_output = f"{ sleep_int :.0f} seconds"
@@ -82,15 +81,11 @@ def time_calc(input_time):
8281 elif 3600 <= input_time < 86400 :
8382 sleep_int = input_time / 60 / 60
8483 time_output = f"{ sleep_int :.1f} hours"
85- elif 86400 <= input_time < 432000 :
84+ else :
8685 sleep_int = input_time / 60 / 60 / 24
8786 time_output = f"{ sleep_int :.1f} days"
88- else : # if > 5 days convert float to int & display whole days
89- sleep_int = input_time / 60 / 60 / 24
90- time_output = f"{ sleep_int :.0f} days"
9187 return time_output
9288
93-
9489def _format_datetime (datetime ):
9590 return "{:02}/{:02}/{} {:02}:{:02}:{:02}" .format (
9691 datetime .tm_mon ,
@@ -101,7 +96,6 @@ def _format_datetime(datetime):
10196 datetime .tm_sec ,
10297 )
10398
104-
10599# Connect to Wi-Fi
106100print ("\n ===============================" )
107101print ("Connecting to WiFi..." )
@@ -117,22 +111,23 @@ def _format_datetime(datetime):
117111
118112while True :
119113 # STREAMER WARNING this will show your credentials!
120- debug_request = False # Set true to see full request
114+ debug_request = False # Set True to see full request
121115 if debug_request :
122116 print ("Full API HEADER: " , str (osn_header ))
123117 print ("Full API GET URL: " , OPENSKY_SOURCE )
124118 print ("===============================" )
125119
126120 print ("\n Attempting to GET OpenSky-Network Data!" )
127121 opensky_response = request .get (url = OPENSKY_SOURCE , headers = osn_header ).json ()
122+
128123 # Print Full JSON to Serial (doesn't show credentials)
129- debug_response = False # Set true to see full response
124+ debug_response = False # Set True to see full response
130125 if debug_response :
131126 dump_object = json .dumps (opensky_response )
132127 print ("JSON Dump: " , dump_object )
133128
134- # Print to Serial
135- osn_debug_keys = True # Set true to print Serial data
129+ # Key:Value Serial Debug (doesn't show credentials)
130+ osn_debug_keys = True # Set True to print Serial data
136131 if osn_debug_keys :
137132 try :
138133 osn_flight = opensky_response ["time" ]
@@ -182,6 +177,5 @@ def _format_datetime(datetime):
182177
183178 except (ConnectionError , ValueError , NameError ) as e :
184179 print ("OSN Connection Error:" , e )
185- print ("You are likely banned for 24 hours" )
186180 print ("Next Retry: " , time_calc (sleep_time ))
187181 time .sleep (sleep_time )
0 commit comments