the-geeky-codes-high-resolution-logo-color-on-transparent-background geeky code red logo
  • Home
  • AI
    AIShow More
    generate vector icons
    Generate Vector Icons with ChatGPT DALLE 3: A Comprehensive Guide
    14 Min Read
    Dalle 3
    Dalle 3: A Step-by-Step Guide to Mastering AI Art Generation
    4 Min Read
    5 Best AI Tools to Convert Images to Video Animations
    5 Best AI Tools to Convert Images to Video Animations
    8 Min Read
    Exploring the Impressive Mistral 7B Model
    Exploring the Impressive Mistral 7B Model for Text Summarization and Coding
    6 Min Read
    The AI Revolution this week
    Must Read – The AI Revolution this week 30 Sep 2023: Integrating AI Tools into Everyday Life
    6 Min Read
  • Tutorial
    • React js
    • Python
    • Javascript
  • AI Tools
Reading: Discover How Many Astronauts Are in Space and Location with Python
Share
the geeky codesthe geeky codes
Aa
  • AI
  • AI Tools
  • Javascript
  • Python
  • React js
  • Advertise
Search
  • Categories
    • AI
    • AI Tools
    • Javascript
    • Python
    • React js
  • More
    • Advertise
Follow US
Copyright ©2023 The Geeky codes. All Rights Reserved.
the geeky codes > Blog > Tutorial > Python > Discover How Many Astronauts Are in Space and Location with Python
TutorialPython

Discover How Many Astronauts Are in Space and Location with Python

thegeekycodes By thegeekycodes 17 September 2023 5 Min Read
Discover How Many Astronauts Are in Space and Location with Python
SHARE

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.

Contents
Intro – Discover How Many Astronauts Are in Space and Location with PythonGetting the ISS LocationGetting the ISS’s Latitude and LongitudeGetting the ISS’s AddressHow Many Astronauts Are in Space Right Now?Getting the Number of People in SpaceComplete CodeReferences

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/

http://open-notify.org/Open-Notify-API/ISS-Location-Now/

http://api.openweathermap.org

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Twitter Copy Link Print
Previous Article Introduction to YouTube Search Engine Optimization Free Course – Introduction to YouTube Search Engine Optimization(SEO).
Next Article Everything about E-commerce E-commerce (Everything about E-commerce)
Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Twitter Follow
Telegram Follow

Subscribe Now

Subscribe to our newsletter to get our newest articles instantly!

Most Popular
Advanced Routing Techniques in Nextjs 15
Advanced Routing Techniques in Next js 15
20 November 2024
Attachment Details Image-to-Text-Converter-with-Claude-Nextjs-15
Building an AI-Powered Image-to-Text Converter with Claude, Next.js 15, and Vercel AI SDK
20 November 2024
Generate-Dynamic-OpenGraph-Images-in-Nextjs15
How to Generate Dynamic OpenGraph Images in Next.js App Router 15 with TypeScript
20 November 2024
Google Analytics 4 in Nextjs 14
How to Install Google Analytics 4 in Next.js 15 (App Router) with TypeScript [2024]
20 November 2024
docker compose
Getting Started with Docker Compose
20 November 2024

You Might Also Like

Advanced Routing Techniques in Nextjs 15
TutorialNextjs

Advanced Routing Techniques in Next js 15

7 Min Read
Attachment Details Image-to-Text-Converter-with-Claude-Nextjs-15
TutorialNextjs

Building an AI-Powered Image-to-Text Converter with Claude, Next.js 15, and Vercel AI SDK

4 Min Read
Generate-Dynamic-OpenGraph-Images-in-Nextjs15
TutorialNextjs

How to Generate Dynamic OpenGraph Images in Next.js App Router 15 with TypeScript

9 Min Read
Google Analytics 4 in Nextjs 14
TutorialNextjs

How to Install Google Analytics 4 in Next.js 15 (App Router) with TypeScript [2024]

6 Min Read

Always Stay Up to Date

Subscribe to our newsletter to get our newest articles instantly!

the geeky codes geeky code red logo

Providing valuable resources for developers in the form of code snippets, software tutorials, and AI related content.

About

  • About Us
  • Contact
  • Terms and Conditions
  • Privacy Policy
  • Disclaimer
  • Affiliate Disclosure

Resource

  • The Art of AI Prompt Engineering: Crafting Effective Inputs for AI Models

Get the Top 10 in Search!

Looking for a trustworthy service to optimize the company website?
Request a Quote
© 2023 The Geeky Codes. All Rights Reserved
We are happy to see you join Us!

🔥📢Subscribe to our newsletter and never miss our latest code snippets, tutorials and AI updates

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?