#! /usr/bin/env python # # Stefano Lacaprara INFN Padova # # Script to get the astronomy picture of the day from # http://apod.nasa.gov/apod/ and set it as desktop background in a gnome environment # # It saves the images in "localdir" (see below) default to # "~/Private/AstroPicture/" . If the directory does not exist, it is created # # Released under Creative Commons license (http://creativecommons.org/licenses/by/2.5/it/) # url = "http://apod.nasa.gov/apod/" page = "astropix.html" # relative to your HOME directory localdir = "Private/AstroPicture/" import gconf import os import mimetypes class GConfClient: def __init__ (self): self.__client__ = gconf.client_get_default() def get_background (self): return self.__client__.get_string("/desktop/gnome/background/picture_filename") def set_background (self, background): self.__client__.set_string("/desktop/gnome/background/picture_filename", background) import urllib2 import re fullpath= os.environ.get('HOME') + "/" + localdir + "/" #check dir existence: if not, create it if (not os.path.isdir(fullpath)): print "creating dir: "+fullpath os.makedirs(fullpath) #open remote page try: f = urllib2.urlopen(url+page).read() except urllib2.URLError: print "cannot open %s",url+page abort #find the image m = re.compile("image/\w+/[\w-]+.jpg").search(f) if m: print 'Match found: ', m.group() try: jpg=urllib2.urlopen(url + m.group()).read() except: print "Error" jpgname=(m.group()).replace("image/","").replace("/","_") filename=os.environ.get('HOME') + "/" + localdir + jpgname #save the image to localdir if not already done if (not os.path.exists(filename)): file=open(filename, "w") file.write(jpg) print "JPG ", jpgname, " saved to ",filename file.close() #set the image as wallpaper client = GConfClient() client.set_background(filename) else: print 'No match: cannot find image at '+url