1
1
# SPDX-FileCopyrightText: 2023 DJDevon3
2
2
# SPDX-License-Identifier: MIT
3
3
# Coded for Circuit Python 8.1
4
- # DJDevon3 ESP32-S3 OpenSkyNetwork_Private__Area_API_Example
4
+ # DJDevon3 ESP32-S3 OpenSkyNetwork_Private_Area_API_Example
5
5
6
6
import os
7
7
import time
12
12
import circuitpython_base64 as base64
13
13
import adafruit_requests
14
14
15
- # OpenSky-Network.org Login required for this API
15
+ # OpenSky-Network.org Website Login required for this API
16
16
# REST API: https://openskynetwork.github.io/opensky-api/rest.html
17
- # All active flights JSON: https://opensky-network.org/api/states/all # PICK ONE! :)
18
17
19
18
# Retrieves all traffic within a geographic area (Orlando example)
20
19
latmin = "27.22" # east bounding box
41
40
osn_cred = str (osnu ) + ":" + str (osnp )
42
41
bytes_to_encode = b" " + str (osn_cred ) + " "
43
42
base64_string = base64 .encodebytes (bytes_to_encode )
44
- basepw = repr (base64_string )[2 :- 1 ]
43
+ base64cred = repr (base64_string )[2 :- 1 ]
45
44
46
45
Debug_Auth = False # STREAMER WARNING this will show your credentials!
47
46
if Debug_Auth :
50
49
print (repr (bytes_to_encode ))
51
50
base64_string = base64 .encodebytes (bytes_to_encode )
52
51
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 ))
55
54
56
55
# OSN requires your username:password to be base64 encoded
57
56
# 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 )}
59
58
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.
61
60
# https://opensky-network.org/api/states/all?lamin=25.21&lomin=-84.36&lamax=30.0&lomax=-78.40
62
61
OPENSKY_SOURCE = (
63
62
"https://opensky-network.org/api/states/all?"
71
70
+ lonmax
72
71
)
73
72
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
76
75
if input_time < 60 :
77
76
sleep_int = input_time
78
77
time_output = f"{ sleep_int :.0f} seconds"
@@ -82,15 +81,11 @@ def time_calc(input_time):
82
81
elif 3600 <= input_time < 86400 :
83
82
sleep_int = input_time / 60 / 60
84
83
time_output = f"{ sleep_int :.1f} hours"
85
- elif 86400 <= input_time < 432000 :
84
+ else :
86
85
sleep_int = input_time / 60 / 60 / 24
87
86
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"
91
87
return time_output
92
88
93
-
94
89
def _format_datetime (datetime ):
95
90
return "{:02}/{:02}/{} {:02}:{:02}:{:02}" .format (
96
91
datetime .tm_mon ,
@@ -101,7 +96,6 @@ def _format_datetime(datetime):
101
96
datetime .tm_sec ,
102
97
)
103
98
104
-
105
99
# Connect to Wi-Fi
106
100
print ("\n ===============================" )
107
101
print ("Connecting to WiFi..." )
@@ -117,22 +111,23 @@ def _format_datetime(datetime):
117
111
118
112
while True :
119
113
# 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
121
115
if debug_request :
122
116
print ("Full API HEADER: " , str (osn_header ))
123
117
print ("Full API GET URL: " , OPENSKY_SOURCE )
124
118
print ("===============================" )
125
119
126
120
print ("\n Attempting to GET OpenSky-Network Data!" )
127
121
opensky_response = request .get (url = OPENSKY_SOURCE , headers = osn_header ).json ()
122
+
128
123
# 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
130
125
if debug_response :
131
126
dump_object = json .dumps (opensky_response )
132
127
print ("JSON Dump: " , dump_object )
133
128
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
136
131
if osn_debug_keys :
137
132
try :
138
133
osn_flight = opensky_response ["time" ]
@@ -182,6 +177,5 @@ def _format_datetime(datetime):
182
177
183
178
except (ConnectionError , ValueError , NameError ) as e :
184
179
print ("OSN Connection Error:" , e )
185
- print ("You are likely banned for 24 hours" )
186
180
print ("Next Retry: " , time_calc (sleep_time ))
187
181
time .sleep (sleep_time )
0 commit comments