1 |
|
2 |
# This Python file uses the following encoding: utf-8 |
3 |
|
4 |
''' |
5 |
Todic plugin for XBMC |
6 |
Version 0.0.15 |
7 |
''' |
8 |
|
9 |
import sys |
10 |
import cgi as urlparse |
11 |
import os |
12 |
|
13 |
|
14 |
import xbmc |
15 |
import xbmcaddon |
16 |
import xbmcgui |
17 |
import xbmcplugin |
18 |
import urllib |
19 |
import urllib2 |
20 |
|
21 |
#import pprint |
22 |
|
23 |
from xml.dom.minidom import parseString |
24 |
|
25 |
__addon__ = xbmcaddon.Addon(id='plugin.video.todic') |
26 |
__key__ = __addon__.getSetting('xbmckey').lower() |
27 |
__backend__ = "http://todic.dk/xbmc.php?xbmckey=" + __key__ |
28 |
fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'movie_bg_blur.jpg') |
29 |
datapath = xbmc.translatePath('special://profile/addon_data/plugin.video.todic/') |
30 |
|
31 |
ADDON_PATH = __addon__.getAddonInfo('path') |
32 |
SkinMasterPath = os.path.join(ADDON_PATH, 'skins' ) + '/' |
33 |
MySkinPath = (os.path.join(SkinMasterPath, '720p')) + '/' |
34 |
MySkin = 'main.xml' |
35 |
|
36 |
|
37 |
class TodicMovieDialog(xbmcgui.WindowXMLDialog): |
38 |
def __new__(cls): |
39 |
return super(TodicMovieDialog, cls).__new__(cls, "main.xml", ADDON_PATH) |
40 |
|
41 |
def __init__(self): |
42 |
super(TodicMovieDialog, self).__init__() |
43 |
|
44 |
def onClick( self, controlId ): |
45 |
print "OnClick: " + str(controlId) |
46 |
|
47 |
if (controlId == 50): |
48 |
self.close() |
49 |
play_real_video(self.url, self.name) |
50 |
|
51 |
if ( controlId == 98 ): |
52 |
self.close() |
53 |
|
54 |
|
55 |
def onInit(self): |
56 |
|
57 |
print "ONINIT" |
58 |
self.getControl( 1 ).setLabel( self.name); |
59 |
self.getControl( 2 ).setLabel( self.moviegroups ) |
60 |
self.getControl( 3 ).setLabel( self.description ) |
61 |
self.getControl( 10 ).setLabel( self.playlength ) |
62 |
self.getControl( 11 ).setLabel( self.codecdetails ) |
63 |
|
64 |
|
65 |
|
66 |
orig_img_width = self.getControl(40).getWidth() |
67 |
self.starwidth = ( float(self.imdbrating) / 10.0) * orig_img_width; |
68 |
self.getControl(40).setWidth( int(self.starwidth) ) |
69 |
|
70 |
def setUrl( self, url): |
71 |
print "SETURL:" + url |
72 |
self.url = url |
73 |
self.fetchClipDetails() |
74 |
|
75 |
|
76 |
def fetchClipDetails(self): |
77 |
param1 = parse_parameter_string( self.url ) |
78 |
|
79 |
self.clipkey = param1[ "clipkey" ] |
80 |
print "CLIPKEY:" + self.clipkey |
81 |
detailurl = __backend__ + "&action=clipdetails&clipkey=" + self.clipkey |
82 |
|
83 |
xml = open_url(detailurl) |
84 |
|
85 |
doc = parseString(xml) |
86 |
self.imdbrating = getText( doc.getElementsByTagName("imdbrating") ) |
87 |
self.moviegroups = getText( doc.getElementsByTagName("moviegroups") ) |
88 |
self.playlength = getText( doc.getElementsByTagName("playlength") ) |
89 |
self.codecdetails = getText( doc.getElementsByTagName("codecdetails") ) |
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
def setName( self, name ): |
96 |
self.name = name |
97 |
|
98 |
def setDescription( self, description ): |
99 |
self.description = description |
100 |
|
101 |
|
102 |
class TodicPlayer(xbmc.Player): |
103 |
def __init__(self, *args, **kwargs): |
104 |
#xbmc.Player.__init__(selv,*args,**kwargs) |
105 |
xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER ) |
106 |
self.stopped = False |
107 |
self.started = False |
108 |
print "[TodicPlayer] init" |
109 |
|
110 |
# @catchall |
111 |
def onPlayBackStarted(self): |
112 |
self.started = True |
113 |
print "[TodicPlayer] : started" |
114 |
# super.onPlayBackStarted() |
115 |
|
116 |
def onPlayBackStopped(self): |
117 |
self.stopped = True |
118 |
print "[TodicPlayer] : stopped" |
119 |
|
120 |
def onPlayBackEnded(self): |
121 |
self.stopped = True |
122 |
print "[TodicPlayer] : ended" |
123 |
|
124 |
def callbackLoop(self): |
125 |
print "[Todic] startLoop" |
126 |
while (self.stopped == False): |
127 |
if (self.started == True ): |
128 |
print "[todic] " + str(self.getTime()) |
129 |
xbmc.sleep(5000) |
130 |
|
131 |
|
132 |
def getText2(nodelist): |
133 |
rc = [] |
134 |
for node in nodelist: |
135 |
if node.nodeType == node.TEXT_NODE: |
136 |
rc.append(node.data) |
137 |
else: |
138 |
rc.append( getText(node.childNodes) ) |
139 |
return ''.join(rc) |
140 |
|
141 |
def getText(nodelist): |
142 |
if nodelist.length == 0: |
143 |
return '' |
144 |
else: |
145 |
if nodelist[0].childNodes.length == 0: |
146 |
return '' |
147 |
else: |
148 |
return nodelist[0].childNodes[0].nodeValue |
149 |
|
150 |
def SaveFile(path, data): |
151 |
file = open(path,'w') |
152 |
file.write(data) |
153 |
file.close() |
154 |
|
155 |
|
156 |
def open_url(url): |
157 |
req = urllib2.Request(url) |
158 |
content = urllib2.urlopen(req) |
159 |
data = content.read() |
160 |
content.close() |
161 |
return data |
162 |
|
163 |
def rootMenu(): |
164 |
|
165 |
msg = open_url(__backend__ + "&action=messages") |
166 |
msg = msg.strip() |
167 |
|
168 |
if msg != "": |
169 |
dialog = xbmcgui.Dialog() |
170 |
dialog.ok('XBMC Todic', msg) |
171 |
|
172 |
buildList(__backend__, "", False) # call default list |
173 |
|
174 |
# Adde xtra items to root menu |
175 |
listitem = xbmcgui.ListItem(label = "Søg film ...", iconImage = 'DefaultFolder.png', thumbnailImage = 'DefaultFolder.png') |
176 |
listitem.setProperty('Fanart_Image', fanartImage) |
177 |
|
178 |
u = sys.argv[0] + "?mode=10&name=" |
179 |
ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = True) |
180 |
|
181 |
#add search series |
182 |
listitem = xbmcgui.ListItem(label = "Søg Serier ...", iconImage = 'DefaultFolder.png', thumbnailImage = 'DefaultFolder.png') |
183 |
listitem.setProperty('Fanart_Image', fanartImage) |
184 |
|
185 |
u = sys.argv[0] + "?mode=11&name=" |
186 |
ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = True) |
187 |
|
188 |
xbmcplugin.endOfDirectory(int(sys.argv[1])) |
189 |
|
190 |
|
191 |
def buildList(url,title, endlist=True): |
192 |
print '[TODIC]:'+str(url) |
193 |
|
194 |
link = open_url(url) |
195 |
doc = parseString(link) |
196 |
ty = doc.getElementsByTagName("meta")[0].getAttribute("type") |
197 |
print '[TODIC]'+str(ty) |
198 |
|
199 |
if ty == 'clipList': |
200 |
mode = '50' |
201 |
folder = False |
202 |
else: |
203 |
mode = '1' |
204 |
folder = True |
205 |
|
206 |
|
207 |
entries = doc.getElementsByTagName("entry") |
208 |
l=len(entries) |
209 |
description = '' |
210 |
for entry in entries: |
211 |
name = getText( entry.getElementsByTagName("title") ) |
212 |
url = getText( entry.getElementsByTagName("url") ) |
213 |
thumb = getText( entry.getElementsByTagName("cover") ) |
214 |
description = getText( entry.getElementsByTagName("description") ) |
215 |
playcount = getText( entry.getElementsByTagName("playcount") ) |
216 |
|
217 |
if playcount == '': |
218 |
playcount = '0' |
219 |
playcount = int(playcount) |
220 |
|
221 |
|
222 |
## print "name:" + name |
223 |
# print "url:" + url |
224 |
# print "thumb:" + thumb |
225 |
# print "description:" + description |
226 |
|
227 |
|
228 |
listitem = xbmcgui.ListItem(label = name, label2='test', iconImage = 'DefaultFolder.png', thumbnailImage = thumb) |
229 |
listitem.setProperty('Fanart_Image', fanartImage) |
230 |
if mode == '50': |
231 |
infoLabels = {} |
232 |
infoLabels['title'] = name |
233 |
infoLabels['plot'] = description |
234 |
infoLabels['playcount'] = playcount |
235 |
listitem.setInfo('video', infoLabels) |
236 |
|
237 |
name = name.encode('UTF-8') |
238 |
description = description.encode('UTF-8') |
239 |
|
240 |
|
241 |
u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description) |
242 |
ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = folder, totalItems = l) |
243 |
|
244 |
if (endlist == True): |
245 |
xbmcplugin.endOfDirectory(int(sys.argv[1])) |
246 |
|
247 |
|
248 |
|
249 |
|
250 |
def play_video(url, name,description): |
251 |
if (description == None or description == ""): |
252 |
play_real_video(url,name) |
253 |
else: |
254 |
d = TodicMovieDialog() |
255 |
d.setUrl( url) |
256 |
d.setName( name ) |
257 |
d.setDescription( description ) |
258 |
|
259 |
d.doModal() |
260 |
|
261 |
|
262 |
def play_real_video(url, name): |
263 |
xml = open_url(url) |
264 |
print 'TODIC url: ' + str(url) |
265 |
print 'TODIC xml: '+ xml |
266 |
|
267 |
doc = parseString(xml) |
268 |
url = getText( doc.getElementsByTagName("url") ) |
269 |
|
270 |
subtitleurl = getText( doc.getElementsByTagName("subtitles") ) |
271 |
subtitlesfile = os.path.join(datapath,'temp.srt') |
272 |
|
273 |
#if old srt file exists delete it first |
274 |
if os.path.isfile(subtitlesfile): |
275 |
os.unlink(subtitlesfile) |
276 |
|
277 |
print '[TODIC] subs: '+str(subtitleurl) |
278 |
if len(subtitleurl) > 0: |
279 |
subtitles = open_url(subtitleurl) |
280 |
SaveFile(subtitlesfile, subtitles) |
281 |
print 'TODIC downloaded subtitles' |
282 |
|
283 |
|
284 |
|
285 |
image = xbmc.getInfoImage( 'ListItem.Thumb' ) |
286 |
listitem = xbmcgui.ListItem(label = name , iconImage = 'DefaultVideo.png', thumbnailImage = image) |
287 |
listitem.setInfo( type = "Video", infoLabels={ "Title": name } ) |
288 |
|
289 |
player = TodicPlayer(xbmc.PLAYER_CORE_AUTO) |
290 |
player.play(str(url), listitem) |
291 |
|
292 |
#kan ikke loade subtitles hvis foerend playeren koerer |
293 |
count = 0 |
294 |
while not xbmc.Player().isPlaying(): |
295 |
xbmc.sleep(500) |
296 |
count += 1 |
297 |
if count > 10: |
298 |
break |
299 |
|
300 |
|
301 |
if xbmc.Player().isPlaying(): |
302 |
if os.path.isfile(subtitlesfile): |
303 |
player.setSubtitles(subtitlesfile) |
304 |
print 'TODIC started subtitles' |
305 |
else: |
306 |
player.disableSubtitles() |
307 |
|
308 |
# player.callbackLoop() |
309 |
|
310 |
|
311 |
|
312 |
def search(): |
313 |
search = getUserInput("Todic Søgning") |
314 |
|
315 |
if (search != None and search != ""): |
316 |
url = __backend__ + "&action=search&search=" + urllib.quote_plus(search) |
317 |
|
318 |
#print "[TODIC] Search start: " + search |
319 |
#print "[TODIC] Search url: " + url |
320 |
|
321 |
buildList(url, "søgning") |
322 |
|
323 |
def searchSeries(): |
324 |
search = getUserInput("Todic Serie Søgning") |
325 |
|
326 |
if (search != None and search != ""): |
327 |
url = __backend__ + "&action=searchseries&search=" + urllib.quote_plus(search) |
328 |
|
329 |
#print "[TODIC] Search start: " + search |
330 |
#print "[TODIC] Search url: " + url |
331 |
|
332 |
buildList(url, "serie søgning") |
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
#=================================== Tool Box ======================================= |
339 |
# shows a more userfriendly notification |
340 |
def showMessage(heading, message): |
341 |
duration = 15*1000 |
342 |
xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) ) |
343 |
|
344 |
|
345 |
# raise a keyboard for user input |
346 |
def getUserInput(title = "Input", default="", hidden=False): |
347 |
result = None |
348 |
|
349 |
# Fix for when this functions is called with default=None |
350 |
if not default: |
351 |
default = "" |
352 |
|
353 |
keyboard = xbmc.Keyboard(default, title) |
354 |
keyboard.setHiddenInput(hidden) |
355 |
keyboard.doModal() |
356 |
|
357 |
if keyboard.isConfirmed(): |
358 |
result = keyboard.getText() |
359 |
|
360 |
return result |
361 |
|
362 |
|
363 |
def get_params(): |
364 |
return parse_parameter_string( sys.argv[2] ) |
365 |
|
366 |
def parse_parameter_string( paramstring ): |
367 |
param=[] |
368 |
if len(paramstring)>=2: |
369 |
params = paramstring |
370 |
cleanedparams=params.replace('?','') |
371 |
if (params[len(params)-1]=='/'): |
372 |
params=params[0:len(params)-2] |
373 |
pairsofparams=cleanedparams.split('&') |
374 |
param={} |
375 |
for i in range(len(pairsofparams)): |
376 |
splitparams={} |
377 |
splitparams=pairsofparams[i].split('=') |
378 |
if (len(splitparams))==2: |
379 |
param[splitparams[0]]=splitparams[1] |
380 |
return param |
381 |
|
382 |
|
383 |
params = get_params() |
384 |
url = None |
385 |
name = None |
386 |
mode = None |
387 |
description = None |
388 |
|
389 |
try: |
390 |
url = urllib.unquote_plus(params["url"]) |
391 |
except: |
392 |
pass |
393 |
try: |
394 |
name = urllib.unquote_plus(params["name"]) |
395 |
except: |
396 |
pass |
397 |
try: |
398 |
mode = int(params["mode"]) |
399 |
except: |
400 |
pass |
401 |
try: |
402 |
description = urllib.unquote_plus(params["description"]) |
403 |
except: |
404 |
pass |
405 |
|
406 |
try: |
407 |
open_url("http://todic.dk") |
408 |
except: |
409 |
showMessage("Fejl", "Kunne ikke forbinde til todic.dk") |
410 |
exit() |
411 |
|
412 |
|
413 |
if url == 'refresh': |
414 |
#xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker ikke med XBMC12 |
415 |
xbmc.executebuiltin("Container.Refresh") |
416 |
|
417 |
|
418 |
elif mode == None: |
419 |
#build main menu |
420 |
rootMenu() |
421 |
|
422 |
elif mode == 1: |
423 |
#build list of movie starting letters |
424 |
buildList(url, name) |
425 |
|
426 |
elif mode == 10: |
427 |
search() |
428 |
|
429 |
elif mode == 11: |
430 |
searchSeries() |
431 |
|
432 |
|
433 |
elif mode == 50: |
434 |
play_video(url, name, description) |