Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
NatGeo Wallpaper Downloader
# NatGeo Wallpaper Downloader
# version: 0.1.2
# author: Abdallah Deeb <>
# description: Simply python script to download
# and set the gnome wallpaper from the
# National Geographic "Photo of the day" page
# source:
import urllib2, urllib, os, re
from gi.repository import Gio
from twitter import *
SCHEMA = 'org.gnome.desktop.background'
URI_KEY = 'picture-uri'
OPTIONS_KEY = 'picture-options'
# comment the following if you don't want to tweet it.
MY_TWITTER_CREDS = os.path.expanduser('~/.ngg3w.twitter_oauth')
oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
CONSUMER_KEY = 'GetYourOwnfvQE4g'
CONSUMER_SECRET = 'Get your own at'
twitter = Twitter(auth=OAuth(
oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
def myReportHook(count, blockSize, totalSize):
percent = int(count*blockSize*100/totalSize)
print percent
if percent>99:
wallpapers_path = '/home/abdallah/Pictures/Wallpapers'
url = urllib2.urlopen("")
print 'Getting main page: %s ...' % url
page =
pic_name = re.findall(r'<title>(.+?)</title>', page)[0].split(' - ')[0].lower().replace(' ', '_')+'.jpg'
pic_file = os.path.join(wallpapers_path, pic_name)
wallpaper_url = re.findall(r'<div class="download_link"><a href="(.+?)"', page)[0]
except IndexError:
wallpaper_url = re.findall(r'<div class="primary_photo">.+?<img.+?src="(.+?)"', page, re.S)[0]
print 'Wallpaper URL: %s' % wallpaper_url
if wallpaper_url:
print 'Downloading ...'
filename, msg = urllib.urlretrieve(wallpaper_url, pic_file, myReportHook)
gsettings =
gsettings.set_string(URI_KEY, "file://" + pic_file)
gsettings.set_string(OPTIONS_KEY, 'stretched')
print 'Setting wallpaper file done at %s' % pic_file
# Now post to Twitter (or comment the next line out
twitter.statuses.update(status='Just updated my desktop #wallpaper with this pic from #natgeo %s ' % wallpaper_url)

This comment has been minimized.

Show comment Hide comment

abdallah Jul 31, 2012

Added tweeting ...


abdallah commented Jul 31, 2012

Added tweeting ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment