Created
May 10, 2012 01:59
-
-
Save venj/2650448 to your computer and use it in GitHub Desktop.
Download NASA Astronomy Picture Of Day media. (Bash version)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Constants | |
TMPDIR=/tmp/apod | |
LISTFILE=$TMPDIR/list.html | |
REMOTEPREFIX=http://antwrp.gsfc.nasa.gov/apod/ | |
# Functions | |
function parse_page() { | |
printf "Downloading photo page %s...\n" $1 | |
page=$TMPDIR/$1 | |
curl -s $REMOTEPREFIX/$1 -o $page | |
regex=\[\^\"\]\*\\.\\\(jpg\\\|png\\\|gif\\\|avi\\\|mov\\\|mp4\\\|m4v\\\) | |
small=`grep -i img $page | grep -e $regex -o` | |
big=`grep -e href=\"$regex $page | grep -e $regex -o` | |
if ! [ -f $SMALLDIR/`basename $small` ];then | |
printf "Downloading small photo %s...\n" `basename $small` | |
curl -s $REMOTEPREFIX/$small -o $SMALLDIR/`basename $small` | |
fi | |
if ! [ -f $BIGDIR/`basename $big` ];then | |
printf "Downloading big photo %s...\n" `basename $big` | |
curl -s $REMOTEPREFIX/$big -o $BIGDIR/`basename $big` | |
fi | |
} | |
function parse_list() { | |
for i in $(grep -e 'ap\w\{5,\}\.html' $LISTFILE -o) | |
do | |
printf "Parsing page %s...\n" $i | |
parse_page $i | |
done | |
} | |
# Judge if curl exists | |
which -s curl | |
if [ $? -ne 0 ];then | |
printf "You must install curl first.\n" | |
exit 1 | |
fi | |
# Show Start up Message | |
function print_info() { | |
clear | |
printf "*************************************************************\n\n" | |
printf "About to download NASA Astronomy Photo of Day Archive.\n" | |
printf "It could take a very long time to download over 10,000 files.\n" | |
printf "So, Please be patient.\n\n" | |
printf "We recommend you to run this script on the background.\n\n" | |
printf "Press ENTER to continue, CTRL-c to stop.\n\n" | |
printf "*************************************************************\n" | |
read | |
clear | |
} | |
# Start to process | |
if [ $# -ne 0 ] && ([ $1='-h' ] || [ $1='--help' ]);then | |
printf "\n\tUsage: %s [-h|--help|DOWNLOAD_DIR]\n\n" | |
printf "\tIf you do not specify download dir, \n" | |
printf "\tphoto will download to current dir.\n\n" | |
exit 0 | |
fi | |
# Make a temp dir | |
mkdir -p $TMPDIR | |
printf "Make directories for hold photos...\n" | |
# Make big and small dir | |
if [ $# -ne 0 ] && [ -d $1 ];then | |
BIGDIR=$1/big | |
SMALLDIR=$1/small | |
else | |
BIGDIR=$PWD/big | |
SMALLDIR=$PWD/small | |
fi | |
print_info | |
mkdir -p $BIGDIR | |
mkdir -p $SMALLDIR | |
printf "Downloading index page...\n" | |
# Download the list html to temp dir as name list.html | |
if ! [ -f $LISTFILE ];then | |
curl -s ${REMOTEPREFIX}/archivepix.html -o $LISTFILE | |
fi | |
# Parse it. | |
parse_list | |
printf "\nDone.\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment