Intro – Discover How Many Astronauts Are in Space and Location with Python
In this article, we will explore how to find out how many people are currently in space and even track the International Space Station’s (ISS) whereabouts, all from the comfort of your own home.
Getting the ISS Location
To start, we’ll need to use Python, a versatile programming language. The script uses the requests
library to make a web request to an API that provides the ISS’s current location. Here’s a snippet of the code:
Getting the ISS’s Latitude and Longitude
import requests
def get_iss_pass_times():
url = "http://api.open-notify.org/iss-now.json"
response = requests.get(url)
data = response.json()
return data
pass_times = get_iss_pass_times()
print(f'Latitude: {pass_times["iss_position"]["latitude"]}, Longitude: {pass_times["iss_position"]["longitude"]}')
This code fetches the ISS’s latitude and longitude, effectively showing you where it is in real-time.
Getting the ISS’s Address
To achieve this, we modify the get_address_from_lat_lng
function to use the OpenWeatherMap API:
import requests
def get_address_from_lat_lng(latitude, longitude, api_key):
base_url = "http://api.openweathermap.org/geo/1.0/reverse"
params = {
"lat": latitude,
"lon": longitude,
"limit": 1, # You can adjust the limit as needed (number of results)
"appid": api_key,
}
try:
response = requests.get(base_url, params=params)
data = response.json()
if data and "name" in data[0]:
location_name = data[0]["name"]
return location_name
else:
return "I am over the sea"
except Exception as e:
return f"Error: {str(e)}"
# Replace 'YOUR_API_KEY' with your actual OpenWeatherMap API key
api_key = 'YOUR_API_KEY'
latitude = 37.7749 # Replace with your latitude
longitude = -122.4194 # Replace with your longitude
address = get_address_from_lat_lng(latitude, longitude, api_key)
print("Current address:", address)
This code fetches and displays the earthly location of the ISS based on its latitude and longitude coordinates.
How Many Astronauts Are in Space Right Now?
Tracking the ISS is fascinating, but how about knowing how many astronauts are currently aboard their names, and the spacecraft they are on? Fortunately, there’s another API that provides this information.
Getting the Number of People in Space
To achieve this, we again use Python and the requests
library to interact with the Open Notify API. The following code segment retrieves the number of people in space and their details:
import requests
def get_people_in_space():
api_url = "http://api.open-notify.org/astros.json"
response = requests.get(api_url)
if response.status_code == 200:
data = response.json()
number_of_people = data["number"]
people_in_space = data["people"]
print(f"There are currently {number_of_people} people in space.")
print("List of people in space:")
for person in people_in_space:
name = person["name"]
spacecraft = person["craft"]
print(f"- {name} is aboard {spacecraft}")
else:
print("Failed to retrieve data from the API.")
get_people_in_space()
This code fetches and displays the number of astronauts currently in space and their respective spacecraft and names.
💁 Check out our other articles😃
👉 Generate a free Developer Portfolio website with AI prompts
👉 Creating a Toggle Switcher with Happy and Sad Faces using HTML, CSS, and JavaScript
Complete Code
import requests
# http://api.openweathermap.org/geo/1.0/reverse?lat={lat}&lon={lon}&limit={limit}&appid={API key}
def get_address_from_lat_lng(latitude, longitude, api_key):
base_url = "http://api.openweathermap.org/geo/1.0/reverse"
params = {
"lat": latitude,
"lon": longitude,
"limit": 1, # You can adjust the limit as needed (number of results)
"appid": api_key,
}
try:
response = requests.get(base_url, params=params)
data = response.json()
if data and "name" in data[0]:
location_name = data[0]["name"]
return location_name
else:
return "I am over the sea"
except Exception as e:
return f"Error: {str(e)}"
# Function to get ISS pass times for your location
def get_iss_pass_times():
url = f"http://api.open-notify.org/iss-now.json"
response = requests.get(url)
data = response.json()
return data
# Define the API URL
api_url = "http://api.open-notify.org/astros.json"
def get_people_in_space():
# Send a GET request to the API
response = requests.get(api_url)
# Check if the request was successful (status code 200)
if response.status_code == 200:
# Parse the JSON response
data = response.json()
# Extract the number of people in space
number_of_people = data["number"]
# Extract the names and spacecraft of the people in space
people_in_space = data["people"]
print(f"There are currently {number_of_people} people in space.")
print("List of people in space:")
for person in people_in_space:
name = person["name"]
spacecraft = person["craft"]
print(f"- {name} is aboard {spacecraft}")
else:
print("Failed to retrieve data from the API.")
pass_times = get_iss_pass_times()
print(
f'Latitude: {pass_times["iss_position"]["latitude"]}, Longitude: {pass_times["iss_position"]["longitude"]}'
)
get_people_in_space()
# Replace 'YOUR_API_KEY' with your actual OpenWeatherMap API key
api_key = "xxx-xxxx-xxxx-xxxx-xxxx"
latitude = pass_times["iss_position"]["latitude"] # Replace with your latitude
longitude = pass_times["iss_position"]["longitude"] # Replace with your longitude
address = get_address_from_lat_lng(latitude, longitude, api_key)
print("Current address:", address)
References
http://open-notify.org/Open-Notify-API/People-In-Space/