Skip to content

Commit 722ab30

Browse files
committed
Fixing reviewer requested changes
Removed unused timezone vars and assorted script clean up. Changed var for base64cred to be more obvious that a base64 encode is required in the request header.
1 parent f149297 commit 722ab30

3 files changed

+40
-61
lines changed

examples/requests_api_openskynetwork_private.py

+15-19
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
osn_cred = str(osnu) + ":" + str(osnp)
3939
bytes_to_encode = b" " + str(osn_cred) + " "
4040
base64_string = base64.encodebytes(bytes_to_encode)
41-
basepw = repr(base64_string)[2:-1]
41+
base64cred = repr(base64_string)[2:-1]
4242

4343
Debug_Auth = False # STREAMER WARNING this will show your credentials!
4444
if Debug_Auth:
@@ -47,16 +47,17 @@
4747
print(repr(bytes_to_encode))
4848
base64_string = base64.encodebytes(bytes_to_encode)
4949
print(repr(base64_string)[2:-1])
50-
basepw = repr(base64_string)[2:-1]
51-
print("Decoded Bytes:", str(basepw))
50+
base64cred = repr(base64_string)[2:-1]
51+
print("Decoded Bytes:", str(base64cred))
5252

53-
# OSN requires your username:password to be base64 encoded
54-
# so technically it's not transmitted in the clear but w/e
55-
osn_header = {"Authorization": "Basic " + str(basepw)}
53+
# Requests URL - icao24 is their endpoint required for a transponder
54+
# example https://opensky-network.org/api/states/all?icao24=a808c5
55+
# OSN private requires your username:password to be base64 encoded
56+
osn_header = {"Authorization": "Basic " + str(base64cred)}
5657
OPENSKY_SOURCE = "https://opensky-network.org/api/states/all?" + "icao24=" + transponder
5758

58-
59-
def time_calc(input_time):
59+
# Converts seconds to human readable minutes/hours/days
60+
def time_calc(input_time): # input_time in seconds
6061
if input_time < 60:
6162
sleep_int = input_time
6263
time_output = f"{sleep_int:.0f} seconds"
@@ -66,15 +67,11 @@ def time_calc(input_time):
6667
elif 3600 <= input_time < 86400:
6768
sleep_int = input_time / 60 / 60
6869
time_output = f"{sleep_int:.1f} hours"
69-
elif 86400 <= input_time < 432000:
70+
else:
7071
sleep_int = input_time / 60 / 60 / 24
7172
time_output = f"{sleep_int:.1f} days"
72-
else: # if > 5 days convert float to int & display whole days
73-
sleep_int = input_time / 60 / 60 / 24
74-
time_output = f"{sleep_int:.0f} days"
7573
return time_output
7674

77-
7875
def _format_datetime(datetime):
7976
return "{:02}/{:02}/{} {:02}:{:02}:{:02}".format(
8077
datetime.tm_mon,
@@ -85,7 +82,6 @@ def _format_datetime(datetime):
8582
datetime.tm_sec,
8683
)
8784

88-
8985
# Connect to Wi-Fi
9086
print("\n===============================")
9187
print("Connecting to WiFi...")
@@ -101,22 +97,23 @@ def _format_datetime(datetime):
10197

10298
while True:
10399
# STREAMER WARNING this will show your credentials!
104-
debug_request = False # Set true to see full request
100+
debug_request = False # Set True to see full request
105101
if debug_request:
106102
print("Full API HEADER: ", str(osn_header))
107103
print("Full API GET URL: ", OPENSKY_SOURCE)
108104
print("===============================")
109105

110106
print("\nAttempting to GET OpenSky-Network Data!")
111107
opensky_response = request.get(url=OPENSKY_SOURCE, headers=osn_header).json()
108+
112109
# Print Full JSON to Serial (doesn't show credentials)
113-
debug_response = False # Set true to see full response
110+
debug_response = False # Set True to see full response
114111
if debug_response:
115112
dump_object = json.dumps(opensky_response)
116113
print("JSON Dump: ", dump_object)
117114

118-
# Print to Serial
119-
osn_debug_keys = True # Set true to print Serial data
115+
# Key:Value Serial Debug (doesn't show credentials)
116+
osn_debug_keys = True # Set True to print Serial data
120117
if osn_debug_keys:
121118
try:
122119
osn_flight = opensky_response["time"]
@@ -148,6 +145,5 @@ def _format_datetime(datetime):
148145

149146
except (ConnectionError, ValueError, NameError) as e:
150147
print("OSN Connection Error:", e)
151-
print("You are likely banned for 24 hours")
152148
print("Next Retry: ", time_calc(sleep_time))
153149
time.sleep(sleep_time)

examples/requests_api_openskynetwork_private_area.py

+15-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
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

66
import os
77
import time
@@ -12,9 +12,8 @@
1212
import circuitpython_base64 as base64
1313
import 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)
2019
latmin = "27.22" # east bounding box
@@ -41,7 +40,7 @@
4140
osn_cred = str(osnu) + ":" + str(osnp)
4241
bytes_to_encode = b" " + str(osn_cred) + " "
4342
base64_string = base64.encodebytes(bytes_to_encode)
44-
basepw = repr(base64_string)[2:-1]
43+
base64cred = repr(base64_string)[2:-1]
4544

4645
Debug_Auth = False # STREAMER WARNING this will show your credentials!
4746
if Debug_Auth:
@@ -50,14 +49,14 @@
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
6261
OPENSKY_SOURCE = (
6362
"https://opensky-network.org/api/states/all?"
@@ -71,8 +70,8 @@
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-
9489
def _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
106100
print("\n===============================")
107101
print("Connecting to WiFi...")
@@ -117,22 +111,23 @@ def _format_datetime(datetime):
117111

118112
while 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("\nAttempting 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)

examples/requests_api_openskynetwork_public.py

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# SPDX-FileCopyrightText: 2023 DJDevon3
22
# SPDX-License-Identifier: MIT
33
# Coded for Circuit Python 8.1
4-
"""DJDevon3 Adafruit Feather ESP32-S3 OpenSkyNetwork_API_Example"""
4+
# Adafruit Feather ESP32-S3 OpenSkyNetwork_Public_API_Example
55
import os
66
import time
77
import ssl
@@ -10,36 +10,32 @@
1010
import socketpool
1111
import adafruit_requests
1212

13-
# No developer account necessary for this API
13+
# No login necessary for Public API. Drastically reduced daily limit vs Private
1414
# OpenSky-Networks.org REST API: https://openskynetwork.github.io/opensky-api/rest.html
15-
# All active flights JSON: https://opensky-network.org/api/states/all
15+
# All active flights JSON: https://opensky-network.org/api/states/all PICK ONE!
1616
# JSON order: transponder, callsign, country
17-
# ACTIVE transponder you want data from
17+
# ACTIVE transpondes only, for multiple "c822af&icao24=cb3993&icao24=c63923"
1818
transponder = "ab1644"
1919

2020
# Initialize WiFi Pool (There can be only 1 pool & top of script)
2121
pool = socketpool.SocketPool(wifi.radio)
2222

2323
# Time between API refreshes
2424
# 900 = 15 mins, 1800 = 30 mins, 3600 = 1 hour
25-
# OpenSky-Networks will temp ban your IP for too many requests, there is a public rate limit.
25+
# OpenSky-Networks will temp ban your IP for too many daily requests.
2626
# https://openskynetwork.github.io/opensky-api/rest.html#limitations
2727
sleep_time = 1800
2828

29-
# this example uses settings.toml for credentials
30-
# timezone offset is in seconds plus or minus GMT
29+
# Wifi credentials pulled from settings.toml
3130
ssid = os.getenv("AP_SSID")
3231
appw = os.getenv("AP_PASSWORD")
33-
timezone = os.getenv("timezone")
34-
tz_offset_seconds = os.getenv("timezone_offset")
3532

36-
# https://opensky-network.org/api/states/all
33+
# Requests URL - icao24 is their endpoint required for a transponder
3734
# example https://opensky-network.org/api/states/all?icao24=a808c5
38-
# You can use states/own to pull your owned craft data without rate limit.
3935
OPENSKY_SOURCE = "https://opensky-network.org/api/states/all?" + "icao24=" + transponder
4036

41-
42-
def time_calc(input_time):
37+
# Converts seconds to human readable minutes/hours/days
38+
def time_calc(input_time): # input_time in seconds
4339
if input_time < 60:
4440
sleep_int = input_time
4541
time_output = f"{sleep_int:.0f} seconds"
@@ -49,15 +45,11 @@ def time_calc(input_time):
4945
elif 3600 <= input_time < 86400:
5046
sleep_int = input_time / 60 / 60
5147
time_output = f"{sleep_int:.1f} hours"
52-
elif 86400 <= input_time < 432000:
48+
else:
5349
sleep_int = input_time / 60 / 60 / 24
5450
time_output = f"{sleep_int:.1f} days"
55-
else: # if > 5 days convert float to int & display whole days
56-
sleep_int = input_time / 60 / 60 / 24
57-
time_output = f"{sleep_int:.0f} days"
5851
return time_output
5952

60-
6153
def _format_datetime(datetime):
6254
return "{:02}/{:02}/{} {:02}:{:02}:{:02}".format(
6355
datetime.tm_mon,
@@ -68,7 +60,6 @@ def _format_datetime(datetime):
6860
datetime.tm_sec,
6961
)
7062

71-
7263
# Connect to Wi-Fi
7364
print("\n===============================")
7465
print("Connecting to WiFi...")
@@ -122,7 +113,6 @@ def _format_datetime(datetime):
122113
print("Flight Country: ", country)
123114
else:
124115
print("This flight has no active data or you're polling too fast.")
125-
print("You will eventually get temp banned for polling too fast!")
126116
print(
127117
"Read: https://openskynetwork.github.io/opensky-api/rest.html#limitations"
128118
)
@@ -138,6 +128,5 @@ def _format_datetime(datetime):
138128

139129
except (ConnectionError, ValueError, NameError) as e:
140130
print("OSN Connection Error:", e)
141-
print("You are likely banned for 24 hours")
142131
print("Next Retry: ", time_calc(sleep_time))
143132
time.sleep(sleep_time)

0 commit comments

Comments
 (0)