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

<p>自己写的. python. <p>一开始是在Windows上运行, windows比较烦就烦在作为桌面的图片还得是BMP的, 这样还得装个python的PIL库来转换. 而且设置也相对麻烦. <p>最近在公司的另一台机器是ubuntu, 而在gnome上不仅可以直接用jpg文件, 设置也是一行命令就搞掂. 方便. <p>  <p>&quot;&quot;&quot;<br>Set the wallpaper as the picture got from national geographic picture of the day<br>by [email protected]<br>FIXME: Needs to set the registry to keep the wallpaper setting after restart system<br>&quot;&quot;&quot;<br>import ctypes<br>import calendar<br>import urllib<br>import socket<br>import os<br>import time <p>STOREDIR = os.path.expanduser(&quot;~&quot;) + '/wallpaper/pod/'<br>DOWNLOADED = 0 <p>def setWallPaper(imagePath):<br>    if os.name==&quot;nt&quot;:<br>        &quot;&quot;&quot;Given a path to an image, convert it to bmp and set it as wallpaper&quot;&quot;&quot;<br>        bmpImage = Image.open(imagePath)<br>        newPath = STOREDIR + 'mywallpaper.bmp'<br>        bmpImage.save(newPath, &quot;BMP&quot;)<br>        setWallpaperFromBMP(newPath)<br>    elif os.name==&quot;posix&quot;: <br>        try:<br>            os.system(&quot;gconftool-2 -t string -s /desktop/gnome/background/picture_filename \&quot;&quot;+imagePath+&quot;\&quot; -s /desktop/gnome/background/picture_options stretched&quot;)<br>        except:<br>            os.system(&quot;dcop kdesktop KBackgroundIface setWallpaper \&quot;&quot;+imagePath+&quot;\&quot; 7&quot;) <br>def getPicture():<br>    try:<br>        sock = urllib.urlopen(&quot;<a href="http://lava.nationalgeographic.com/cgi-bin/pod/PhotoOfTheDay.cgi&quot;)">http://lava.nationalgeographic.com/cgi-bin/pod/PhotoOfTheDay.cgi&quot;)</a><br>        htmlSource = sock.read()<br>        sock.close()    <br>        pos1 = htmlSource.find('Enlarge&lt;/a&gt;')<br>        pos2 = htmlSource.find('Wallpaper&lt;/a&gt;')<br>        page2 = htmlSource[pos1+50:pos2-2]<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[pos1+42:pos2-2]<br>        print filename<br>        fileurl = '<a href="http://photography.nationalgeographic.com'+filename">http://photography.nationalgeographic.com'+filename</a><br>        onlyfile = filename[filename.rfind(&quot;/&quot;)+1:];<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 &quot;fail to getting picture of &quot;,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&amp;NA=1149&amp;PI=73329&amp;RF=&amp;DI=3919&amp;PS=85545&amp;TP=seasonsme.spaces.live.com&amp;GT1=seasonsme">

你可能感兴趣的:(python,脚本,OS,ubuntu,cgi)