/[projects]/misc/xbmc/plugin.video.todic/default.py
ViewVC logotype

Annotation of /misc/xbmc/plugin.video.todic/default.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3208 - (hide annotations) (download) (as text)
Mon Sep 18 06:43:20 2017 UTC (6 years, 8 months ago) by torben
File MIME type: text/x-python
File size: 14716 byte(s)
add support for configurable entrypoints
1 torben 1678
2 torben 1677 # This Python file uses the following encoding: utf-8
3    
4 torben 1629 '''
5     Todic plugin for XBMC
6 torben 3165 Version 0.1.6
7 torben 1629 '''
8    
9     import sys
10 torben 1648 import os
11 torben 1629
12 torben 1648
13 torben 1629 import xbmc
14     import xbmcaddon
15     import xbmcgui
16     import xbmcplugin
17     import urllib
18 torben 1678 import urllib2
19 torben 1629
20 torben 2601 # import pprint
21 torben 2595
22 torben 1678 from xml.dom.minidom import parseString
23 torben 3142 from time import time
24 torben 1678
25 torben 1629 __addon__ = xbmcaddon.Addon(id='plugin.video.todic')
26     __key__ = __addon__.getSetting('xbmckey').lower()
27 torben 3208 __entrypoint__ = __addon__.getSetting('entrypoint').lower()
28 torben 3037 __backend__ = "https://todic.dk/xbmc.php?xbmckey=" + __key__
29 torben 3208
30     if __entrypoint__ == "alternative":
31     __backend__ = "https://alt.todic.dk/xbmc.php?xbmckey=" + key
32    
33     if __entrypoint__ == "testing":
34     __backend__ = "https://todic.dk/xbmc-beta.php?xbmckey=" + __key__
35    
36    
37    
38 torben 2594 fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'movie_bg_blur.jpg')
39 torben 2601 datapath = xbmc.translatePath(
40     'special://profile/addon_data/plugin.video.todic/')
41 torben 1629
42 torben 2592 ADDON_PATH = __addon__.getAddonInfo('path')
43 torben 2601 SkinMasterPath = os.path.join(ADDON_PATH, 'skins') + '/'
44 torben 2592 MySkinPath = (os.path.join(SkinMasterPath, '720p')) + '/'
45     MySkin = 'main.xml'
46 torben 1676
47 torben 2592
48     class TodicMovieDialog(xbmcgui.WindowXMLDialog):
49    
50 torben 2601 def __new__(cls):
51     return super(TodicMovieDialog, cls).__new__(cls, "main.xml", ADDON_PATH)
52 torben 2592
53 torben 2601 def __init__(self):
54     super(TodicMovieDialog, self).__init__()
55 torben 3147 self.position = 0
56 torben 2592
57 torben 2601 def onClick(self, controlId):
58 torben 3147 print "[Todic] MovieDialog OnClick: " + str(controlId)
59 torben 2592
60 torben 2601 if (controlId == 50):
61     self.close()
62 torben 3147 play_real_video(self.url, self.name, 0)
63 torben 2592
64 torben 3147 if (controlId == 51):
65     self.close()
66     play_real_video(self.url, self.name, self.position)
67    
68 torben 2601 if (controlId == 98):
69     self.close()
70 torben 2592
71 torben 2601 def onInit(self):
72 torben 2592
73 torben 3152 print "[Todic] MovieDialog onInit"
74 torben 2601 self.getControl(1).setLabel(self.name)
75     self.getControl(2).setLabel(self.moviegroups)
76     self.getControl(3).setLabel(self.description)
77     self.getControl(10).setLabel(self.playlength)
78     self.getControl(11).setLabel(self.codecdetails)
79 torben 2596
80 torben 3147 if (self.position > 0):
81     self.getControl(51).setVisible(True)
82     self.getControl(50).setPosition(100, 570)
83     self.getControl(51).setPosition(450, 570)
84     self.getControl(50).controlLeft( self.getControl(51) )
85     self.getControl(50).controlRight( self.getControl(51) )
86     else:
87     self.getControl(51).setVisible(False)
88 torben 2596
89 torben 3147 #orig_img_width = self.getControl(40).getWidth()
90     #self.starwidth = (float(self.imdbrating) / 10.0) * orig_img_width
91     #self.getControl(40).setWidth(int(self.starwidth))
92    
93 torben 3152 def setDetailsDoc(self, detailsDoc):
94     print "[Todic] MovieDialog setDetailsDoc:"
95     self.imdbrating = getText(detailsDoc.getElementsByTagName("imdbrating"))
96     self.moviegroups = getText(detailsDoc.getElementsByTagName("moviegroups"))
97     self.playlength = getText(detailsDoc.getElementsByTagName("playlength"))
98     self.codecdetails = getText(detailsDoc.getElementsByTagName("codecdetails"))
99     self.position = int( getText(detailsDoc.getElementsByTagName("position")) )
100 torben 2592
101 torben 3153 def setUrl(self, url):
102     self.url = url
103 torben 3147
104 torben 2601 def setName(self, name):
105     self.name = name
106 torben 2595
107 torben 2601 def setDescription(self, description):
108     self.description = description
109 torben 2595
110 torben 2596
111 torben 2601 class TodicPlayer(xbmc.Player):
112 torben 2596
113 torben 2601 def __init__(self, *args, **kwargs):
114     # xbmc.Player.__init__(selv,*args,**kwargs)
115 torben 3163 xbmc.Player.__init__(self)
116 torben 2601 self.stopped = False
117     self.started = False
118 torben 3142 self.playingPosition = 0.0
119     self.lastReport = 0
120 torben 2601 print "[TodicPlayer] init"
121 torben 2592
122 torben 2601 def onPlayBackStarted(self):
123     self.started = True
124     print "[TodicPlayer] : started"
125 torben 2592
126 torben 3142 #When user presses stop, we report back the the position registered in the last call to self.tick()
127 torben 2601 def onPlayBackStopped(self):
128     self.stopped = True
129     print "[TodicPlayer] : stopped"
130 torben 3149 self.reportPlaytime("stopped")
131 torben 2592
132 torben 3157 def onPlayBackPaused(self):
133     print "[TodicPlayer] : paused"
134     self.reportPlaytime("paused")
135 torben 3142
136 torben 3157 def onPlayBackResumed(self):
137     print "[TodicPlayer] : resumed"
138     self.reportPlaytime("resumed")
139 torben 3152
140 torben 3157
141 torben 2601 def onPlayBackEnded(self):
142     self.stopped = True
143     print "[TodicPlayer] : ended"
144 torben 3149 self.reportPlaytime("ended")
145 torben 1676
146 torben 3142 def tick(self):
147 torben 3157 #print "[Todic] Tick: " + str( self.isPlaying() )
148 torben 3144 if ( self.isPlaying() ):
149 torben 3142 self.playingPosition = self.getTime()
150     now = time()
151     #print "[Todic] tick " + str(now) + " " + str(self.lastReport) + " : " +str(now - self.lastReport)
152     if ( (now - self.lastReport) > 60.0):
153 torben 3147 self.lastReport = now
154 torben 3149 self.reportPlaytime("playing")
155 torben 3147
156 torben 3149 def reportPlaytime(self, subaction):
157 torben 3151 if (self.playingPosition > 60):
158     url = __backend__ + "&action=playbacktime&subaction=" + subaction + "&time=" + str( self.playingPosition )
159     print "[Todic] reportPlaytime:" + url
160     open_url_safe(url)
161    
162 torben 3142
163    
164 torben 2601 def getText2(nodelist):
165     rc = []
166     for node in nodelist:
167     if node.nodeType == node.TEXT_NODE:
168     rc.append(node.data)
169     else:
170     rc.append(getText(node.childNodes))
171     return ''.join(rc)
172 torben 1676
173    
174 torben 1678 def getText(nodelist):
175 torben 2601 if nodelist.length == 0:
176     return ''
177     else:
178     if nodelist[0].childNodes.length == 0:
179     return ''
180     else:
181     return nodelist[0].childNodes[0].nodeValue
182 torben 1676
183 torben 2601
184 torben 3143
185 torben 1914 def SaveFile(path, data):
186 torben 2601 file = open(path, 'w')
187     file.write(data)
188     file.close()
189 torben 1678
190 torben 1914
191 torben 3143
192 torben 1629 def open_url(url):
193 torben 2601 req = urllib2.Request(url)
194     content = urllib2.urlopen(req)
195     data = content.read()
196     content.close()
197     return data
198 torben 1629
199 torben 2601
200 torben 3143 # wraps open url in a catch-all exception handler
201     # usefull for periodic back-reporting that should not interrupt the program flow
202     def open_url_safe(url):
203     try:
204     return open_url(url)
205     except:
206 torben 3157 print "[Todic] Some error during open_url call to ", url
207 torben 3143
208    
209    
210 torben 1629 def rootMenu():
211 torben 1648
212 torben 2601 msg = open_url(__backend__ + "&action=messages")
213     msg = msg.strip()
214 torben 1799
215 torben 2601 if msg != "":
216     dialog = xbmcgui.Dialog()
217     dialog.ok('XBMC Todic', msg)
218 torben 1799
219 torben 2601 buildList(__backend__, "", False) # call default list
220 torben 1631
221 torben 2601 # Adde xtra items to root menu
222     listitem = xbmcgui.ListItem(
223 torben 3155 label="Søg Film ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
224 torben 2601 listitem.setProperty('Fanart_Image', fanartImage)
225 torben 1648
226 torben 2601 u = sys.argv[0] + "?mode=10&name="
227 torben 2602 xbmcplugin.addDirectoryItem(
228 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
229 torben 1631
230 torben 2601 # add search series
231     listitem = xbmcgui.ListItem(
232     label="Søg Serier ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
233     listitem.setProperty('Fanart_Image', fanartImage)
234 torben 2097
235 torben 2601 u = sys.argv[0] + "?mode=11&name="
236 torben 2602 xbmcplugin.addDirectoryItem(
237 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
238 torben 2097
239 torben 2601 xbmcplugin.endOfDirectory(int(sys.argv[1]))
240 torben 1629
241    
242 torben 2601 def buildList(url, title, endlist=True):
243 torben 3157 print '[Todic]:' + str(url)
244 torben 1678
245 torben 2601 link = open_url(url)
246     doc = parseString(link)
247     ty = doc.getElementsByTagName("meta")[0].getAttribute("type")
248 torben 3157 print '[Todic]' + str(ty)
249 torben 1646
250 torben 2601 if ty == 'clipList':
251     mode = '50'
252     folder = False
253     else:
254     mode = '1'
255     folder = True
256 torben 1646
257 torben 2601 entries = doc.getElementsByTagName("entry")
258     l = len(entries)
259 torben 3159
260 torben 2601 for entry in entries:
261     name = getText(entry.getElementsByTagName("title"))
262     url = getText(entry.getElementsByTagName("url"))
263     thumb = getText(entry.getElementsByTagName("cover"))
264     playcount = getText(entry.getElementsByTagName("playcount"))
265 torben 1829
266 torben 3148
267 torben 2601 if playcount == '':
268     playcount = '0'
269     playcount = int(playcount)
270 torben 1647
271 torben 2601 # print "name:" + name
272 torben 1678 # print "url:" + url
273     # print "thumb:" + thumb
274 torben 2601 listitem = xbmcgui.ListItem(
275     label=name, label2='test', iconImage='DefaultFolder.png', thumbnailImage=thumb)
276     listitem.setProperty('Fanart_Image', fanartImage)
277     if mode == '50':
278     infoLabels = {}
279     infoLabels['title'] = name
280     infoLabels['playcount'] = playcount
281     listitem.setInfo('video', infoLabels)
282 torben 1678
283 torben 2601 name = name.encode('UTF-8')
284 torben 1678
285 torben 2601 u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
286 torben 3159 name) + "&url=" + urllib.quote(url)
287 torben 2602 xbmcplugin.addDirectoryItem(
288 torben 2601 handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
289 torben 1648
290 torben 2601 if (endlist == True):
291     xbmcplugin.endOfDirectory(int(sys.argv[1]))
292 torben 1923
293    
294 torben 3159 def play_video(url, name):
295     description = ""
296 torben 3158 playPosition = 0
297     savedPosition = 0
298     try:
299     param1 = parse_parameter_string(url)
300     clipkey = param1["clipkey"]
301 torben 3152
302 torben 3158 print "[Todic] ClipKey:" + clipkey
303     detailurl = __backend__ + "&action=clipdetails&clipkey=" + clipkey
304     print "[Todic] detailURL = " + detailurl
305 torben 3152
306 torben 3158 xml = open_url(detailurl)
307 torben 3152
308 torben 3158 clipDetailsDoc = parseString(xml)
309     savedPosition = int( getText(clipDetailsDoc.getElementsByTagName("position")) )
310 torben 3159 description = getText(clipDetailsDoc.getElementsByTagName("description"))
311 torben 3158 except:
312     print "[Todic] Unexpected error:", sys.exc_info()[0]
313 torben 3152
314 torben 2601 if (description == None or description == ""):
315 torben 3152 if (savedPosition > 0):
316     dialog = xbmcgui.Dialog()
317     #yes / true -afspil fra position
318 torben 3155 answer = dialog.yesno(heading='Todic', line1='Afspil fra sidste position', nolabel='Fra start', yeslabel='Fortsæt')
319 torben 3152 if (answer == True):
320     playPosition = savedPosition
321    
322     play_real_video(url, name, playPosition)
323    
324 torben 2601 else:
325     d = TodicMovieDialog()
326 torben 3152 d.setDetailsDoc(clipDetailsDoc)
327 torben 2601 d.setName(name)
328 torben 3153 d.setUrl(url)
329 torben 2601 d.setDescription(description)
330 torben 1629
331 torben 2601 d.doModal()
332 torben 1629
333    
334 torben 3147 def play_real_video(url, name, position):
335 torben 2601 xml = open_url(url)
336 torben 3147 print '[Todic] url: ' + str(url)
337     print '[Todic] xml: ' + xml
338     print '[Todic] pos: ' + str(position)
339 torben 1678
340 torben 2601 doc = parseString(xml)
341     url = getText(doc.getElementsByTagName("url"))
342 torben 1678
343 torben 2601 subtitleurl = getText(doc.getElementsByTagName("subtitles"))
344     subtitlesfile = os.path.join(datapath, 'temp.srt')
345 torben 1914
346 torben 2601 # if old srt file exists delete it first
347     if os.path.isfile(subtitlesfile):
348     os.unlink(subtitlesfile)
349 torben 1917
350 torben 3157 print '[Todic] subs: ' + str(subtitleurl)
351 torben 2601 if len(subtitleurl) > 0:
352     subtitles = open_url(subtitleurl)
353     SaveFile(subtitlesfile, subtitles)
354 torben 3157 print '[Todic] downloaded subtitles'
355 torben 1914
356 torben 2601 image = xbmc.getInfoImage('ListItem.Thumb')
357     listitem = xbmcgui.ListItem(
358     label=name, iconImage='DefaultVideo.png', thumbnailImage=image)
359     listitem.setInfo(type="Video", infoLabels={"Title": name})
360 torben 3147 listitem.setProperty('ResumeTime', '300')
361     listitem.setProperty('TotalTime', '3000')
362 torben 1914
363 torben 3165 player = TodicPlayer()
364 torben 2601 player.play(str(url), listitem)
365 torben 1914
366 torben 2601 # kan ikke loade subtitles hvis foerend playeren koerer
367     count = 0
368     while not xbmc.Player().isPlaying():
369 torben 3146 xbmc.sleep(250)
370 torben 2601 count += 1
371     if count > 10:
372     break
373 torben 1629
374 torben 3147
375    
376 torben 2601 if xbmc.Player().isPlaying():
377     if os.path.isfile(subtitlesfile):
378     player.setSubtitles(subtitlesfile)
379 torben 3157 print '[Todic] started subtitles'
380 torben 2601 else:
381 torben 3162 player.showSubtitles(False)
382 torben 1914
383 torben 3142
384 torben 3147 if (position > 0):
385     while (player.getTotalTime() == 0.0): #Vent indtil vi har beregnet hvor langt klippet er
386     xbmc.sleep(250)
387 torben 3142
388 torben 3147 print "[Todic] totalTime " + str( player.getTotalTime() )
389     player.seekTime(position)
390 torben 3142
391 torben 1676
392 torben 3147 #Holder python kørernde indtil at det bliver bedt om at stoppe
393     while (not xbmc.abortRequested):
394     player.tick()
395     xbmc.sleep(500)
396 torben 1676
397 torben 3147
398    
399 torben 1631 def search():
400 torben 2601 search = getUserInput("Todic Søgning")
401 torben 1629
402 torben 2601 if (search != None and search != ""):
403     url = __backend__ + "&action=search&search=" + \
404     urllib.quote_plus(search)
405 torben 1631
406 torben 3157 # print "[Todic] Search start: " + search
407     # print "[Todic] Search url: " + url
408 torben 1631
409 torben 2601 buildList(url, "søgning")
410 torben 1631
411 torben 2601
412 torben 2097 def searchSeries():
413 torben 2601 search = getUserInput("Todic Serie Søgning")
414 torben 2097
415 torben 2601 if (search != None and search != ""):
416     url = __backend__ + "&action=searchseries&search=" + \
417     urllib.quote_plus(search)
418 torben 2097
419 torben 3157 # print "[Todic] Search start: " + search
420     # print "[Todic] Search url: " + url
421 torben 2097
422 torben 2601 buildList(url, "serie søgning")
423 torben 2097
424 torben 1631
425 torben 2601 #=================================== Tool Box =======================================
426 torben 1631 # shows a more userfriendly notification
427     def showMessage(heading, message):
428 torben 2601 duration = 15 * 1000
429     xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
430     (heading, message, duration))
431 torben 1631
432    
433     # raise a keyboard for user input
434 torben 2601 def getUserInput(title="Input", default="", hidden=False):
435     result = None
436 torben 1631
437 torben 2601 # Fix for when this functions is called with default=None
438     if not default:
439     default = ""
440 torben 1631
441 torben 2601 keyboard = xbmc.Keyboard(default, title)
442     keyboard.setHiddenInput(hidden)
443     keyboard.doModal()
444 torben 1631
445 torben 2601 if keyboard.isConfirmed():
446     result = keyboard.getText()
447    
448     return result
449    
450    
451 torben 1629 def get_params():
452 torben 2601 return parse_parameter_string(sys.argv[2])
453 torben 2595
454 torben 1629
455 torben 2601 def parse_parameter_string(paramstring):
456     param = []
457     if len(paramstring) >= 2:
458     params = paramstring
459     cleanedparams = params.replace('?', '')
460     if (params[len(params) - 1] == '/'):
461     params = params[0:len(params) - 2]
462     pairsofparams = cleanedparams.split('&')
463     param = {}
464     for i in range(len(pairsofparams)):
465     splitparams = {}
466     splitparams = pairsofparams[i].split('=')
467     if (len(splitparams)) == 2:
468     param[splitparams[0]] = splitparams[1]
469     return param
470 torben 1629
471 torben 2601
472 torben 1629 params = get_params()
473     url = None
474     name = None
475     mode = None
476    
477 torben 3149
478 torben 3148 #print params
479    
480 torben 1629 try:
481 torben 2601 url = urllib.unquote_plus(params["url"])
482 torben 1629 except:
483 torben 2601 pass
484 torben 1629 try:
485 torben 2601 name = urllib.unquote_plus(params["name"])
486 torben 1629 except:
487 torben 2601 pass
488 torben 1629 try:
489 torben 2601 mode = int(params["mode"])
490 torben 1629 except:
491 torben 2601 pass
492 torben 1629
493 torben 3148
494    
495 torben 3147
496     try:
497 torben 2601 open_url("http://todic.dk")
498 torben 2440 except:
499 torben 2601 showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
500     exit()
501 torben 2440
502 torben 2601
503 torben 1814 if url == 'refresh':
504 torben 2601 # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
505     # ikke med XBMC12
506     xbmc.executebuiltin("Container.Refresh")
507 torben 1629
508 torben 2601
509 torben 1814 elif mode == None:
510 torben 2601 # build main menu
511     rootMenu()
512    
513 torben 1629 elif mode == 1:
514 torben 2601 # build list of movie starting letters
515     buildList(url, name)
516 torben 1629
517 torben 1631 elif mode == 10:
518 torben 2601 search()
519 torben 2097
520     elif mode == 11:
521 torben 2601 searchSeries()
522 torben 1631
523 torben 2601
524 torben 1629 elif mode == 50:
525 torben 3159 play_video(url, name)

  ViewVC Help
Powered by ViewVC 1.1.20