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

  ViewVC Help
Powered by ViewVC 1.1.20