seasons 发表于 2013-2-7 09:40:18

一个用NationalGeographic每日图片做桌面的脚本

<p>自己写的. python. <p>一开始是在Windows上运行, windows比较烦就烦在作为桌面的图片还得是BMP的, 这样还得装个python的PIL库来转换. 而且设置也相对麻烦. <p>最近在公司的另一台机器是ubuntu, 而在gnome上不仅可以直接用jpg文件, 设置也是一行命令就搞掂. 方便. <p>  <p>"""<br>Set the wallpaper as the picture got from national geographic picture of the day<br>by xpwang@bea.com<br>FIXME: Needs to set the registry to keep the wallpaper setting after restart system<br>"""<br>import ctypes<br>import calendar<br>import urllib<br>import socket<br>import os<br>import time<p>STOREDIR = os.path.expanduser("~") + '/wallpaper/pod/'<br>DOWNLOADED = 0<p>def setWallPaper(imagePath):<br>    if os.name=="nt":<br>        """Given a path to an image, convert it to bmp and set it as wallpaper"""<br>        bmpImage = Image.open(imagePath)<br>        newPath = STOREDIR + 'mywallpaper.bmp'<br>        bmpImage.save(newPath, "BMP")<br>        setWallpaperFromBMP(newPath)<br>    elif os.name=="posix": <br>        try:<br>            os.system("gconftool-2 -t string -s /desktop/gnome/background/picture_filename \""+imagePath+"\" -s /desktop/gnome/background/picture_options stretched")<br>        except:<br>            os.system("dcop kdesktop KBackgroundIface setWallpaper \""+imagePath+"\" 7") <br>def getPicture():<br>    try:<br>        sock = urllib.urlopen("<a href="http://lava.nationalgeographic.com/cgi-bin/pod/PhotoOfTheDay.cgi")">http://lava.nationalgeographic.com/cgi-bin/pod/PhotoOfTheDay.cgi")</a><br>        htmlSource = sock.read()<br>        sock.close()    <br>        pos1 = htmlSource.find('Enlarge</a>')<br>        pos2 = htmlSource.find('Wallpaper</a>')<br>        page2 = htmlSource<br>        pageurl = '<a href="http://photography.nationalgeographic.com'+page2">http://photography.nationalgeographic.com'+page2</a><br>        print pageurl<br>        sock = urllib.urlopen(pageurl)<br>        htmlSource = sock.read()<br>        sock.close()    <br>        pos1 = htmlSource.find('art-icon-wallpaper-1280.gif')<br>        pos2 = htmlSource.find('1280 x 1024 pixels')<br>        filename = htmlSource<br>        print filename<br>        fileurl = '<a href="http://photography.nationalgeographic.com'+filename">http://photography.nationalgeographic.com'+filename</a><br>        onlyfile = filename;<br>        print onlyfile<br>        fname = STOREDIR + onlyfile<br>        if not os.path.exists(fname):<br>            print 'file not retrieved'<br>            urllib.urlretrieve(fileurl, fname)<br>            print 'got the picture from ' + fileurl<br>        else:<br>            print 'The file is retrieved'<br>        return fname<br>    except Error:<br>        print "fail to getting picture of ",yy,mm,dd<br>        pass<br>    return fname<p>def setWallpaperOfToday():<br>    #filename  = getPicture()<br>    filename  = getPicture()<br>    print 'the filename is ' + filename<br>    setWallPaper(filename)<p>def setWallpaperByDate(yy,mm,dd):<br>    filename  = getPictureByDate(yy,mm,dd)<br>    setWallPaper(filename)<p>def prepare():<br>    #prepare the directory<br>    if not os.path.exists(STOREDIR):<br>        print 'going to make dir ' + STOREDIR<br>        os.makedirs(STOREDIR)<br>    print 'prepare done ...'<p>prepare()<br>setWallpaperOfToday()<p>print 'Wallpaper set ok!' <img src="http://c.services.spaces.live.com/CollectionWebService/c.gif?cid=-7639948550247457024&page=RSS%3a+%e4%b8%80%e4%b8%aa%e7%94%a8NationalGeographic%e6%af%8f%e6%97%a5%e5%9b%be%e7%89%87%e5%81%9a%e6%a1%8c%e9%9d%a2%e7%9a%84%e8%84%9a%e6%9c%ac&referrer=" width="1px" height="1px" border="0" alt=""><img style="position:absolute" alt="" width="0px" height="0px" src="http://c.live.com/c.gif?NC=31263&NA=1149&PI=73329&RF=&DI=3919&PS=85545&TP=seasonsme.spaces.live.com&GT1=seasonsme">
页: [1]
查看完整版本: 一个用NationalGeographic每日图片做桌面的脚本