[c0ea65]: / plugin.video.trailer.addict / addonfunc.py  Maximize  Restore  History

Download this file

187 lines (176 with data), 6.8 kB

  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
'''
Addon Functions
__author__ = 'stacked <stacked.xbmc@gmail.com>'
__url__ = 'http://code.google.com/p/plugin/'
__date__ = '06-01-2013'
__version__ = '0.0.10'
'''
import xbmc, xbmcgui, xbmcaddon, xbmcplugin, urllib, urllib2, sys, time, datetime, buggalo
from urlparse import urlparse
from os.path import splitext, basename
settings = sys.modules["__main__"].settings
plugin = sys.modules["__main__"].plugin
useragent = 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0'
import SimpleDownloader as downloader
downloader = downloader.SimpleDownloader()
def addListItem(label, image, url, isFolder, totalItems, infoLabels = False, fanart = False, duration = False, cm = False):
listitem = xbmcgui.ListItem(label = label, iconImage = image, thumbnailImage = image)
if url['mode']:
u = sys.argv[0] + '?' + urllib.urlencode(url)
else:
u = url['url']
if not isFolder:
if settings.getSetting('download') == '' or settings.getSetting('download') == 'false':
if label != '* ' + settings.getLocalizedString( 30030 ) + ' *':
listitem.setProperty('IsPlayable', 'true')
if settings.getSetting('playall') == 'true':
playlist = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
playlist.add(url = u, listitem = listitem)
if fanart:
listitem.setProperty('fanart_image', fanart)
if cm:
listitem.addContextMenuItems( cm )
if infoLabels:
listitem.setInfo(type = 'video', infoLabels = infoLabels)
if duration:
if hasattr(listitem, 'addStreamInfo'):
listitem.addStreamInfo('video', { 'duration': int(duration) })
else:
listitem.setInfo(type = 'video', infoLabels = { 'duration': str(datetime.timedelta(milliseconds=int(duration)*1000)) } )
ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = isFolder, totalItems = int(totalItems))
return ok
def playListItem(label, image, path, infoLabels, PlayPath = False):
listitem = xbmcgui.ListItem(label = label, iconImage = image, thumbnailImage = image, path = path)
listitem.setInfo(type = 'video', infoLabels = infoLabels)
if PlayPath: listitem.setProperty('PlayPath', PlayPath)
xbmcplugin.setResolvedUrl(handle = int(sys.argv[1]), succeeded = True, listitem = listitem)
def getUrl(url, gzip = False):
retries = 0
while retries <= 10:
data = {'content': None, 'error': None}
try:
if retries != 0:
time.sleep(3)
data = getPage(url, gzip)
if data['content'] != None and data['error'] == None:
return data['content']
if data['error'].find('404:') != -1 or data['error'].find('400:') != -1:
break
except Exception, e:
data['error'] = str(e)
retries += 1
dialog = xbmcgui.Dialog()
ret = dialog.yesno(plugin, settings.getLocalizedString( 30050 ), data['error'], '', settings.getLocalizedString( 30052 ), settings.getLocalizedString( 30053 ))
if ret == False:
getUrl(url)
else:
ok = dialog.ok(plugin, settings.getLocalizedString( 30051 ))
buggalo.addExtraData('url', url)
buggalo.addExtraData('error', data['error'])
raise Exception('getUrl Error')
def getPage(url, gzip = False):
data = {'content': None, 'error': None}
try:
req = urllib2.Request(url)
req.add_header('User-Agent', useragent)
content = urllib2.urlopen(req)
if gzip:
try:
if content.info()['Content-Encoding'] == 'gzip':
import gzip, StringIO
gzip_filehandle = gzip.GzipFile(fileobj=StringIO.StringIO(content.read()))
html = gzip_filehandle.read()
else:
html = content.read()
except:
html = content.read()
else:
html = content.read()
content.close()
try:
data['content'] = html.decode('utf-8')
return data
except:
data['content'] = html
return data
except Exception, e:
data['error'] = str(e)
return data
def setViewMode(id, type = False):
if type == False: type = 'episodes'
if xbmc.getSkinDir() == 'skin.confluence':
xbmcplugin.setContent(int( sys.argv[1] ), type)
if settings.getSetting('view') == 'true':
xbmc.executebuiltin('Container.SetViewMode(' + id + ')')
def start_download(name, path):
try:
ext = splitext(basename(urlparse(path).path))[1]
except:
ext = '.mp4'
dialog = xbmcgui.Dialog()
#dir = dialog.browse(0, settings.getLocalizedString( 30059 ), 'files', '', False, False, settings.getSetting('downloadPath'))
if settings.getSetting('downloadPrompt') == 'true':
dir = dialog.browse(0, settings.getLocalizedString( 30059 ), 'files')
else:
while not settings.getSetting('downloadPath'):
ok = dialog.ok(plugin, settings.getLocalizedString( 30058 ))
settings.openSettings()
if settings.getSetting('downloadPrompt') == 'true':
dir = dialog.browse(0, 'XBMC', 'files')
settings.setSetting('downloadPath', dir)
dir = settings.getSetting('downloadPath')
if len(dir) == 0:
return
params = { "url": path, "download_path": dir, "Title": name }
downloader.download(clean_file(name) + ext, params)
def clean_file(name):
remove=[('\"',''),('\\',''),('/',''),(':',' - '),('|',''),('>',''),('<',''),('?',''),('*','')]
for old, new in remove:
name=name.replace(old,new)
return name
#From http://wiki.xbmc.org/index.php?title=Add-on:Parsedom_for_xbmc_plugins
def getParameters(parameterString):
commands = {}
splitCommands = parameterString[parameterString.find('?') + 1:].split('&')
for command in splitCommands:
if (len(command) > 0):
splitCommand = command.split('=')
key = splitCommand[0]
value = splitCommand[1]
commands[key] = value
return commands
#From http://www.saltycrane.com/blog/2009/11/trying-out-retry-decorator-python/
def retry(ExceptionToCheck, tries = 10, delay = 3, backoff = 1, logger = None):
def deco_retry(f):
def f_retry(*args, **kwargs):
mtries, mdelay = tries, delay
try_one_last_time = True
while mtries >= -1:
if mtries == -1:
dialog = xbmcgui.Dialog()
ret = dialog.yesno(plugin, settings.getLocalizedString( 30054 ), '', '', settings.getLocalizedString( 30052 ), settings.getLocalizedString( 30053 ))
if ret == False:
mtries, mdelay = tries, delay
else:
ok = dialog.ok(plugin, settings.getLocalizedString( 30051 ))
buggalo.addExtraData('error', str(e))
raise Exception("retry Error")
try:
return f(*args, **kwargs)
try_one_last_time = False
break
except ExceptionToCheck, e:
if mtries >= 1:
msg = "%s, Retrying in %d seconds..." % (str(e), mdelay)
if logger:
logger.warning(msg)
else:
print msg
time.sleep(mdelay)
mdelay *= backoff
mtries -= 1
if try_one_last_time:
return f(*args, **kwargs)
return
return f_retry
return deco_retry