Created
August 10, 2023 15:10
-
-
Save hiAndrewQuinn/48a80663cd34c6b3a52e52b72155b59e to your computer and use it in GitHub Desktop.
Check 1000 random points on Earth to determine whether the Sun rose on August 9, 2023.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
import requests | |
import random | |
API_URL = "https://api.sunrise-sunset.org/json?lat={}&lng={}&date=2023-08-09" | |
# Change this if you wish to test a different set of points. | |
random.seed(12345) | |
def get_sunrise_data(lat, lng): | |
response = requests.get(API_URL.format(lat, lng)) | |
if response.status_code == 200: | |
return response.json() | |
return None | |
def check_sunrise(data): | |
if data: | |
if "results" in data and "sunrise" in data["results"]: | |
return True | |
return False | |
def generate_random_coords(): | |
return (random.uniform(-90, 90), random.uniform(-180, 180)) | |
if __name__ == "__main__": | |
print("Starting sunrise check...") | |
num_sunrise = 0 | |
total_requests = 1000 | |
for _ in range(total_requests): | |
lat, lng = generate_random_coords() | |
print( | |
"Checking sunrise for ({:.3f}, {:.3f})".format(lat, lng).ljust(40), | |
end="... ", | |
) | |
data = get_sunrise_data(lat, lng) | |
if ( | |
check_sunrise(data) | |
and data["results"]["sunrise"] != data["results"]["sunset"] | |
): | |
print("π Sunrise found!") | |
num_sunrise += 1 | |
elif not check_sunrise(data): | |
print("β Error getting sunrise data. Maybe we're getting rate limited?") | |
time.sleep(10) | |
else: | |
print("βοΈ No sunrise found.") | |
time.sleep(1) # sleep for 10 seconds | |
print( | |
f"Out of {total_requests} random locations, the sun rose in {num_sunrise} locations on August 9, 2023." | |
) |
Author
hiAndrewQuinn
commented
Aug 10, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment