python脚本换桌面

原来何勃亮发过一个gnome下自动更新每日天图(APOD)作为桌面的脚本,挺有意思的,我这里发一个Python的,可以看作是免费软件Pic-a-PODPicture of the Day的替代品,在2.6下测试通过,需要安装PIL库,可以自行选择天文每日一图(APOD)、地理每日一图(EPOD)、或者美国国家地理每日一图(NGPOD),点此下载

这是由seasons的版本修改而来,他那里漏了一个setWallpaperFromBMP函数,还好在他的 javaEye 日志中找到了,有人是用win32gui实现,不过我测试没有成功。

源码如下,方便引擎收录:)

"""
Set the wallpaper with APOD、EPOD or NGPOD
by henrysting            2009-01-31
"""
import ctypes
import calendar
import urllib
import socket
import os
import time 
import Image

STOREDIR = os.path.expanduser("~") + '/wallpaper/pod/'
DOWNLOADED = 0
TYPE = 3  """ 1:Astronomy POD, 2:Earth Science POD, 3:National Geographic POD"""

def setWallPaper(imagePath):
    if os.name=="nt":
        """Given a path to an image, convert it to bmp and set it as wallpaper"""
        bmpImage = Image.open(imagePath)
        newPath = STOREDIR + 'mywallpaper.bmp'
        bmpImage.save(newPath, "BMP")
        setWallpaperFromBMP(newPath)
    elif os.name=="posix": 
        try:
            os.system("gconftool-2 -t string -s /desktop/gnome/background/picture_filename \""+imagePath+"\" -s /desktop/gnome/background/picture_options stretched")
        except:
            os.system("dcop kdesktop KBackgroundIface setWallpaper \""+imagePath+"\" 7") 

def getPicture():
    if TYPE == 1:
        siteurl = 'http://apod.nasa.gov/apod/'
        sock = urllib.urlopen(siteurl)
        htmlSource = sock.read()
        sock.close()    
        pos1 = htmlSource.find('href="image/')
        pos2 = htmlSource.find('jpg">')
        page2 = htmlSource[pos1+6:pos2+3]
        filename = htmlSource[pos1+17:pos2+3]
        fileurl = siteurl+page2
    elif TYPE == 2:
        siteurl = 'http://epod.usra.edu/'
        sock = urllib.urlopen(siteurl)
        htmlSource = sock.read()
        sock.close()    
        pos1 = htmlSource.find('')
        page2 = htmlSource[pos1+10:pos2+4]
        filename = htmlSource[pos1+25:pos2+4]
        fileurl = siteurl+page2
    elif TYPE == 3:
        siteurl = 'http://photography.nationalgeographic.com'
        sock = urllib.urlopen("http://lava.nationalgeographic.com/cgi-bin/pod/PhotoOfTheDay.cgi")
        htmlSource = sock.read()
        sock.close()    
        pos1 = htmlSource.find('Enlarge')
        pos2 = htmlSource.find('Wallpaper')
        page2 = htmlSource[pos1+50:pos2-2]
        pageurl = siteurl+page2
        sock = urllib.urlopen(pageurl)
        htmlSource = sock.read()
        sock.close()    
        pos1 = htmlSource.find('art-icon-wallpaper-1280.gif')
        pos2 = htmlSource.find('1280 x 1024 pixels')
        page2 = htmlSource[pos1+42:pos2-2]
        fileurl = siteurl+page2
        filename = page2[page2.rfind("/")+1:];
    else:
        print 'Wrong Type!'
        
    try:    
        print filename
        print fileurl
        fname = STOREDIR + filename
        if not os.path.exists(fname):
            print 'file not retrieved'
            urllib.urlretrieve(fileurl, fname)
            print 'got the picture from ' + fileurl
        else:
            print 'The file is retrieved'
        return fname
    except Error:
        print "fail to getting picture of ",yy,mm,dd
        pass
    return fname 

def setWallpaperFromBMP(imagepath):  
    SPI_SETDESKWALLPAPER = 20 # According to http://support.microsoft.com/default.aspx?scid=97142  
    ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imagepath , 0) #SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE  
    #win32gui.SystemParametersInfo(SPI_SETDESKWALLPAPER,imagepath , 0)

def setWallpaperOfToday():
    filename  = getPicture()
    print 'the filename is ' + filename
    setWallPaper(filename) 

def prepare():
    #prepare the directory
    if not os.path.exists(STOREDIR):
        print 'going to make dir ' + STOREDIR
        os.makedirs(STOREDIR)
    print 'prepare done ...' 

prepare()
setWallpaperOfToday() 
print 'Wallpaper set ok!' 
订阅评论
提醒
3 评论
最旧
最新 最多投票
内联反馈
查看所有评论
asd
2009 年 4 月 6 日 20:39

Oh.my god. – – It’s wonderful 3q.

回复给  gerry
2010 年 7 月 27 日 23:20

很有意思