ISS Overhead Notifier
Objectives: Use API to create an ISS tracker.
Libraries: requests, smtplib, datetime
Code: ISS Overhead Notifier
What is API?
“Application Programming Interface, aka API, is a set of commands, functions, protocols, and objects that programmers can use to create software or interact with an external system.” – Angela Yu
API is like a barrier between our program and external system. We follow API’s rules to make a request to the external system. Then it respondes to us and give us the data we requested.
Imagine websites as the restaurant and data that powers the websites as the kitchen. You can’t head straight into the kitchen. Instead, we have menu as an interface between customers and restaurant.
Let’s try to build a Kanye Quotes App.
import requests
response = requests.get(url="https://api.kanye.rest/")
response.raise_for_status()
print(response.json()["quote"])
Build an ISS Tracker
- If the ISS is close to my current location
- And it is dark outside
- then send me an email
- let it run every 60 seconds
We need two functions: is_iss_overhead() and is_night()
def is_iss_overhead():
response = requests.get(url="http://api.open-notify.org/iss-now.json")
response.raise_for_status()
data = response.json()
iss_longitude = float(data["iss_position"]["longitude"])
iss_latitude = float(data["iss_position"]["latitude"])
print("iss location:", iss_longitude, iss_latitude)
# Compare my position - within 5 degrees
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()
sunrise = int(data["results"]["sunrise"].split("T")[1].split(":")[0])
sunset = int(data["results"]["sunset"].split("T")[1].split(":")[0])
# Summer in Boston -4 , Winter would be -5
sunrise = (24 + sunrise - 4) % 24
sunset = (24 + sunset - 4) % 24
time_now = datetime.now().hour
if time_now >= sunset or time_now <= sunrise:
return True
If those two functions return True, send an email tell us to look up.
Lastly, put the script on cloud server and let it run every 60 seconds.