public
Created

NatGeo Wallpaper Downloader

  • Download Gist
NatGeoWallpaperDownloader.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
#!/usr/bin/python
# NatGeo Wallpaper Downloader
# version: 0.1.2
# author: Abdallah Deeb <abdallah.deeb@gmail.com>
# description: Simply python script to download
# and set the gnome wallpaper from the
# National Geographic "Photo of the day" page
# source: http://photography.nationalgeographic.com/photography/photo-of-the-day/
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'
DL_COMPLETE = False
 
# 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 https://dev.twitter.com/apps/'
twitter = Twitter(auth=OAuth(
oauth_token, oauth_secret, CONSUMER_KEY, CONSUMER_SECRET))
 
 
def myReportHook(count, blockSize, totalSize):
global DL_COMPLETE
percent = int(count*blockSize*100/totalSize)
print percent
if percent>99:
DL_COMPLETE = True
wallpapers_path = '/home/abdallah/Pictures/Wallpapers'
url = urllib2.urlopen("http://photography.nationalgeographic.com/photography/photo-of-the-day/")
print 'Getting main page: %s ...' % url
page = url.read()
pic_name = re.findall(r'<title>(.+?)</title>', page)[0].split(' - ')[0].lower().replace(' ', '_')+'.jpg'
pic_file = os.path.join(wallpapers_path, pic_name)
try:
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)
if DL_COMPLETE:
gsettings = Gio.Settings.new(SCHEMA)
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)

Added tweeting ...

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.