import time
import requests
import smtplib
import math
from datetime import datetime
EMAIL = "[email protected]"
PASSWORD = "XXXXX"
MY_LAT = 51.507351 # Your latitude
MY_LONG = -0.127758 # Your longitude
MY_POSITION = (MY_LAT, MY_LONG)
#Your position is within +5 or -5 degrees of the ISS position.
def check_position():
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"])
iss_position = (iss_latitude, iss_longitude)
distance = math.dist(MY_POSITION, iss_position)
if distance < 5:
return True
def send_email():
with smtplib.SMTP("smtp.gmail.com") as connection :
connection.starttls()
connection.login(EMAIL, PASSWORD)
connection.sendmail(
from_addr=EMAIL,
to_addrs="[email protected]",
msg="Subject: Look up☝️\\n\\n The iSS is above you in the sky"
)
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()
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.
# BONUS: run the code every 60 seconds.
while True:
time.sleep(60)
if check_position() and is_night():
send_email()