Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save casouri/90089a458dfdff2b72a6fa486cd8a7e1 to your computer and use it in GitHub Desktop.
Save casouri/90089a458dfdff2b72a6fa486cd8a7e1 to your computer and use it in GitHub Desktop.
A BASH script to download National Geographic's picture of the day (http://photography.nationalgeographic.com/photography/photo-of-the-day) saving and setting as wallpaper automatically.
#!/bin/bash
# Copyright (c) 2011 Josh Schreuder
# http://www.postteenageliving.com
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# ********************************
# *** NOTICE
# ********************************
# you have to change <your_home_dir> on line 130 to the name of your home directory if you are using mac OS
# ********************************
# *** OPTIONS
# ********************************
# Set this to 'yes' to save a description (to ~/description.txt) from ngeo page
GET_DESCRIPTION="yes"
# Set this to 'yes' if you are running the script on a mac OS
MAC="yes"
# Set this to 'yes' if you are running the script on a Linux os
Linux="no"
# Set this to the directory you want pictures saved
PICTURES_DIR=~/Pictures/ngpd
# ********************************
# *** FUNCTIONS
# ********************************
function get_page {
echo "Downloading page to find image"
wget http://photography.nationalgeographic.com/photography/photo-of-the-day/ --quiet -O- 2> /dev/null | grep -m 1 http://images.nationalgeographic.com/.*.jpg -o > /tmp/pic_url
}
function get_page_mac {
echo "Get website page for url"
wget --quiet -O - http://www.nationalgeographic.com/photography/photo-of-the-day/ | grep -m 1 http://yourshot.nationalgeographic.com/u/.*/ -o > /tmp/pic_url
}
function clean_up {
echo "Clean up temporary files"
if [ -e "tmp/pic_url" ]; then
rm /tmp/pic_url
fi
}
# ********************************
# *** MAIN
# ********************************
echo "===================="
echo "== NGEO Wallpaper =="
echo "===================="
# Set date
TODAY=$(date +'%Y%m%d')
if [ LINUX="yes" ]; then
# If we don't have the image already today
if [ ! -e ~/Pictures/${TODAY}_ngeo.jpg ]; then
echo "We don't have the picture saved, save it"
get_page
# Got the link to the image
PICURL=`/bin/cat /tmp/pic_url`
echo "Picture URL is: ${PICURL}"
echo "Downloading image"
wget --quiet $PICURL -O $PICTURES_DIR/${TODAY}_ngeo.jpg
echo "Setting image as wallpaper"
gconftool-2 -t string -s /desktop/gnome/background/picture_filename $PICTURES_DIR/${TODAY}_ngeo.jpg
# Else if we have it already, check if it's the most updated copy
else
get_page
# Got the link to the image
PICURL=`/bin/cat /tmp/pic_url`
echo "Picture URL is: ${PICURL}"
# Get the filesize
SITEFILESIZE=$(wget --spider $PICURL 2>&1 | grep Length | awk '{print $2}')
FILEFILESIZE=$(stat -c %s $PICTURES_DIR/${TODAY}_ngeo.jpg)
# If the picture has been updated
if [ $SITEFILESIZE != $FILEFILESIZE ]; then
echo "The picture has been updated, getting updated copy"
rm $PICTURES_DIR/${TODAY}_ngeo.jpg
# Got the link to the image
PICURL=`/bin/cat /tmp/pic_url`
echo "Downloading image"
wget --quiet $PICURL -O $PICTURES_DIR/${TODAY}_ngeo.jpg
echo "Setting image as wallpaper"
gconftool-2 -t string -s /desktop/gnome/background/picture_filename $PICTURES_DIR/${TODAY}_ngeo.jpg
# If the picture is the same
else
echo "Picture is the same, finishing up"
fi
fi
else
# routine for mac
if [ ! -e $PICTURES_DIR/${TODAY}_NGPD.jpg ]; then
get_page_mac
PICURL=`/bin/cat /tmp/pic_url`
# ${str:0:${#str} - 2} is string without th last 2 degit,
# the url I get in get_page has a tail of "/
echo "Picture URL is: ${PICURL:0:${#PICURL} - 2}"
echo "Download picture"
wget --quiet ${PICURL:0:${#PICURL} - 2} -O $PICTURES_DIR/${TODAY}_NGPD.jpg
chmod 666 $PICTURES_DIR/${TODAY}_NGPD.jpg
# Save picture for export to desktop picture,
# for osascript would not take substutution of variable
cp $PICTURES_DIR/${TODAY}_NGPD.jpg /Users/<your_home_dir>/Pictures/ngpd/ngpd_ept.jpg
fi
echo "Set picture as wallpaper"
FILE_PATH=$PICTURES_DIR/${TODAY}_NGPD.jpg
osascript -e 'tell application "Finder" to set desktop picture to "/Users/<your_home_dir>/Pictures/ngpd/ngpd_ept.jpg" as POSIX file'
fi
clean_up
#!/bin/bash
PICTURES_DIR=~/Pictures/ngpd
# ====================
# = FUNCTION =
# ====================
function get_page {
echo "Get website page for url"
wget --quiet -O - http://www.nationalgeographic.com/photography/photo-of-the-day/ | grep -m 1 http://yourshot.nationalgeographic.com/u/.*/ -o > /tmp/pic_url
}
function clean_up {
echo "Clean up temporary files"
if [ -e "tmp/pic_url" ]; then
rm /tmp/pic_url
fi
if [ -e "/tmp/ngpd_ept.jpg" ]; then
rm /tmp/ngpd_ept.jpg
fi
}
# ====================
# = MAIN =
# ====================
TODAY=$(date +'%Y%m%d')
if [ ! -e $PICTURES_DIR/ ]; then
echo "First time to use, setting up."
mkdir $PICTURES_DIR
fi
get_page
PICURL=`/bin/cat /tmp/pic_url`
PICURL2="${PICURL:0:${#PICURL} - 2}"
# echo "Picture URL is: $PICURL2"
echo "Download picture"
wget --quiet $PICURL2 -O $PICTURES_DIR/${TODAY}_NGPD.jpg
chmod 666 $PICTURES_DIR/${TODAY}_NGPD.jpg
# Save picture for export to desktop picture, for osascript would not take substutution of variable
cp $PICTURES_DIR/${TODAY}_NGPD.jpg /tmp/ngpd_ept.jpg
echo "Set picture as wallpaper"
osascript -e 'tell application "Finder" to set desktop picture to "/tmp/ngpd_ept.jpg" as POSIX file'
clean_up
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment