-
Notifications
You must be signed in to change notification settings - Fork 7
/
script.py
286 lines (248 loc) · 10.1 KB
/
script.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2012 Tristan Fischer ([email protected])
#
# 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 3 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, see <http://www.gnu.org/licenses/>.
#
import os
import re
import sys
import urllib
import xbmc
import xbmcaddon
import xbmcgui
from thebigpictures import ScraperManager, ALL_SCRAPERS
addon = xbmcaddon.Addon()
addon_path = addon.getAddonInfo('path')
addon_name = addon.getAddonInfo('name')
class Downloader(object):
def __init__(self, download_path):
self.log('__init__ with path="%s"' % download_path)
self.download_path = download_path
self.pDialog = xbmcgui.DialogProgress()
self.pDialog.create(addon.getAddonInfo('name'))
self.pDialog.update(1, addon.getLocalizedString(32301))
def download(self, photos):
self.total_items = len(photos)
album_title = photos[0]['album_title']
album_title = re.sub('[^\w\- ]', '', album_title).replace(' ', '_')
full_path = os.path.join(self.download_path, album_title)
self.log('using full_path="%s"' % full_path)
line3 = addon.getLocalizedString(32304) % full_path
self.pDialog.update(2, '', '', line3)
if not os.path.isdir(full_path):
os.mkdir(full_path)
for i, photo in enumerate(photos):
self.current_item = i + 1
url = photo['pic']
self.current_file = '%d.%s' % (
self.current_item, url.rsplit('.', 1)[-1]
)
filename = os.path.join(full_path, self.current_file)
self.log('Downloading "%s" to "%s"' % (url, filename))
try:
urllib.urlretrieve(url, filename, self.update_progress)
except IOError, e:
self.log('ERROR: "%s"' % str(e))
break
self.log('Done')
if self.pDialog.iscanceled():
self.log('Canceled')
break
def update_progress(self, block_count, block_size, item_size):
overall_percent = int(self.current_item * 100 / self.total_items)
item_percent = int(block_count * block_size * 100 / item_size)
line1 = addon.getLocalizedString(32302) % (
self.current_item,
self.total_items
)
line2 = addon.getLocalizedString(32303) % (
self.current_file,
item_percent
)
self.pDialog.update(overall_percent, line1, line2)
def log(self, msg):
xbmc.log('TheBigPictures Downloader: %s' % msg)
def __del__(self):
self.pDialog.close()
class GUI(xbmcgui.WindowXML):
CONTROL_MAIN_IMAGE = 100
CONTROL_VISIBLE = 102
CONTROL_ASPECT_KEEP = 103
CONTROL_ARROWS = 104
CONTROL_BG = 105
ACTION_CONTEXT_MENU = [117]
ACTION_PREVIOUS_MENU = [9, 92, 10]
ACTION_SHOW_INFO = [11]
ACTION_EXIT_SCRIPT = [13]
ACTION_DOWN = [4]
ACTION_UP = [3]
ACTION_0 = [58, 18]
ACTION_PLAY = [79]
def __init__(self, skin_file, addon_path):
self.log('__init__')
self.getScrapers()
def getScrapers(self):
enabled_scrapers = []
for scraper in ALL_SCRAPERS:
if addon.getSetting('enable_%s' % scraper) == 'true':
enabled_scrapers.append(scraper)
self.scraper_manager = ScraperManager(enabled_scrapers)
def onInit(self):
self.log('onInit')
self.show_info = True
self.aspect_keep = True
self.last_seen_album_id = 0
self.getControls()
if addon.getSetting('show_arrows') == 'false':
self.arrows_controller.setVisible(False)
if addon.getSetting('aspect_ratio2') == '0':
self.aspect_controller.setVisible(False)
if addon.getSetting('black_background') == 'true':
if self.bg_controller:
self.bg_controller.setVisible(False)
self.showHelp()
self.showAlbums()
self.setFocus(self.image_list)
self.log('onInit finished')
def getControls(self):
self.window = xbmcgui.Window(xbmcgui.getCurrentWindowId())
self.image_list = self.getControl(self.CONTROL_MAIN_IMAGE)
self.arrows_controller = self.getControl(self.CONTROL_ARROWS)
self.aspect_controller = self.getControl(self.CONTROL_ASPECT_KEEP)
self.info_controller = self.getControl(self.CONTROL_VISIBLE)
try:
self.bg_controller = self.getControl(self.CONTROL_BG)
except RuntimeError:
# catch exception with skins which override the xml
# but are not up2date like Aeon Nox
self.bg_controller = None
def onAction(self, action):
action_id = action.getId()
if action_id in self.ACTION_SHOW_INFO:
self.toggleInfo()
elif action_id in self.ACTION_CONTEXT_MENU:
self.download_album()
elif action_id in self.ACTION_PREVIOUS_MENU:
if self.getWindowProperty('Category') == 'Album':
self.close()
elif self.getWindowProperty('Category') == 'Photo':
self.showAlbums(self.last_seen_album_id)
elif action_id in self.ACTION_EXIT_SCRIPT:
self.close()
elif action_id in self.ACTION_DOWN:
if self.getWindowProperty('Category') == 'Album':
self.scraper_manager.next()
self.showAlbums()
elif action_id in self.ACTION_UP:
if self.getWindowProperty('Category') == 'Album':
self.scraper_manager.previous()
self.showAlbums()
elif action_id in self.ACTION_0:
self.toggleAspect()
elif action_id in self.ACTION_PLAY:
self.startSlideshow()
def onClick(self, controlId):
if controlId == self.CONTROL_MAIN_IMAGE:
if self.getWindowProperty('Category') == 'Album':
self.showPhotos()
elif self.getWindowProperty('Category') == 'Photo':
self.toggleInfo()
def showPhotos(self):
self.log('showPhotos')
self.last_seen_album_id = int(self.getItemProperty('album_id'))
album_url = self.getItemProperty('album_url')
self.addItems(self.scraper_manager.get_photos(album_url))
self.setWindowProperty('Category', 'Photo')
self.log('showPhotos finished')
def showAlbums(self, switch_to_album_id=0):
self.log('showAlbums started with switch to album_id: %s'
% switch_to_album_id)
self.addItems(self.scraper_manager.get_albums())
if switch_to_album_id:
self.image_list.selectItem(switch_to_album_id)
self.setWindowProperty('Category', 'Album')
self.log('showAlbums finished')
def addItems(self, items):
self.log('addItems')
self.image_list.reset()
for item in items:
li = xbmcgui.ListItem(
label=item['title'],
label2=item['description'],
iconImage=item['pic']
)
li.setProperty(
'album_title',
self.scraper_manager.current_scraper.title
)
li.setProperty('album_url', item.get('album_url'))
li.setProperty('album_id', str(item.get('album_id')))
self.image_list.addItem(li)
def getItemProperty(self, key):
return self.image_list.getSelectedItem().getProperty(key)
def getWindowProperty(self, key):
return self.window.getProperty(key)
def setWindowProperty(self, key, value):
return self.window.setProperty(key, value)
def toggleInfo(self):
self.show_info = not self.show_info
self.info_controller.setVisible(self.show_info)
def toggleAspect(self):
self.aspect_keep = not self.aspect_keep
self.aspect_controller.setVisible(self.aspect_keep)
def startSlideshow(self):
self.log('startSlideshow')
params = {
'scraper_id': self.scraper_manager._current_index,
'album_url': self.getItemProperty('album_url')
}
if addon.getSetting('random_slideshow') == 'true':
random = 'random'
else:
random = 'notrandom'
url = 'plugin://%s/?%s' % (addon.getAddonInfo('id'),
urllib.urlencode(params))
self.log('startSlideshow using url=%s' % url)
xbmc.executebuiltin('Slideshow(%s, recursive, %s)'
% (url, random))
self.log('startSlideshow finished')
def download_album(self):
self.log('download_album')
download_path = addon.getSetting('download_path')
if not download_path:
s = addon.getLocalizedString(32300) # Choose default download path
new_path = xbmcgui.Dialog().browse(3, s, 'pictures')
if not new_path:
return
else:
download_path = new_path
addon.setSetting('download_path', download_path)
self.log('download_album using download_path="%s"' % download_path)
downloader = Downloader(download_path)
album_url = self.getItemProperty('album_url')
items = self.scraper_manager.get_photos(album_url)
downloader.download(items)
del downloader
self.log('download_album finished')
def showHelp(self):
if not addon.getSetting('dont_show_help') == 'true':
addon.openSettings()
def log(self, msg):
xbmc.log('TheBigPictures GUI: %s' % msg)
if __name__ == '__main__':
gui = GUI(u'script-%s-main.xml' % addon_name, addon_path).doModal()
del gui
sys.modules.clear()