Skip to content

Instantly share code, notes, and snippets.

@cstroie
Last active September 28, 2015 21:18
Show Gist options
  • Save cstroie/1498114 to your computer and use it in GitHub Desktop.
Save cstroie/1498114 to your computer and use it in GitHub Desktop.
National Geographic's Picture of the Day downloader
#!/usr/bin/env python
#
# ng_pod.py
#
# Copyright 2011 Costin STROIE <[email protected]>
#
# NG_PoD is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# NG_PoD is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with NG_PoD. If not, see <http://www.gnu.org/licenses/>.
#
""" Download the National Geographic Picture of the Day and
set the xfdesktop to use it as background """
# The Photo of the Day page
POD_ROOT = 'http://photography.nationalgeographic.com'
POD_URL = POD_ROOT + '/photography/photo-of-the-day'
# Import the required modules
import sys, urllib, os
from lxml import html as HTML
from lxml.cssselect import CSSSelector
from datetime import datetime
import subprocess
# Download directory
POD_DIRECTORY = os.path.expanduser(os.path.join('~', 'Pictures', 'PoD', 'NG'))
if not os.path.isdir(POD_DIRECTORY):
os.makedirs(POD_DIRECTORY)
def download_image(url, recursive = False):
""" The main method """
# Try to open the page
try:
pod_fd = urllib.urlopen(url)
pod_page = HTML.parse(pod_fd)
except:
raise
# Find the previous photo
csssel = CSSSelector('div#pod_nav>p.prev>a')
try:
prev_a = csssel(pod_page)[0]
except IndexError:
previous = None
print 'ERROR: No previous link.'
else:
previous = POD_ROOT + prev_a.get('href')
print 'INFO: Previous link: %s' % previous
# Find the publising time
csssel = CSSSelector('p.publication_time')
try:
pubtime = csssel(pod_page)[0]
except IndexError:
print 'INFO: No publishing time.'
date = ''
else:
pubtime = str(pubtime.text)
print 'INFO: Published %s' % pubtime
try:
pubdate = datetime.strptime(pubtime, '%B %d, %Y')
except ValueError:
print 'ERROR: Could not parse the pubtime.'
date = ''
else:
date = datetime.strftime(pubdate, '%Y%m%d')
# Find the primary photo
csssel = CSSSelector('div.primary_photo a img')
try:
photo_img = csssel(pod_page)[0]
except IndexError:
print 'ERROR: No primary photo.'
photo_src = None
photo_alt = None
else:
# Get its src and alt
photo_src = photo_img.get('src')
if photo_src.startswith('//'):
photo_src = 'http:' + photo_src
photo_alt = photo_img.get('alt')
# Fix the alt
if photo_alt.startswith('Photo:'):
photo_alt = photo_alt[6:].strip()
print 'INFO: Photo src: %s' % photo_src
print 'INFO: Photo alt: %s' % photo_alt
# Find the download link
csssel = CSSSelector('div.download_link a')
try:
dw_a = csssel(pod_page)[0]
except IndexError:
print 'INFO: No download link, using the primary photo.'
pod_url = photo_src
use_photo = False
else:
# Get the href
pod_url = dw_a.get('href')
print 'INFO: Photo href: %s' % pod_url
use_photo = True
if pod_url is not None:
# Download the image
filename = '%s %s.jpg' % (date, photo_alt)
file_path = os.path.join(POD_DIRECTORY, filename)
if os.path.exists(file_path):
# Stop the recurence
recursive = False
print 'INFO: Recursive mode stop.'
else:
print 'INFO: Downloading "%s"' % photo_alt
urllib.urlretrieve(pod_url, file_path)
# Recurence
if recursive and previous is not None:
download_image(previous, recursive = recursive)
if use_photo:
return file_path
if __name__ == '__main__':
recursive = False
if len(sys.argv) > 1 and sys.argv[1] == '-R':
# Recursive
recursive = True
print 'INFO: Recursive mode start.'
# Download
file_path = download_image(POD_URL, recursive = recursive)
# Set the wallpaper in XFCE
if file_path is not None:
subprocess.call(['xfconf-query', '-c', 'xfce4-desktop', '-p', '/backdrop/screen0/monitor0/image-path', '-s', file_path])
# vim: set ft=python ai ts=4 sts=4 et sw=4 sta nowrap nu :
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment