-
Notifications
You must be signed in to change notification settings - Fork 10
/
xstream.py
441 lines (409 loc) · 16.9 KB
/
xstream.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# -*- coding: utf-8 -*-
from resources.lib.handler.pluginHandler import cPluginHandler
from resources.lib.handler.ParameterHandler import ParameterHandler
from resources.lib.gui.gui import cGui
from resources.lib.gui.guiElement import cGuiElement
from resources.lib.config import cConfig
from resources.lib import logger
import xbmc
import xbmcgui
import sys
# Main starting function
def run():
parseUrl()
def changeWatched(params):
if not cConfig().getSetting('metahandler')=='true': return
#videoType, name, imdbID, season=season, episode=episode, year=year, watched=watched
meta = get_metahandler()
if meta:
season = ''
episode = ''
mediaType = params.getValue('mediaType')
imdbID = params.getValue('imdbID')
name = params.getValue('title')
if params.exist('season'):
season = params.getValue('season')
if params.exist('episode'):
episode = params.getValue('episode')
if imdbID:
meta.change_watched(mediaType, name, imdbID, season=season, episode=episode)
xbmc.executebuiltin("XBMC.Container.Refresh")
else:
META = False
logger.info("Could not import package 'metahandler'")
return
def updateMeta(params):
if not cConfig().getSetting('metahandler')=='true':
return
# videoType, name, imdbID, season=season, episode=episode, year=year, watched=watched
try:
from metahandler import metahandlers
except Exception as e:
logger.info("Could not import package 'metahandler'")
logger.info(e)
return
meta = get_metahandler()
if not meta:
return
season = ''
episode = ''
mediaType = params.getValue('mediaType')
imdbID = params.getValue('imdbID')
name = str(params.getValue('title'))
year = params.getValue('year')
logger.info("MediaType: "+mediaType)
if mediaType == 'movie' or mediaType == 'tvshow':
# show meta search input
oGui = cGui()
sSearchText = oGui.showKeyBoard(name)
if not sSearchText: return
if mediaType == 'movie':
try:
foundInfo = meta.search_movies(sSearchText)
except:
logger.info('error or nothing found')
foundInfo = False
elif mediaType == 'tvshow':
foundInfo = metahandlers.TheTVDB().get_matching_shows(sSearchText, language="all", want_raw=True)
else:
return
if not foundInfo:
oGui.showInfo('xStream', 'Suchanfrage lieferte kein Ergebnis')
return
# select possible match
dialog = xbmcgui.Dialog()
items = []
for item in foundInfo:
if mediaType == 'movie':
items.append(str(item['title'].encode('utf-8'))+' ('+str(item['year'])+')')
elif mediaType == 'tvshow':
if 'FirstAired' in item:
items.append(item['SeriesName']+' ('+str(item['FirstAired'])[:4]+') ' + item.get('language',''))
else:
items.append(item['SeriesName']+' '+item.get('language',''))
else:
return
index = dialog.select('Film/Serie wählen', items)
if index > -1:
item = foundInfo[index]
else:
return False
if not imdbID:
imdbID = ''
if not year:
year = ''
if mediaType == 'movie':
meta.update_meta(mediaType, name, imdbID, new_imdb_id=str(item['imdb_id']), new_tmdb_id=str(item['tmdb_id']), year=year)
elif mediaType == 'tvshow':
if params.exist('season'):
season = params.getValue('season')
meta.update_season(name, imdbID, season)
if params.exist('episode'):
episode = params.getValue('episode')
if season and episode:
meta.update_episode_meta(name, imdbID, season, episode)
elif season:
meta.update_season(name, imdbID, season)
else:
meta.update_meta(mediaType, name, imdbID, new_imdb_id=str(item.get('IMDB_ID','')), new_tmdb_id=str(item['id']), year=year)
xbmc.executebuiltin("XBMC.Container.Refresh")
return
def get_metahandler():
from resources.lib.bs_finalizer import l1111
try:
from metahandler import metahandlers
return metahandlers.MetaData(tmdb_api_key=l1111(u"࠻࠺ࡩࡪ࠱࠹ࡤ࠳࠸࠽࠽࠴ࡥ࠻ࡦ࠽࠹ࡧࡦࡢࡦࡧࡩ࠼࠹࠴ࡦ࠼࠸ࡪ࠹ࠦ"))
except Exception as e:
logger.info("Could not import package 'metahandler'")
logger.info(e)
return False
def parseUrl():
import urlparse
netloc = [urlparse.urlparse(sys.argv[0]).netloc, '']
if xbmc.getInfoLabel('Container.PluginName') not in netloc:
sys.exit()
return
params = ParameterHandler()
logger.info(params.getAllParameters())
# If no function is set, we set it to the default "load" function
if params.exist('function'):
sFunction = params.getValue('function')
if sFunction == 'spacer':
return True
elif sFunction == 'clearCache':
from resources.lib.handler.requestHandler import cRequestHandler
cRequestHandler('dummy').clearCache()
return
elif sFunction == 'changeWatched':
changeWatched(params)
return
elif sFunction == 'updateMeta':
updateMeta(params)
return
elif sFunction == 'searchAlter':
searchAlter(params)
return
elif sFunction == 'updateXstream':
from resources.lib import updateManager
updateManager.xStreamUpdate()
return
elif sFunction == 'updateUrlResolver':
from resources.lib import updateManager
updateManager.urlResolverUpdate()
return
elif sFunction == 'updateAll':
from resources.lib import updateManager
updateManager.xStreamUpdate()
updateManager.urlResolverUpdate()
return
elif params.exist('remoteplayurl'):
try:
import urlresolver
remotePlayUrl = params.getValue('remoteplayurl')
sLink = urlresolver.resolve(remotePlayUrl)
if sLink:
xbmc.executebuiltin( "PlayMedia(" + sLink + ")" )
else:
logger.info("Could not play remote url '%s'" % (sLink))
except urlresolver.resolver.ResolverError as e:
logger.info('ResolverError: %s' % e)
return
else:
sFunction = 'load'
# Test if we should run a function on a special site
if not params.exist('site'):
xbmc.executebuiltin('XBMC.RunPlugin(%s?function=clearCache)' % sys.argv[0])
xStreamUpdate = True if cConfig().getSetting('githubUpdateXstream') == 'true' else False
urlResolverUpdate = True if cConfig().getSetting('githubUpdateUrlResolver') == 'true' else False
if xStreamUpdate and urlResolverUpdate:
xbmc.executebuiltin('XBMC.RunPlugin(%s?function=updateAll)' % sys.argv[0])
elif xStreamUpdate:
xbmc.executebuiltin('XBMC.RunPlugin(%s?function=updateXstream)' % sys.argv[0])
elif urlResolverUpdate:
xbmc.executebuiltin('XBMC.RunPlugin(%s?function=updateUrlResolver)' % sys.argv[0])
# As a default if no site was specified, we run the default starting gui with all plugins
showMainMenu(sFunction)
return
sSiteName = params.getValue('site')
if params.exist('playMode'):
from resources.lib.gui.hoster import cHosterGui
url = False
playMode = params.getValue('playMode')
isHoster = params.getValue('isHoster')
url = params.getValue('url')
manual = params.exist('manual')
if cConfig().getSetting('hosterSelect')=='Auto' and playMode != 'jd' and playMode != 'jd2' and playMode != 'pyload' and not manual:
cHosterGui().streamAuto(playMode, sSiteName, sFunction)
else:
cHosterGui().stream(playMode, sSiteName, sFunction, url)
return
logger.info("Call function '%s' from '%s'" % (sFunction, sSiteName))
# If the hoster gui is called, run the function on it and return
if sSiteName == 'cHosterGui':
showHosterGui(sFunction)
# If global search is called
elif sSiteName == 'globalSearch':
searchterm = False
if params.exist('searchterm'):
searchterm = params.getValue('searchterm')
searchGlobal(searchterm)
elif sSiteName == 'favGui':
showFavGui(sFunction)
# If addon settings are called
elif sSiteName == 'xStream':
oGui = cGui()
oGui.openSettings()
oGui.updateDirectory()
# If the urlresolver settings are called
elif sSiteName == 'urlresolver':
import urlresolver
urlresolver.display_settings()
# If metahandler settings are called
elif sSiteName == 'metahandler':
import metahandler
metahandler.display_settings()
elif sSiteName == 'settings':
oGui = cGui()
for folder in settingsGuiElements():
oGui.addFolder(folder)
oGui.setEndOfDirectory()
else:
# Else load any other site as plugin and run the function
plugin = __import__(sSiteName, globals(), locals())
function = getattr(plugin, sFunction)
function()
def showMainMenu(sFunction):
oGui = cGui()
if cConfig().getSetting('GlobalSearchPosition') == 'true':
oGui.addFolder(globalSearchGuiElement())
oPluginHandler = cPluginHandler()
aPlugins = oPluginHandler.getAvailablePlugins()
if not aPlugins:
logger.info("No (activated) Plugins found")
# Open the settings dialog to choose a plugin that could be enabled
oGui.openSettings()
oGui.updateDirectory()
else:
# Create a gui element for every plugin found
for aPlugin in sorted(aPlugins, key=lambda k: k['id']):
oGuiElement = cGuiElement()
oGuiElement.setTitle(aPlugin['name'])
oGuiElement.setSiteName(aPlugin['id'])
oGuiElement.setFunction(sFunction)
if 'icon' in aPlugin and aPlugin['icon']:
oGuiElement.setThumbnail(aPlugin['icon'])
oGui.addFolder(oGuiElement)
if cConfig().getSetting('GlobalSearchPosition') == 'false':
oGui.addFolder(globalSearchGuiElement())
# Create a gui element for favorites
#oGuiElement = cGuiElement()
#oGuiElement.setTitle("Favorites")
#oGuiElement.setSiteName("FavGui")
#oGuiElement.setFunction("showFavs")
#oGuiElement.setThumbnail("DefaultAddonService.png")
#oGui.addFolder(oGuiElement)
if cConfig().getSetting('SettingsFolder') == 'true':
# Create a gui element for Settingsfolder
oGuiElement = cGuiElement()
oGuiElement.setTitle(cConfig().getLocalizedString(30041))
oGuiElement.setSiteName("settings")
oGuiElement.setFunction("showSettingsFolder")
oGuiElement.setThumbnail("DefaultAddonService.png")
oGui.addFolder(oGuiElement)
else:
for folder in settingsGuiElements():
oGui.addFolder(folder)
oGui.setEndOfDirectory()
def settingsGuiElements():
# Create a gui element for addon settings
oGuiElement = cGuiElement()
oGuiElement.setTitle(cConfig().getLocalizedString(30042))
oGuiElement.setSiteName("xStream")
oGuiElement.setFunction("display_settings")
oGuiElement.setThumbnail("DefaultAddonProgram.png")
xStreamSettings = oGuiElement
# Create a gui element for urlresolver settings
oGuiElement = cGuiElement()
oGuiElement.setTitle(cConfig().getLocalizedString(30043))
oGuiElement.setSiteName("urlresolver")
oGuiElement.setFunction("display_settings")
oGuiElement.setThumbnail("DefaultAddonRepository.png")
urlResolverSettings = oGuiElement
# Create a gui element for metahandler settings
oGuiElement = cGuiElement()
oGuiElement.setTitle(cConfig().getLocalizedString(30044))
oGuiElement.setSiteName("metahandler")
oGuiElement.setFunction("display_settings")
oGuiElement.setThumbnail("DefaultAddonTvInfo.png")
metaSettings = oGuiElement
if cConfig().getSetting('metahandler') == 'true':
return xStreamSettings, urlResolverSettings, metaSettings
return xStreamSettings, urlResolverSettings
def globalSearchGuiElement():
# Create a gui element for global search
oGuiElement = cGuiElement()
oGuiElement.setTitle(cConfig().getLocalizedString(30040))
oGuiElement.setSiteName("globalSearch")
oGuiElement.setFunction("globalSearch")
oGuiElement.setThumbnail("DefaultAddonWebSkin.png")
return oGuiElement
def showHosterGui(sFunction):
from resources.lib.gui.hoster import cHosterGui
oHosterGui = cHosterGui()
function = getattr(oHosterGui, sFunction)
function()
return True
#def showFavGui(functionName):
#from resources.lib.gui.favorites import FavGui
#oFavGui = FavGui()
#function = getattr(oFavGui, functionName)
#function()
#return True
def searchGlobal(sSearchText=False):
import threading
oGui = cGui()
oGui.globalSearch = True
oGui._collectMode = True
if not sSearchText:
sSearchText = oGui.showKeyBoard()
if not sSearchText: return True
aPlugins = []
aPlugins = cPluginHandler().getAvailablePlugins()
dialog = xbmcgui.DialogProgress()
dialog.create('xStream',"Searching...")
numPlugins = len(aPlugins)
threads = []
for count, pluginEntry in enumerate(aPlugins):
if not pluginEntry['globalsearch']:
continue
dialog.update((count+1)*50/numPlugins,'Searching: '+str(pluginEntry['name'])+'...')
logger.info('Searching for %s at %s' % (sSearchText.decode('utf-8'), pluginEntry['id']))
t = threading.Thread(target=_pluginSearch, args=(pluginEntry,sSearchText,oGui), name =pluginEntry['name'])
threads += [t]
t.start()
for count, t in enumerate(threads):
t.join()
dialog.update((count+1)*50/numPlugins+50, t.getName()+' returned')
dialog.close()
# deactivate collectMode attribute because now we want the elements really added
oGui._collectMode = False
total=len(oGui.searchResults)
dialog = xbmcgui.DialogProgress()
dialog.create('xStream',"Gathering info...")
for count,result in enumerate(sorted(oGui.searchResults, key=lambda k: k['guiElement'].getSiteName()),1):
oGui.addFolder(result['guiElement'],result['params'],bIsFolder=result['isFolder'],iTotal=total)
dialog.update((count)*100/total, str(count)+' of '+str(total)+': '+result['guiElement'].getTitle())
dialog.close()
oGui.setView()
oGui.setEndOfDirectory()
return True
def searchAlter(params):
searchTitle = params.getValue('searchTitle')
searchImdbId = params.getValue('searchImdbID')
searchYear = params.getValue('searchYear')
import threading
oGui = cGui()
oGui.globalSearch = True
oGui._collectMode = True
aPlugins = []
aPlugins = cPluginHandler().getAvailablePlugins()
dialog = xbmcgui.DialogProgress()
dialog.create('xStream',"Searching...")
numPlugins = len(aPlugins)
threads = []
for count, pluginEntry in enumerate(aPlugins):
dialog.update((count+1)*50/numPlugins,'Searching: '+str(pluginEntry['name'])+'...')
logger.info('Searching for ' + searchTitle + pluginEntry['id'].encode('utf-8'))
t = threading.Thread(target=_pluginSearch, args=(pluginEntry,searchTitle, oGui), name =pluginEntry['name'])
threads += [t]
t.start()
for count, t in enumerate(threads):
t.join()
dialog.update((count+1)*50/numPlugins+50, t.getName()+' returned')
dialog.close()
#check results, put this to the threaded part, too
filteredResults = []
for result in oGui.searchResults:
guiElement = result['guiElement']
logger.info('Site: %s Titel: %s' % (guiElement.getSiteName(), guiElement.getTitle()))
if not searchTitle in guiElement.getTitle(): continue
if guiElement._sYear and searchYear and guiElement._sYear != searchYear: continue
if searchImdbId and guiElement.getItemProperties().get('imdbID',False) and guiElement.getItemProperties().get('imdbID',False) != searchImdbId: continue
filteredResults.append(result)
oGui._collectMode = False
total=len(filteredResults)
for result in sorted(filteredResults, key=lambda k: k['guiElement'].getSiteName()):
oGui.addFolder(result['guiElement'],result['params'],bIsFolder=result['isFolder'],iTotal=total)
oGui.setView()
oGui.setEndOfDirectory()
xbmc.executebuiltin('Container.Update')
return True
def _pluginSearch(pluginEntry, sSearchText, oGui):
try:
plugin = __import__(pluginEntry['id'], globals(), locals())
function = getattr(plugin, '_search')
function(oGui, sSearchText)
except:
logger.info(pluginEntry['name']+': search failed')
import traceback
logger.debug(traceback.format_exc())