Created
June 27, 2012 09:10
-
-
Save abdallah/3002699 to your computer and use it in GitHub Desktop.
NatGeo Wallpaper Downloader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added tweeting ...