1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
| # -*- coding: utf-8 -*-
# Set the wallpaper for Windows/Linux(gnome)
# with APOD or NGPOD
# by henrysting 2010-08-05
import ctypes
import urllib
import socket
import os
import time
if os.name=="nt":
import Image
import _winreg
def ftime():
str = time.strftime('%y%m%d', time.localtime())
return str
def convertToBMP(imagePath):
im = Image.open(imagePath)
w,h = im.size
if w > screen[0]: #width conformity
whRatio = float(w)/h
im = im.resize((screen[0],int(float(screen[0])/whRatio)),Image.BILINEAR)
if im.size[1] > screen[1]: #height conformity
im = im.resize(int(screen[1]*whRatio),screen[1])
newPath = STOREDIR + '\wallpaper.bmp'
im.save(newPath,'BMP')
return newPath
def registerCentered():
# changes the registry to center wallpaper.
# Stretch is 2 0,Tiled is 0 1.
wallpaperStyle = '1'
tileWallpaper = '0'
desktopKey = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, 'Control Panel\\Desktop',0,_winreg.KEY_SET_VALUE)
_winreg.SetValueEx(desktopKey,'WallpaperStyle',0,_winreg.REG_SZ,wallpaperStyle)
_winreg.SetValueEx(desktopKey,'TileWallpaper',0,_winreg.REG_SZ,tileWallpaper)
return 1
def getPicture():
if TYPE == 0:
homepage = 'http://apod.nasa.gov/apod/'
siteurl = 'http://apod.nasa.gov/apod/ap'+date+'.html'
sock = urllib.urlopen(siteurl)
htmlSource = sock.read()
if htmlSource.find('404 Not Found')+1:
print "Sever haven't updated yet"
return ''
sock.close()
pos1 = htmlSource.find('href="image/')
pos2 = htmlSource.find('.jpg"',pos1)
page2 = htmlSource[pos1+6:pos2+4]
filename = htmlSource[pos1+17:pos2+4]
fileurl = homepage+page2
elif TYPE == 1:
siteurl = 'http://photography.nationalgeographic.com'
sock = urllib.urlopen(" [photography.nationalgeographic.com] htmlSource = sock.read()
sock.close()
pos0 = htmlSource.find('download_link')
if pos0+1:
print 'There is a Wallpaper today!'
pos1 = htmlSource.find('href=" [images.',pos0)] pos2 = htmlSource.find('.jpg">Download',pos0)
else:
print 'Not so attractive ...'
pos0 = htmlSource.find('primary_photo')
pos1 = htmlSource.find(' src=" [images.',pos0)] pos2 = htmlSource.find('.jpg"',pos0)
fileurl = htmlSource[pos1+6:pos2+4]
filename = htmlSource[pos1+79:pos2+4]
else:
print 'Wrong Type Number!'
try:
if os.name=="nt":
fname = STOREDIR + '\\'+ filename
elif os.name=="posix":
fname = STOREDIR + '/'+ filename
if not os.path.exists(fname):
print 'Retrieving the picture from ' + fileurl
urllib.urlretrieve(fileurl, fname)
else:
print 'The file is already there.'
return fname
except:
print filename
print fileurl
print fname
print "Failed to retrieve the picture of ",date
pass
return fname
def setWallpaperFromBMP(imagepath):
SPI_SETDESKWALLPAPER = 20
ctypes.windll.user32.SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, imagepath , 0)
def setWallPaper(imagePath):
if os.name=="nt":
registerCentered()
newPath = convertToBMP(imagePath)
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 scaled")
# option can also be centered,stretched,tiled etc.
except:
os.system("dcop kdesktop KBackgroundIface setWallpaper \""+imagePath+"\" 7")
def setWallpaperOfToday():
fname = getPicture()
if fname != '':
print 'Local file is ' + fname
setWallPaper(fname)
print 'Wallpaper is set!'
else:
print 'Nothing done.'
if __name__ == "__main__":
STOREDIR = os.getcwd()
sites = ['Astronomy POD','National Geographic POD']
TYPE = 1
screen = (1280,1024) # 屏幕分辨率
date = '' # format should be 'yymmdd'
if date == '':
date = ftime()
print sites[TYPE],date
setWallpaperOfToday() |