Who doesn’t like those mesmerizing images of space!
National Aeronautics and Space Administration (NASA) provides amazing astronomical images through the Astronomy Picture of the Day service. In this tutorial, we are going to use NASA API and nasa.py library to fetch these images.
An application-programming interface (API) is a set of instructions and standards used to access a web application. APIs are a very useful tool that allow us to share information publicly. The best way to learn how to use APIs it is to program yourself.
Today, we will learn how to download and display the daily astronomical images. First, we are going to fetch today’s. image. Then, we’ll make a few changes to our code so that we automatically get an image of any date.
Here is what we’ll cover today:
APIs are used for writing business logic. This course teaches you how to write API and integration tests from scratch.
Learn REST and SOAP API Test Automation in Java
In the following program. we are going to download today’s Astronomy Picture of the Day(APOD) using NASA’s Open API. The program will download image files only. If there is no image file available for today’s date, it will simply print a message that says, “Image not available”.
If the image file is available, it will be downloaded on the local system. Then, our code will fetch the image from the local system and display it along with any extra information (i.e. title, url).
Next, our code will ask the user if they want to hear an audio explanation for the image. If the user chooses to hear the audio explanation, we will use the Google Text to Speech (GTTS) library to convert the text explanation to an audio file and play it.
Note: To access the services provided by NASA, we need to have a registered api key. To get an api key, we simply have to fill out a form on the NASA API website. We can also use a
demo_key
.We can place 1000 requests per hour by registered api, and with
demo_key
, we can only place 50 requests per day.After successful registration, you’ll get an api key in the mentioned email address. We’ll use this key later in this tutorial.
Our Aqua satellite captured this composite visible and infrared image on Sep. 29, 2020, which shows that fires and smoke continue to dominate the landscape of the western U.S.
#Import required libraries:import nasapyimport osfrom datetime import datetimeimport urllib.requestfrom IPython.display import Image,display,Audiofrom gtts import gTTS
nasapy
: We are going to use the nasapy library to access the information provided by NASA’s API.os
: To store the image in a particular directory, we use the os library.datetime
: We use a date while sending a request to NASA API to access the information. To get a date in a specific format, we’ll use a datetime library.urllib
: The image we get from NASA API will be in a form of a URL, so we use urllib to fetch it.Ipython
: We need to display the image in a jupyter notebook using Ipython library. We will also play the audio explanation of the image using the audio module.gtts
: We use Google Text to Speech library to convert the text explanation of the image into an audio file.Here, we are going to create an object called nasa
. We are using the nasapy
library and our registered api key. We’ll use this object to call a method of the Nasa
class to get the information on astronomical images.
#Initialize Nasa class by creating an object:k = "523p5hPYHGzafYGLCkqa54kKMTV2vbP0XcPxkcLm"nasa = nasapy.Nasa(key = k)
Now to get an image for a specific date, we need to pass a date in a specific format. The required format for the date is YYYY-MM-DD
using the datetime.today()
function. So, we need to convert this tuple into a string using the strftime(%Y-%m-%d)
function.
#Get today's date in YYYY-MM-DD format:d = datetime.today().strftime('%Y-%m-%d')
Now that we have today’s date in the proper format, we can place a request for today’s astronomical image. Here we use the picture_of_the_day()
method to get the image data.
#Get the image data:apod = nasa.picture_of_the_day(date=d, hd=True)
Our apod variable is a dictionary of various keys and values. Let’s take a look at the keys of this variable:
date
title
copyright
explanation
url
hdurl
media_type
service_version
#POINT A:#Check the media type available:if(apod["media_type"] == "image"):#POINT B:#Displaying hd images only:if("hdurl" in apod.keys()):#POINT C:#Saving name for image:title = d + "_" + apod["title"].replace(" ","_").replace(":","_") + ".jpg"#POINT D:#Path of the directory:image_dir = "./Astro_Images"#Checking if the directory already exists?dir_res = os.path.exists(image_dir)#If it doesn't exist then make a new directory:if (dir_res==False):os.makedirs(image_dir)#If it exist then print a statement:else:print("Directory already exists!\n")#POINT E:#Retrieving the image:urllib.request.urlretrieve(url = apod["hdurl"] , filename = os.path.join(image_dir,title))#POINT F:#Displaying information related to image:if("date" in apod.keys()):print("Date image released: ",apod["date"])print("\n")if("copyright" in apod.keys()):print("This image is owned by: ",apod["copyright"])print("\n")if("title" in apod.keys()):print("Title of the image: ",apod["title"])print("\n")if("explanation" in apod.keys()):print("Description for the image: ",apod["explanation"])print("\n")if("hdurl" in apod.keys()):print("URL for this image: ",apod["hdurl"])print("\n")#POINT G:#Displaying main image:display(Image(os.path.join(image_dir,title)))#Point H:#Text to Speech Conversion:#Take input from user:print("\n")choice = input("Press * to hear the audio explanation : ")if(choice=="*"):#Text to be converted:mytext = apod["explanation"]#mytext="Good Evening Pratik."#Creating an object:myobj = gTTS(text=mytext, lang="en", slow=False)#Generating audio file name:audio_title = d + "_" + apod["title"] + ".mp3"#Save the converted file:myobj.save(os.path.join(image_dir,audio_title))#Name of sound file:sound_file = os.path.join(image_dir,audio_title)# Playing the converted filedisplay(Audio(sound_file, autoplay=True))#POINT I:#If media type is not image:else:print("Sorry, Image not available!")
Now we will understand the code block by block.
Displaying Images:
Creating Directory:
.exist()
function. If the directory doesn’t exist, then we create it using .makedirs()
function.urllib.request.urlretrieve()
function.os.path.join()
to store the image in the newly created directory with a specified title name.apod[“hdurl”]
to download the image.Display other information:
Image()
function of the Ipython library.Audio Explanation:
*
, we’ll use Google Text to Speech (gtts
) library to convert the text into audio file..mp3
extension.Audio()
function to play the audio file in our jupyter notebook.Please run the following Google Collaboratory file to view proper output. You can download and run this code file online on Google Collaboratory.
But what if the user wants to get an image for a specific date? It’s very simple! Instead of using today’s date, we’ll get the date from the user in the specified format and pass that as an argument. That is the only difference in the code. Check out the complete below.
#Import required libraries:import nasapyimport osfrom datetime import datetimeimport urllib.requestfrom IPython.display import Image,display,Audiofrom gtts import gTTS#Initialize Nasa class by creating an object:nasa = nasapy.Nasa(key="523p5hPYHGzafYGLCkqa54kKMTV2vbP0XcPxkcLm")#Get today's date in YYYY-MM-DD format:d = input("Enter date in YYYY-MM-DD format : ")#Get the image data:apod = nasa.picture_of_the_day(date=d, hd=True)#POINT A:#Check the media type available:if(apod["media_type"] == "image"):#POINT B:#Displaying hd images only:if("hdurl" in apod.keys()):#POINT C:#Saving name for image:title = d + "_" + apod["title"].replace(" ","_").replace(":","_") + ".jpg"#POINT D:#Path of the directory:image_dir = "Astro_Images"#Checking if the directory already exists?dir_res = os.path.exists(image_dir)#If it doesn't exist then make a new directory:if (dir_res==False):os.makedirs(image_dir)#If it exist then print a statement:else:print("Directory already exists!\n")#POINT E:#Retrieving the image:urllib.request.urlretrieve(url = apod["hdurl"] , filename = os.path.join(image_dir,title))#POINT F:#Displaying information related to image:if("date" in apod.keys()):print("Date image released: ",apod["date"])print("\n")if("copyright" in apod.keys()):print("This image is owned by: ",apod["copyright"])print("\n")if("title" in apod.keys()):print("Title of the image: ",apod["title"])print("\n")if("explanation" in apod.keys()):print("Description for the image: ",apod["explanation"])print("\n")if("hdurl" in apod.keys()):print("URL for this image: ",apod["hdurl"])print("\n")#POINT G:#Displaying main image:display(Image(os.path.join(image_dir,title)))#Point H:#Text to Speech Conversion:#Take input from user:print("\n")choice = input("Press * to hear the audio explanation : ")if(choice=="*"):#Text to be converted:mytext = apod["explanation"]#mytext="Good Evening Pratik."#Creating an object:myobj = gTTS(text=mytext, lang="en", slow=False)#Generating audio file name:audio_title = d + "_" + apod["title"] + ".mp3"#Save the converted file:myobj.save(os.path.join(image_dir,audio_title))#Name of sound file:sound_file = os.path.join(image_dir,audio_title)# Playing the converted filedisplay(Audio(sound_file, autoplay=True))#POINT I:#If media type is not image:else:print("Sorry, Image not available!")
Please run the following Google Collaboratory file to view proper output. You can download and run this code file online on Google Collaboratory.
Now that we know how to download and display an image for a particular date, it’s time we push things a little further. We will download images for a range of dates and display a random image from our downloads. Let’s understand how we can achieve this below.
These are the same libraries we used previously.
These are the same libraries we used previously.#Import required libraries:import nasapyimport osimport pandas as pdimport urllib.requestfrom IPython.display import Image,display
Here we are going to create an object called nasa
. We are using the nasapy library and our registered api key. We’ll use this object to call a method of Nasa class to get the information on our astronomical images.
#Initialize Nasa class by creating an object:k = "523p5hPYHGzafYGLCkqa54kKMTV2vbP0XcPxkcLm"nasa = nasapy.Nasa(key = k)
We want to download more than one image for more than one date. So, we are going to use the date_range()
function of the pandas library. This function will give us the list of dates in required format. Here, the period value determines the range of the list.
We are going to download 5 images, but you can download as many as you want by adjusting the periods and end values.
#Get a list of dates:dates = pd.date_range(end = '2020-08-15', periods=5)
In the previous case, we only had one image. But here, we are going to download many images. To do so, we are creating an empty list to store all the values for all the images as a dictionary.
First of all, we’ll take the date value and store it in the apod variable. Then we are going to get the required information as a dictionary and append it in the list.
#Empty list to store dictionary keys and values:data = []#Getting all the data:for d in dates:apod = nasa.picture_of_the_day(d, hd=True)if apod['media_type'] == 'image':if 'hdurl' in apod.keys():data.append({'date':apod['date'], 'title': apod['title'],'hdurl': apod['hdurl']})
Now we are going to create a directory to store images as we did previously.
#Path of the directory:image_dir = "AAA_Images_1"#Checking if the directory already exists?dir_res = os.path.exists(image_dir)#If it doesn't exist then make a new directory:if (dir_res==False):os.makedirs(image_dir)#If it exist then print a statement:else:print("Directory already exists!\n")
Now that we have all the information of the image and directory, we are going to download the images. We use a for loop
to loop through all the data in our list.
#Retrieving the image:for img in data:#Creating title for image:title = img["date"]+"_"+img["title"].replace(" ","_").replace(":","_")+".jpg"#Downloading the image:urllib.request.urlretrieve(img['hdurl'],os.path.join(image_dir,title))
Now that we have downloaded all the images, it’s time to display them. There will be many images in the directory, so we can get a list of images using the following code.
#Get a list of images:astro_images = os.listdir(image_dir)
Now we can easily display an image using Image()
. We are going to use os.path.join()
to find the image location. Here we are going to display the image from the list of images available.
#Displaying an image:Image(os.path.join(image_dir, astro_images[0]))
We can use the random library of python to get a random image from the list of images available.
#Get random image:import randomImage(os.path.join(image_dir, astro_images[random.randint(0,len(astro_images)-1)]))
#Import required libraries:import nasapyimport osimport pandas as pdimport urllib.requestfrom IPython.display import Image,display#Initialize Nasa class by creating an object:k = "523p5hPYHGzafYGLCkqa54kKMTV2vbP0XcPxkcLm"nasa = nasapy.Nasa(key = k)#Get a list of dates:dates = pd.date_range(end = '2020-08-15', periods=5)#Empty list to store dictionary keys and values:data = []#Getting all the data:for d in dates:apod = nasa.picture_of_the_day(d, hd=True)if apod['media_type'] == 'image':if 'hdurl' in apod.keys():data.append({'date':apod['date'], 'title': apod['title'],'hdurl': apod['hdurl']})#Path of the directory:image_dir = "AAA_Images_1"#Checking if the directory already exists?dir_res = os.path.exists(image_dir)#If it doesn't exist then make a new directory:if (dir_res==False):os.makedirs(image_dir)#If it exist then print a statement:else:print("Directory already exists!\n")#Retrieving the image:for img in data:#Creating title for image:title = img["date"]+"_"+img["title"].replace(" ","_").replace(":","_")+".jpg"#Downloading the image:urllib.request.urlretrieve(img['hdurl'], os.path.join(image_dir,title))#Get list of images:astro_images = os.listdir(image_dir)
#Displaying an image:Image(os.path.join(image_dir, astro_images[0]))
#Get random image:import randomImage(os.path.join(image_dir, astro_images[random.randint(0,len(astro_images)-1)]))
Please run the following Google Collaboratory file to view proper output. You can download and run this code file online on Google Collaboratory.
Congratulations of completing this fun activity with NASA’s API and Python. I hope this activity taught you some new things you can do with Python and inspired you to keep learning.
In the next part of this tutorial, we’ll see how we can send the downloaded images from email.
To download all the jupyter notebooks used in this tutorial, click here. If you have any doubts, questions, or thoughts regarding this article, feel free to contact me at shuklapratik22@gmail.com
If you want to continue learning abut APIs and want to take these skills to the next level, check out Educative’s course Learn REST and SOAP API Test Automation in Java. You will learn how to perform REST and SOAP API test automation and learn how to write APIs and integration tests from scratch.
Happy learning!
Free Resources