每天换一个壁纸,每天好心情。
# -*- coding: UTF-8 -*-
from __future__ import unicode_literals
import Image
import datetime
import win32gui,win32con,win32api
import re
from HttpWrapper import SendRequest
StoreFolder = "c:\\dayImage"
def setWallpaperFromBMP(imagepath):
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸适应桌面,0桌面居中
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)
def setWallPaper(imagePath):
"""
Given a path to an image, convert it to bmp and set it as wallpaper
"""
bmpImage = Image.open(imagePath)
newPath = StoreFolder + '\\mywallpaper.bmp'
bmpImage.save(newPath, "BMP")
setWallpaperFromBMP(newPath)
def getPicture():
url = "http://photography.nationalgeographic.com/photography/photo-of-the-day/"
h = SendRequest(url)
if h.GetSource():
r = re.findall('
return SendRequest(r[0]).GetSource()
path = StoreFolder + "\\%s.jpg" % datetime.date.today()
其中的httpwrapper是我写的一个http访问的封装:
#-------------------------------------------------------------------------------
#-------------------------------------------------------------------------------
SendRequest('http://xxx.com',data=dict, type='POST', auth='base',user='xxx', password='xxx')
cookie: 请求附带的cookie,一般用于登录后的认证
e.g. referer='www.sina.com.cn'
self.referer = header[referer]
self.user_agent = header[user-agent]
## self.user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20100101 Firefox/8.0'
if self.url is None or self.url == '':
if self.method.lower() == 'post':
self.Req = urllib2.Request(self.url, urllib.urlencode(self.data))
elif self.method.lower() == 'get':
self.Req = urllib2.Request(self.url)
self.Req = urllib2.Request(self.url + '?' + urllib.urlencode(self.data))
if self.user == None or self.password == None:
raise 'The user or password was not given!'
auth_info = base64.encodestring(self.user + ':' + self.password).replace('\n','')
auth_info = 'Basic ' + auth_info
self.Req.add_header("Authorization", auth_info)
raise 'The cookie was not given!'
self.Req.add_header("Cookie", self.cookie)
self.Req.add_header('referer', self.referer)
self.Req.add_header('user-agent', self.user_agent)
self.Res = urllib2.urlopen(self.Req)
self.code = self.Res.getcode()
self.head_dict = self.Res.info().dict
print "Error: HttpWrapper=>_SendRequest ", sys.exc_info()[1]
if 'set-cookie' in self.head_dict:
return self.head_dict['set-cookie']
if 'content-type' in self.head_dict:
return self.head_dict['content-type']
contentType = self.GetContentType()
index = contentType.find("charset")
if 'expires' in self.head_dict:
return self.head_dict['expires']
if 'server' in self.head_dict:
return self.head_dict['server']
b = SendRequest("http://www.baidu.com")