From d36f9109f35438100810b5f4febd1e17cf32cdc4 Mon Sep 17 00:00:00 2001 From: neubrom Date: Wed, 19 Jan 2022 22:34:21 +0100 Subject: [PATCH] Day 33 --- 33/issoverhead-start/main.py | 68 +++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 28 deletions(-) diff --git a/33/issoverhead-start/main.py b/33/issoverhead-start/main.py index 445d398..d194aeb 100644 --- a/33/issoverhead-start/main.py +++ b/33/issoverhead-start/main.py @@ -1,37 +1,49 @@ import requests from datetime import datetime - -MY_LAT = 51.507351 # Your latitude -MY_LONG = -0.127758 # Your longitude - -response = requests.get(url="http://api.open-notify.org/iss-now.json") -response.raise_for_status() -data = response.json() - -iss_latitude = float(data["iss_position"]["latitude"]) -iss_longitude = float(data["iss_position"]["longitude"]) - -#Your position is within +5 or -5 degrees of the ISS position. - - -parameters = { - "lat": MY_LAT, - "lng": MY_LONG, - "formatted": 0, -} - -response = requests.get("https://api.sunrise-sunset.org/json", params=parameters) -response.raise_for_status() -data = response.json() -sunrise = int(data["results"]["sunrise"].split("T")[1].split(":")[0]) -sunset = int(data["results"]["sunset"].split("T")[1].split(":")[0]) - -time_now = datetime.now() +import time +import smtplib + +MY_LAT = 51.507351 # Your latitude +MY_LONG = -0.127758 # Your longitude + + +def is_iss_here(): + response = requests.get(url="http://api.open-notify.org/iss-now.json") + response.raise_for_status() + data = response.json() + + iss_latitude = float(data["iss_position"]["latitude"]) + iss_longitude = float(data["iss_position"]["longitude"]) + + # Your position is within +5 or -5 degrees of the ISS position. + if MY_LONG - 5 <= iss_longitude <= MY_LONG + 5 and MY_LAT - 5 <= iss_latitude <= MY_LAT + 5: + return True + + +def is_night(): + parameters = { + "lat": MY_LAT, + "lng": MY_LONG, + "formatted": 0, + } + response = requests.get("https://api.sunrise-sunset.org/json", params=parameters) + response.raise_for_status() + data = response.json() + print(data) + sunrise = int(data["results"]["sunrise"].split("T")[1].split(":")[0]) + sunset = int(data["results"]["sunset"].split("T")[1].split(":")[0]) + time_now = datetime.now().hour + if time_now >= sunset or time_now <= sunrise: + return True #If the ISS is close to my current position # and it is currently dark -# Then send me an email to tell me to look up. +# Then print message to tell me to look up. # BONUS: run the code every 60 seconds. +while True: + time.sleep(60) + if is_iss_here() and is_night(): + print("Look up! ISS is here")