每天换一个新桌面的python

This is a python snippet to set wallpaper as the photo of the day in national geographic.

1. install python form http://www.python.org/download/
2. install python image library(PLI) from http://www.pythonware.com/products/pil/
3. say the code below and run it by python

python 代码
 
  1. """ 
  2. Set the wallpaper as the picture got from national geographic picture of the day 
  3. by [email protected] 
  4. """  
  5. import ctypes  
  6. import Image  
  7. import calendar  
  8. import urllib  
  9. import socket  
  10.   
  11. STOREDIR = 'C:/lg_wallpaper/pod/'  
  12.   
  13. def setWallpaperFromBMP(imagepath):  
  14.     SPI_SETDESKWALLPAPER = 20 # According to http://support.microsoft.com/default.aspx?scid=97142  
  15.     ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imagepath , 0) #SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE  
  16.   
  17. def setWallPaper(imagePath):  
  18.     """Given a path to an image, convert it to bmp and set it as wallpaper"""  
  19.     bmpImage = Image.open(imagePath)  
  20.     newPath = STOREDIR + 'mywallpaper.bmp'  
  21.     bmpImage.save(newPath, "BMP")  
  22.     setWallpaperFromBMP(newPath)  
  23.       
  24. def getPicture(fname):  
  25.     sock = urllib.urlopen("http://lava.nationalgeographic.com/pod/index.html")  
  26.     htmlSource = sock.read()  
  27.     sock.close()      
  28.     pos1 = htmlSource.find('sm_wallpaper')  
  29.     pos2 = htmlSource.find('/pod/pictures/normal')  
  30.     filename = htmlSource[pos1+13:pos2-12]  
  31.     fileurl = 'http://lava.nationalgeographic.com/pod/pictures/lg_wallpaper/'+filename  
  32.     urllib.urlretrieve(fileurl, fname)  
  33.     print 'got the picture from ' + fileurl  
  34.   
  35. def setWallpaperOfToday():  
  36.     filename = STOREDIR + str(calendar.datetime.date.today()) + '.jpg'  
  37.     print filename  
  38.     getPicture(filename)  
  39.     setWallPaper(filename)  
  40.      
  41. setWallpaperOfToday()  
  42. print 'Wallpaper set ok!'  

你可能感兴趣的:(c,python,socket,Microsoft,Gmail)