NatGeo - Download a month's photos
NG_download_month.sh
#!/bin/sh CYAN='\033[0;36m' YELLOW='\033[1;33m' NC='\033[0m' # No Color currentPath=`pwd` currentYear=`date +"%Y"` currentMonth=`date +"%m"` downloadYear=$1 downloadMonth=$2 force=$3 if [ $# -lt 2 ] then echo "YEAR and MONTH arguments required; eg: ./NG_download_month.sh 2017 06" exit fi jsonFilename="gallery."$downloadYear"-"$downloadMonth".json" if [ $currentYear -eq $downloadYear -a $currentMonth -eq $downloadMonth ] then echo "${CYAN}Current month${NC} images get update. Deleting file: $jsonFilename" rm $jsonFilename elif [ $force == '-f' ] then echo "Force deleting file: $jsonFilename" rm $jsonFilename fi if [ ! -f $jsonFilename ] then echo "${YELLOW}$jsonFilename${NC} unavailable. Downloading..." # BASE_URL="http://www.nationalgeographic.com/photography/photo-of-the-day/_jcr_content" BASE_URL="https://www.nationalgeographic.com/content/photography/en_US/photo-of-the-day/_jcr_content" curl -# ${BASE_URL}/.${jsonFilename} -o $jsonFilename fi itemsCount=`cat $jsonFilename | python -c "import sys,json;g=json.load(sys.stdin);print len(g['items'])"` echo 'Number of images to download: '$itemsCount for (( i=0; i<$itemsCount; i++ )) do photoOfTheDay=`cat $jsonFilename | python -c "import sys,json;g=json.load(sys.stdin);print g['items']["$i"]['url'] + g['items']["$i"]['originalUrl']"` imageFilename=`cat $jsonFilename | python -c "import sys,json,re;g=json.load(sys.stdin);p=g['items']["$i"]['pageUrl'];print re.search('([^\/]+$)', p).group(0)"` imageFilename=$downloadYear"-"$downloadMonth"-"$imageFilename'.jpg' if [ -f NG_archive/$imageFilename ] then printf "${YELLOW}Image already exist.${NC} skipping ${CYAN}$imageFilename${NC}\n" else printf "${YELLOW}Image doesnot exist.${NC} Downloading ${CYAN}$imageFilename${NC}\n" curl -# $photoOfTheDay -o NG_archive/$imageFilename fi done
Comments
Post a Comment