/
bp_interfacelift.py
executable file
·195 lines (166 loc) · 6.23 KB
/
bp_interfacelift.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# bp_interfacelift.py
#
# Copyright 2010 Bruno Paulin <brunopaulin@bpaulin.net>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
########## SETTINGS
# Resolutions to choose among this list
# 2560x1600, 2560x1440, 2560x1024, 1920x1200, 1920x1080, 1680x1050, 1600x1200
# 1600x900, 1440x900, 1400x1050, 1280x1024, 1280x960, 1280x800, 1280x720
# 1024x1024, 1024x768, 1024x600, 800x480, 640x960, 480x272, 320x480, 320x240
Resolutions = ('1680x1050','1280x1024','1024x768')
# Directory in which the 'wallpapers' directory will be
SaveTo = 'Images'
# interfacelift feed url
UrlXml = 'http://feeds.feedburner.com/InterfaceliftNewestWallpaper?format=xml'
# Picture url (%s will be the picture name)
UrlPicture = 'http://interfacelift.com/wallpaper_beta/grab/%s'
#
UrlDirPreview = 'http://interfacelift.com/wallpaper_beta/previews/'
########## SETTINGS
from xml.dom.minidom import parse
import os
import urllib
import getopt, sys
def GetValueTag(name_tag,root_tag):#############################################
tag = root_tag.getElementsByTagName(name_tag)
if tag:
tag = tag[0]
first_child = tag.firstChild
return None if not first_child else first_child.data
else:
return None
#############################################def GetValueTag(name_tag,root_tag):
def GetImage(url, path_save):##################################################
print u"Downloading %s" % (path_save)
img_obj = urllib.urlopen(url)
img_data = img_obj.read()
# Saving picture
if img_data :
file_obj = open(path_save, 'w')
file_obj.write(img_data)
file_obj.close()
#######################################################def GetImage(url, name):
def CheckDir():#################################################################
for reso in Resolutions:
dir = os.path.join(os.path.expanduser('~'), SaveTo, 'Wallpapers')
# Making directories
try:
os.makedirs(os.path.join(dir,reso))
except OSError: # Directory exists
pass
return dir
#################################################################def CheckDir():
def Download():#################################################################
# Checking directories
dir = CheckDir()
# Opening xml
page = urllib.urlopen(UrlXml)
doc = parse(page)
items = doc.getElementsByTagName('item')
# For each pictures
for item in items:
# Checking resolutions
descri = GetValueTag('description', item)
if not descri:
continue
check_reso = [reso for reso in Resolutions if reso in descri]#resolutions wanted for this picture
if check_reso:
# Getting picture's details
title = GetValueTag('title', item)#picture title
guid = GetValueTag('guid', item)#picture's page url
if not(title and guid):
continue
print title
name = [preview for preview in descri.split(' ') if preview.count('src="%s'%UrlDirPreview)][0]\
.replace('src="%s'%UrlDirPreview,'').replace('.jpg"', '')
# For each resolutions
for reso in check_reso:
# Checking if file exists
filename = '%s_%s.jpg' %( name, reso)
file_path_save = os.path.join(dir,reso,filename)
if not os.path.isfile(file_path_save):
# Downloading picture
url = UrlPicture % filename
GetImage(url, file_path_save)
#################################################################def Download():
def Synchro():##################################################################
print u"Synchro"
dir = CheckDir()
# Getting pictures list
images = list()
for reso in Resolutions:
liste = os.listdir(os.path.join(dir,reso))
liste = [os.path.splitext(item)[0].rstrip(reso) for item in liste]
images.extend(liste)
images = list(set(images))
images.sort()
# Downloading missing pictures
for item in images:
for reso in Resolutions:
filename = '%s%s.jpg' %( item, reso)
file_path_save = os.path.join(dir,reso,filename)
if not os.path.isfile(file_path_save):
# Downloading picture
url = UrlPicture % filename
GetImage(url, file_path_save)
##################################################################def Synchro():
def Help(problem):##############################################################
print """
bp_interfacelift comes with ABSOLUTELY NO WARRANTY. This is free software, and you are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
bp_interfacelift is a wallpaper download program for several resolutions. Pictures are downloaded from http://www.interfacelift.com
Options
-d --download Download new pictures
-s --synchro Download pictures missing for some resolution
-h --help Print this message
"""
sys.exit(problem)
##############################################################def Help(problem):
def Arguments():################################################################
download = False
synchro = False
help = False
problem = False
try:
opts, args = getopt.getopt(sys.argv[1:], "hds", ["help", "download", "synchro"])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
problem = True
else:
for o, a in opts:
if o in ("-h", "--help"):
help = True
elif o in ("-s", "--synchro"):
synchro = True
elif o in ("-d", "--download"):
download = True
return (problem, download, synchro, help)
################################################################def Arguments():
if __name__ == '__main__':
(problem, download, synchro, help) = Arguments()
if problem:
Help(1)
elif help:
Help(0)
else:
if (not synchro or download):
Download()
if synchro:
Synchro()