A Newbie’s Scripts for Cygwin 1

March 10th, 2007 § 0 comments

国家地理网站下载每日图片

#!/bin/bash
# picngs: Download today's picture from National Geographics website
# Michael.Fantasia@Gmail.com
# arg1: datefmt
# arg2: picdir

# export LC_ALL=C

BADDATE=66
WEBFAILURE=67
SEDFAILURE=68

pntmp=$(basename "$0")_$$

picdir=${2:-"${HOME}/Pictures/Photography"}

cd /tmp

if [ $# -eq 0 ]
then
    mfdate=$(date +%Y.%m.%d)
    wget -q "http://photography.nationalgeographic.com/photography/photo-of-the-day/" -O $pntmp
else
    if date -d "$1" +%Y-%m-%d 2>/dev/null
    then
        mfdate=$(date -d $1 +%Y.%m.%d)
        mfdate2=$(date -d $1 +"%B %e, %Y" | sed 's/ +/ /g')
        wget -q "http://photography.nationalgeographic.com/photography/photo-of-the-day/archive/?page=1&month=$(date -d $1 +%m)&year=$(date -d $1 +%Y)" -O "$pntmp"_search
        if [ $? -ne 0 ]
        then
            echo "Error in finding picture." >&2
            exit $WEBFAILURE
        fi
        papername=$(sed -n '/'"$mfdate2"'/,/</div>/p' "$pntmp"_search | sed 's/</n/g' | grep '^a href' | grep 'photo-of-the-day' | head -n1 | cut -d'"' -f2)
        wget -q "http://photography.nationalgeographic.com$papername" -O $pntmp
    else
        echo "Unknown date format." >&2
        exit $BADDATE
    fi
fi

picname=$mfdate-$(sed -n 's/.*<h1>([^<]*)</h1>.*/1/p' $pntmp | sed 's/['"'"',"]//g;s/ +$//;s/ +/_/g;s///_/g;'"s/&#039/'/g" | tr '[:upper:]' '[:lower:]')
findname=$(sed 's/</div>/n/g' $pntmp | grep 'Download Wallpaper' | sed 's/</n/g' | grep '^a href' | head -n1 | cut -d'"' -f2)
if [ "$findname" = "" ]
then
    #findname=$(sed -n '/primary_photo/,/</div>/p' $pntmp | sed 's/</n/g' | grep '^img src' | grep 'ngeo' | head -n1 | cut -d'"' -f2)
    findname=$(sed -n '/primary_photo/,/</div>/p' $pntmp | sed 's/</n/g' | grep '^img src' | cut -d'"' -f2)
fi

if [ -z "$findname" ]
then
    echo "No picture found." >&2
    exit $SEDFAILURE
fi

wget -q -c "$findname" -O $picname.jpg

if [ $? -ne 0 ]
then
    echo "Error in downloading picture." >&2
    exit $WEBFAILURE
else
    mfdate=$(echo $mfdate | sed 's/.//g')
    mkdir -p $picdir/$(date -d $mfdate +%Y)
    mv $picname.jpg $picdir/$(date -d $mfdate +%Y)/
    open $picdir/$(date -d $mfdate +%Y)/$picname.jpg
fi

echo "Done."
growlnotify --image "$picdir/$(date -d $mfdate +%Y)/$picname.jpg" -m "National Geographic Picture of the Day $mfdate downloaded."

exit 0

Category Brain

Tagged

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>