This project contains several scripts for downloading images from NASA's Astronomy Picture of the Day (APOD), NASA's Earth Polychromatic Imaging Camera (EPIC), and recent SpaceX launches. Downloaded images can be published in the Telegram channel on demand or at a certain frequency. The utility script common_scripts.py
contains utility functions for creating folders, getting file extensions, and formatting the epic NASA date.
- Obtain your NASA API key at https://api.nasa.gov/.
- Get your Telegram API key by creating a new bot using BotFather.
- Clone the repository and create a
.env
file in the project root directory with the following content:
NASA_API_TOKEN=your_nasa_api_token
TELEGRAM_BOT_TOKEN=your_telegram_bot_token
FREQUENSY_OF_PUBLICATION=hours
TELEGRAM_CHAT_ID=your telegram chat_id
- Python3 must be installed on your computer. Then use
pip
(orpip3
if there is a conflict with Python2) to install the dependencies:
pip install -r requirements.txt
This script downloads images from NASA's Astronomy Picture of the Day (APOD) and saves them to the local `'images'' folder. If there is no such folder, it creates it.
fetch_nasa_apod(token, count_images)
: The main script function that accepts the following parameters:token
: Your NASA API key.count
: Number of images to upload (default: 3). The script downloads a random selection of images if the number is not specified.
The script can be run from the console using the command:
python fetch_nasa_apod_images.py [--count COUNT]
--count COUNT:
(optional) Number of images to upload. By default, 3 images are loaded.
This script imports and uses functions from separate scripts as follows:
- It uses the
download_image
function from thecommon_scripts.py
script to save downloaded images. - To get the file extension, it uses the
get_extension
function from thecommon_scripts.py
script.
This script downloads images from NASA's Earth Polychromatic Imaging Camera (EPIC) and saves them to the local `'images'' folder. If there is no such folder, it creates it.
fetch_nasa_epic(token)
: The main script function that accepts the following parameters:token
: Your NASA API key.
The script can be run from the console using the command:
python fetch_nasa_epic_images.py
This script does not accept command line arguments.
This script imports and uses functions from separate scripts as follows:
- It uses the
download_image
function from thecommon_scripts.py
script to save downloaded images. - To get the date from NASA EPIC
get_date_from_epic
from the scriptcommon_scripts.py
.
This script downloads images associated with the latest SpaceX launch or specified launch and saves them to the local 'images'
folder. If there is no such folder, it creates it.
fetch_spacex_images(launch_id=None)
: The main script function that accepts the following parameters:launch_id
: (optional) The ID of the SpaceX launch for which images need to be loaded. If not specified, images from the last run will be loaded.
The script can be run from the console using the command:
python fetch_spacex_images.py [--id LAUNCH_ID]
--id LAUNCH_ID
: (optional) The ID of the SpaceX launch for which images need to be loaded. If not specified, images from the last run will be loaded.
This script imports and uses functions from separate scripts as follows:
- It uses the
download_image
function from thecommon_scripts.py
script to save downloaded images.
This script combines the functionality of the fetch_spacex_images.py
, fetch_nasa_apod_images.py
and fetch_nasa_epic_images.py
scripts. It downloads images from SpaceX, NASA APOD and NASA EPIC and saves them to a local ``images'` folder. If there is no such folder, it creates it.
The script can be run from the console using the command:
python fetch_all_images.py [--id LAUNCH_ID] [--count COUNT]
--id LAUNCH_ID
: (optional) The ID of the SpaceX launch for which images need to be loaded. If not specified, images from the last run will be loaded.--count COUNT
: (optional) Number of NASA APOD images to load. If not specified, the default value of 3 will be used.
This script imports and uses functions from separate scripts as follows:
fetch_spacex_images(args.id)
from thefetch_spacex_images.py
script.fetch_nasa_apod(token, args.count)
from thefetch_nasa_apod_images.py
script.fetch_nasa_epic(token)
from thefetch_nasa_epic_images.py
script.
This script publishes one image to a specified Telegram channel. The image can be selected randomly from a folder or specified by name.
publish_image(token, chat_id, folder_name, image_name)
: The main script function that accepts the following parameters:token
: Your Telegram API token.chat_id
: Unique identifier of the Telegram channel in which the image will be published.folder_name
: (optional) The name of the folder containing the images. If not specified,images
will be used by default.image_name
: (optional) Name of the image to post. If not specified, a random image from the folder will be published.
The script can be run from the console using the command:
python publish_images_bot.py [--folder_name FOLDER_NAME] [--image_name IMAGE_NAME]
--folder_name FOLDER_NAME
: (optional) The name of the folder containing the images. If not specified, the default folder name 'images' will be used.--image_name IMAGE_NAME
: (optional) Name of the image to post. If not specified, a random image from the folder will be published.
This script publishes images to a specified Telegram channel at specific intervals. Images are randomly selected from the folder and published at a specified frequency.
autopublish_images(token, chat_id, folder_name, frequensy_of_publication)
: The main script function that accepts the following parameters:token
: Your Telegram API token.chat_id
: Unique identifier of the Telegram channel in which the images will be published.folder_name
: Name of the folder containing the images. The default isimages
.frequensy_of_publication
: Publication frequency in hours. By default, once every 4 hours. The frequency can be changed in the.env
file.
The script can be run from the console using the following command:
python autopublish_images_bot.py [--folder_name FOLDER_NAME]
--folder_name
Name of the folder containing the images. The default isimages
.
This script contains helper functions that are used in several other scripts. It is not intended to be run directly, but is imported and used by other scripts.
download_image(url, path)
: Downloads images and saves to the specified folder.url
: link to the image.path
: path to save the image.get_extension(url)
: Retrieves and returns the file extension from the given image URL.url
: Image URL.get_date_from_epic(date)
: changes the date format to year/month/day/
The functions of this script can be imported into other scripts and used as needed. For example, to import and use the download_image
function, you can add the following line to the beginning of another script:
from common_scripts import download_image
Because common_scripts.py
is a collection of utility functions, there are no command line arguments or specific instructions for running it directly.
The code was written for educational purposes in an online course for web developers dvmn.org