/
bwpf.py
156 lines (130 loc) · 7.14 KB
/
bwpf.py
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# Python Bing Wallpaper Fetcher
# Author ReSec
# The Bing XML URL to fetch: http://www.bing.com/HPImageArchive.aspx
# With parameter:
# format=xml, indicates this will return an XML formatted stream that contains information such as the date the images is for, the relative URL, description, and copyright information.
# idx=0, tells where you want to start from. 0 would start at the current day, 1 the previous day, 2 the day after that, etc. For instance, if the date were 1/30/2011, using idx = 0, the file would start with 20110130; using idx = 1, it would start with 20110129; and so forth.
# n=1, tells how many images to return. n = 1 would return only one, n = 2 would return two, and so on.
# mkt tells which of the eight markets Bing is available for you would like images from.
# The valid mkt values are: en-US, zh-CN, ja-JP, en-AU, en-UK, de-DE, en-NZ, en-CA.
# This script will try to fetch the above XML and get today's Bing Wallpaper URL, then download the 1920*1200 picture and then set it as Windows Background
import xml.etree.ElementTree
import urllib.request
import os
import re
import time
import threading
import log
import setting
import service
manager = setting.getManager()
settings = manager.getDefaultSettings()
lock = threading.RLock()
def fetchElementTree(xmlUrl):
et = None
try:
log.info('Connect to %s to fetch XML' % xmlUrl)
response = urllib.request.urlopen(xmlUrl,timeout=5)
et = xml.etree.ElementTree.fromstring(response.read())
except Exception as e:
log.exception(e)
return et
def getWallPaperNodes(root):
return root.findall(settings['xml-path-image'])
def getWallPaperBaseUrl(imageNode):
return imageNode.find(settings['xml-path-base-url']).text
def parseWallPaperBaseName(wallpaperBaseUrl):
return re.split(r'/', wallpaperBaseUrl)[-1]
def parseWallPaperRealName(wallpaperBaseName):
return re.split(r'_', wallpaperBaseName)[0]
def getWallPaperDate(imageNode):
return imageNode.find(settings['xml-path-start-date']).text
def fetchWallpaper(wallpaperUrl,wallpaperPath):
try:
urllib.request.urlretrieve(wallpaperUrl,filename=wallpaperPath)
return 0
except Exception as e:
log.exception(e)
return 1
markets=settings['markets'].split(r',')
log.info('Start program')
processDate=time.strftime('%Y%m%d',time.localtime(time.time()))
xmlBaseUrl = settings['xml-base-url']
n = max(min(service.getDayCountSinceLatestProcessDate(),8), int(settings['url-parm-n']))
attempCountMax = int(settings['failure-attemp-time'])
lock = threading.Lock()
def fetchWallpaperFromMarket(market):
marketAttempCount = 0
while (marketAttempCount < attempCountMax):
log.info('Downlaod xml for market: %s' % market)
xmlUrl = xmlBaseUrl + 'format=' + settings['url-parm-format'] + '&idx=' + settings['url-parm-index'] + '&n=' + str(n) + '&mkt=' + market
xmlElementTreeRoot = fetchElementTree(xmlUrl)
if xmlElementTreeRoot != None:
log.info('Fetched XML, parse wallpaper information and download')
imageNodeList=getWallPaperNodes(xmlElementTreeRoot)
for imageNode in imageNodeList:
# parse the Wallpaper URL
wallpaperBaseUrl = getWallPaperBaseUrl(imageNode)
wallpaperBaseName = parseWallPaperBaseName(wallpaperBaseUrl)
wallpaperRealName = parseWallPaperRealName(wallpaperBaseName)
wallpaperPiexlSize = settings['wallpaper-pixel-width'] + 'x' + settings['wallpaper-pixel-hight']
wallpaperNamePostfix = '_' + wallpaperPiexlSize + '.' + settings['wallpaper-foramt']
wallpaperUrl = settings['wallpaper-base-url'] + wallpaperBaseName + wallpaperNamePostfix
wallpaperFolder = settings['wallpaper-folder']
wallpaperTempFolder = settings['temp-folder']
wallpaperPath = wallpaperFolder + processDate + '_' + wallpaperRealName + '_' + market + wallpaperNamePostfix
wallpaperTempPath = wallpaperTempFolder + '/' + processDate + '_' + wallpaperRealName + '_' + market + wallpaperNamePostfix + '.temp'
# show Wallpaper info
log.info('------------------------------------------')
log.info('Wallpaper Information')
log.info('Base Url : %s' % wallpaperBaseUrl)
log.info('Base Name : %s' % wallpaperBaseName)
log.info('Real Name : %s' % wallpaperRealName)
log.info('Full Url : %s' % wallpaperUrl)
log.info('Full Path : %s' % wallpaperPath)
log.info('------------------------------------------')
log.info('Fetch wallpaper')
fetchAttempCount = 0
while (fetchAttempCount < attempCountMax):
log.info('Check if %s has been fetched before' % wallpaperTempPath)
if not os.path.exists(wallpaperTempPath):
exitCode = fetchWallpaper(wallpaperUrl, wallpaperTempPath)
else:
log.info('Wallpaper %s has been download to temp folder, skip download' % (wallpaperTempPath))
exitCode = 0
if exitCode == 0:
lock.acquire()
if service.isFetched(wallpaperRealName)==True:
log.info('Wallpaper %s has been move to picture folder, skip it' % (wallpaperRealName))
lock.release()
break
log.info('%s %s has been temperately fetched to %s' % (wallpaperRealName, market, wallpaperTempPath))
log.info('Moving %s from %s to %s' % (wallpaperRealName, wallpaperTempPath, wallpaperPath))
os.rename(wallpaperTempPath, wallpaperPath)
service.saveRecord(processDate, wallpaperRealName)
lock.release()
break
else:
fetchAttempCount = fetchAttempCount + 1
sleep=settings['failure-sleep-time']
log.warning('Encountered error while fetching wallpaper, retry again in %s seconds' % sleep)
time.sleep(int(settings['failure-sleep-time']))
break
else:
marketAttempCount = marketAttempCount + 1
sleep=settings['failure-sleep-time']
log.error('Encountered error while fetching XML for %s, retry again in %s seconds' % (market, sleep))
time.sleep(int(settings['failure-sleep-time']))
time.sleep(2)
threadList = []
for market in markets:
time.sleep(0.25)
threadName = market
thread = threading.Thread(target=fetchWallpaperFromMarket, name=threadName, args=(market,))
thread.start()
threadList.append(thread)
while (len(threadList) > 0):
#log.info('Still has ' + str(len(threadList)) + ' thread running')
while (len(threadList) > 0 and not threadList[0].is_alive()): del threadList[0]
time.sleep(2)
log.info('Process is done, exit program')