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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1676 by torben, Mon Jan 2 20:51:02 2012 UTC revision 3163 by torben, Mon Dec 5 20:22:35 2016 UTC
# Line 1  Line 1 
1    
2    # This Python file uses the following encoding: utf-8
3    
4  '''  '''
5      Todic plugin for XBMC      Todic plugin for XBMC
6      Version 0.0.2      Version 0.1.5
7  '''  '''
8    
9  import sys  import sys
 import cgi as urlparse  
10  import os  import os
11    
12    
# Line 13  import xbmcaddon Line 15  import xbmcaddon
15  import xbmcgui  import xbmcgui
16  import xbmcplugin  import xbmcplugin
17  import urllib  import urllib
18  import urllib2, re  import urllib2
19    
20    # import pprint
21    
22    from xml.dom.minidom import parseString
23    from time import time
24    
25  __addon__ = xbmcaddon.Addon(id='plugin.video.todic')  __addon__ = xbmcaddon.Addon(id='plugin.video.todic')
26  __key__ = __addon__.getSetting('xbmckey').lower()  __key__ = __addon__.getSetting('xbmckey').lower()
27  __backend__ = "http://todic.dk/xbmc.php?xbmckey=" + __key__  __backend__ = "https://todic.dk/xbmc.php?xbmckey=" + __key__
28  fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'fanart.jpg')  fanartImage = os.path.join(__addon__.getAddonInfo('path'), 'movie_bg_blur.jpg')
29    datapath = xbmc.translatePath(
30        'special://profile/addon_data/plugin.video.todic/')
31    
32    ADDON_PATH = __addon__.getAddonInfo('path')
33    SkinMasterPath = os.path.join(ADDON_PATH, 'skins') + '/'
34    MySkinPath = (os.path.join(SkinMasterPath, '720p')) + '/'
35    MySkin = 'main.xml'
36    
37    
38    class TodicMovieDialog(xbmcgui.WindowXMLDialog):
39    
40        def __new__(cls):
41            return super(TodicMovieDialog, cls).__new__(cls, "main.xml", ADDON_PATH)
42    
43        def __init__(self):
44            super(TodicMovieDialog, self).__init__()
45            self.position = 0
46    
47        def onClick(self, controlId):
48            print "[Todic] MovieDialog OnClick: " + str(controlId)
49    
50            if (controlId == 50):
51                self.close()
52                play_real_video(self.url, self.name, 0)
53    
54            if (controlId == 51):
55                self.close()
56                play_real_video(self.url, self.name, self.position)
57    
58            if (controlId == 98):
59                self.close()
60    
61        def onInit(self):
62    
63            print "[Todic] MovieDialog onInit"
64            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    
70            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    
79            #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        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    
91        def setUrl(self, url):
92            self.url = url
93    
94        def setName(self, name):
95            self.name = name
96    
97        def setDescription(self, description):
98            self.description = description
99    
100    
101  class TodicPlayer(xbmc.Player):  class TodicPlayer(xbmc.Player):
         def __init__(self, *args, **kwargs):  
                 #xbmc.Player.__init__(selv,*args,**kwargs)  
                 xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER )  
                 self.stopped = False  
                 self.started = False  
                 print "[TodicPlayer] init"  
   
 #       @catchall        
         def onPlayBackStarted(self):  
                 self.started = True  
                 print "[TodicPlayer] : started"  
 #               super.onPlayBackStarted()  
   
         def onPlayBackStopped(self):  
                 self.stopped = True  
                 print "[TodicPlayer] : stopped"  
   
         def onPlayBackEnded(self):  
                 self.stopped = True  
                 print "[TodicPlayer] : ended"  
   
         def callbackLoop(self):  
                 print "[Todic] startLoop"  
                 while (self.stopped == False):  
                         if (self.started == True ):  
                                 print "[todic] " + str(self.getTime())  
                         xbmc.sleep(5000)  
                           
102    
103        def __init__(self, *args, **kwargs):
104            # xbmc.Player.__init__(selv,*args,**kwargs)
105            xbmc.Player.__init__(self)
106            self.stopped = False
107            self.started = False
108            self.playingPosition = 0.0
109            self.lastReport = 0
110            print "[TodicPlayer] init"
111    
112        def onPlayBackStarted(self):
113            self.started = True
114            print "[TodicPlayer] : started"
115    
116        #When user presses stop, we report back the the position registered in the last call to self.tick()
117        def onPlayBackStopped(self):
118            self.stopped = True
119            print "[TodicPlayer] : stopped"
120            self.reportPlaytime("stopped")
121    
122        def onPlayBackPaused(self):
123            print "[TodicPlayer] : paused"
124            self.reportPlaytime("paused")
125    
126        def onPlayBackResumed(self):
127            print "[TodicPlayer] : resumed"
128            self.reportPlaytime("resumed")
129    
130    
131        def onPlayBackEnded(self):
132            self.stopped = True
133            print "[TodicPlayer] : ended"
134            self.reportPlaytime("ended")
135    
136        def tick(self):
137            #print "[Todic] Tick: " + str( self.isPlaying() )
138            if ( self.isPlaying() ):
139                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                    self.lastReport = now
144                    self.reportPlaytime("playing")
145    
146        def reportPlaytime(self, subaction):
147            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    
 def open_url(url):  
         req = urllib2.Request(url)  
         content = urllib2.urlopen(req)  
         data = content.read()  
         content.close()  
         return data  
153    
154  def rootMenu():  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    
         buildList(__backend__, "", False) # call default list  
163    
164          # Adde xtra items to root menu  def getText(nodelist):
165          listitem = xbmcgui.ListItem(label = "Søg film ...", iconImage = 'DefaultFolder.png', thumbnailImage = 'DefaultFolder.png')      if nodelist.length == 0:
166          listitem.setProperty('Fanart_Image', fanartImage)          return ''
167        else:
168            if nodelist[0].childNodes.length == 0:
169                return ''
170            else:
171                return nodelist[0].childNodes[0].nodeValue
172    
         u = sys.argv[0] + "?mode=10&name="  
         ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = True)  
173    
         xbmcplugin.endOfDirectory(int(sys.argv[1]))  
174    
175    def SaveFile(path, data):
176        file = open(path, 'w')
177        file.write(data)
178        file.close()
179    
 def buildList(url,title, endlist=True):  
         print '[TODIC]:'+str(url)          
         link = open_url(url)  
         ty=re.compile('<meta type=\'(.+?)\'').findall(link)  
         print '[TODIC]'+str(ty[0])  
180    
         if ty[0] == 'clipList':  
                 mode = '50'  
                 folder = False  
         else:  
                 mode = '1'  
                 folder = True  
181    
182          m=re.compile('<title>(.+?)</title><url>(.+?)</url><cover>(.+?)</cover><description>(.*)</description>').findall(link)  def open_url(url):
183          l=len(m)      req = urllib2.Request(url)
184          for name,url,thumb,description in m:                              content = urllib2.urlopen(req)
185        data = content.read()
186                  listitem = xbmcgui.ListItem(label = name, label2='test', iconImage = 'DefaultFolder.png', thumbnailImage = thumb)      content.close()
187                  listitem.setProperty('Fanart_Image', fanartImage)      return data
188                  if mode == '50':  
                         infoLabels = {}  
                         infoLabels['title'] = name  
                         infoLabels['plot'] = description          
                         listitem.setInfo('video', infoLabels)  
189    
190                  u = sys.argv[0] + "?mode=" + urllib.quote_plus(mode) + "&name=" + urllib.quote_plus(name) + "&url=" + urllib.quote_plus(url)  # wraps open url in a catch-all exception handler
191                  ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = folder, totalItems = l)  # 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            print "[Todic] Some error during open_url call to ", url
197    
         if (endlist == True):    
                 xbmcplugin.endOfDirectory(int(sys.argv[1]))  
198    
199    
200    def rootMenu():
201    
202        msg = open_url(__backend__ + "&action=messages")
203        msg = msg.strip()
204    
205        if msg != "":
206            dialog = xbmcgui.Dialog()
207            dialog.ok('XBMC Todic', msg)
208    
209        buildList(__backend__, "", False)  # call default list
210    
211        # Adde xtra items to root menu
212        listitem = xbmcgui.ListItem(
213            label="Søg Film ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
214        listitem.setProperty('Fanart_Image', fanartImage)
215    
216        u = sys.argv[0] + "?mode=10&name="
217        xbmcplugin.addDirectoryItem(
218            handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
219    
220        # 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    
225        u = sys.argv[0] + "?mode=11&name="
226        xbmcplugin.addDirectoryItem(
227            handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
228    
229        xbmcplugin.endOfDirectory(int(sys.argv[1]))
230    
231    
232    def buildList(url, title, endlist=True):
233        print '[Todic]:' + str(url)
234    
235        link = open_url(url)
236        doc = parseString(link)
237        ty = doc.getElementsByTagName("meta")[0].getAttribute("type")
238        print '[Todic]' + str(ty)
239    
240        if ty == 'clipList':
241            mode = '50'
242            folder = False
243        else:
244            mode = '1'
245            folder = True
246    
247        entries = doc.getElementsByTagName("entry")
248        l = len(entries)
249    
250        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    
256    
257            if playcount == '':
258                playcount = '0'
259            playcount = int(playcount)
260    
261    # print "name:" + name
262    #               print "url:" + url
263    #               print "thumb:" + thumb
264            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    
273            name = name.encode('UTF-8')
274    
275            u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
276                name) + "&url=" + urllib.quote(url)
277            xbmcplugin.addDirectoryItem(
278                handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
279    
280        if (endlist == True):
281            xbmcplugin.endOfDirectory(int(sys.argv[1]))
282    
283    
284  def play_video(url, name):  def play_video(url, name):
285          link = open_url(url)      description = ""
286          match=re.compile('<url>(.+?)</url>').findall(link)      playPosition = 0
287          url = match[0]      savedPosition = 0
288          print '[TODIC]:'+str(url)      try:
289          image = xbmc.getInfoImage( 'ListItem.Thumb' )          param1 = parse_parameter_string(url)
290          listitem = xbmcgui.ListItem(label = name , iconImage = 'DefaultVideo.png', thumbnailImage = image)          clipkey = param1["clipkey"]
291  #       listitem = xbmcgui.ListItem(label = name , iconImage = 'DefaultVideo.png', thumbnailImage = 'DefaultVideo.png')  
292          listitem.setInfo( type = "Video", infoLabels={ "Title": name } )          print "[Todic] ClipKey:" + clipkey
293  #       xbmc.Player(xbmc.PLAYER_CORE_AUTO).play(str(url), listitem)          detailurl = __backend__ + "&action=clipdetails&clipkey=" + clipkey
294            print "[Todic] detailURL = " + detailurl
295          player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)  
296          player.play(str(url), listitem)          xml = open_url(detailurl)
297          player.callbackLoop()  
298            clipDetailsDoc = parseString(xml)
299            savedPosition = int( getText(clipDetailsDoc.getElementsByTagName("position")) )
300            description = getText(clipDetailsDoc.getElementsByTagName("description"))
301        except:
302            print "[Todic] Unexpected error:", sys.exc_info()[0]
303    
304        if (description == None or description == ""):
305            if (savedPosition > 0):
306                dialog = xbmcgui.Dialog()
307                #yes / true -afspil fra position
308                answer = dialog.yesno(heading='Todic', line1='Afspil fra sidste position', nolabel='Fra start', yeslabel='Fortsæt')
309                if (answer == True):
310                    playPosition = savedPosition
311            
312            play_real_video(url, name, playPosition)
313    
314        else:
315            d = TodicMovieDialog()
316            d.setDetailsDoc(clipDetailsDoc)
317            d.setName(name)
318            d.setUrl(url)
319            d.setDescription(description)
320    
321            d.doModal()
322    
323    
324    def play_real_video(url, name, position):
325        xml = open_url(url)
326        print '[Todic] url: ' + str(url)
327        print '[Todic] xml: ' + xml
328        print '[Todic] pos: ' + str(position)
329    
330        doc = parseString(xml)
331        url = getText(doc.getElementsByTagName("url"))
332    
333        subtitleurl = getText(doc.getElementsByTagName("subtitles"))
334        subtitlesfile = os.path.join(datapath, 'temp.srt')
335    
336        # if old srt file exists delete it first
337        if os.path.isfile(subtitlesfile):
338            os.unlink(subtitlesfile)
339    
340        print '[Todic] subs: ' + str(subtitleurl)
341        if len(subtitleurl) > 0:
342            subtitles = open_url(subtitleurl)
343            SaveFile(subtitlesfile, subtitles)
344            print '[Todic] downloaded subtitles'
345    
346        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        listitem.setProperty('ResumeTime', '300')
351        listitem.setProperty('TotalTime', '3000')
352    
353        player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)
354        player.play(str(url), listitem)
355    
356        # kan ikke loade subtitles hvis foerend playeren koerer
357        count = 0
358        while not xbmc.Player().isPlaying():
359            xbmc.sleep(250)
360            count += 1
361            if count > 10:
362                break
363    
364    
365    
366        if xbmc.Player().isPlaying():
367            if os.path.isfile(subtitlesfile):
368                player.setSubtitles(subtitlesfile)
369                print '[Todic] started subtitles'
370            else:
371                player.showSubtitles(False)
372    
373    
374            if (position > 0):
375                while (player.getTotalTime() == 0.0): #Vent indtil vi har beregnet hvor langt klippet er
376                    xbmc.sleep(250)
377    
378                print "[Todic] totalTime " +  str( player.getTotalTime() )
379                player.seekTime(position)
380    
381    
382        #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    
387    
388    
389  def search():  def search():
390          search = getUserInput("Todic Søgning")      search = getUserInput("Todic Søgning")
391    
392        if (search != None and search != ""):
393            url = __backend__ + "&action=search&search=" + \
394                urllib.quote_plus(search)
395    
396          if (search != None and search != ""):          # print "[Todic] Search start: " + search
397                  url = __backend__ + "&action=search&search=" + urllib.quote_plus(search)          # print "[Todic] Search url: " + url
398    
399                  #print "[TODIC] Search start: " + search          buildList(url, "søgning")
                 #print "[TODIC] Search url: " + url  
400    
                 buildList(url, "søgning")  
401    
402            def searchSeries():
403        search = getUserInput("Todic Serie Søgning")
404    
405        if (search != None and search != ""):
406            url = __backend__ + "&action=searchseries&search=" + \
407                urllib.quote_plus(search)
408    
409                            # print "[Todic] Search start: " + search
410  #=================================== Tool Box =======================================          # print "[Todic] Search url: " + url
411    
412            buildList(url, "serie søgning")
413    
414    
415    #=================================== Tool Box =======================================
416  # shows a more userfriendly notification  # shows a more userfriendly notification
417  def showMessage(heading, message):  def showMessage(heading, message):
418          duration = 15*1000      duration = 15 * 1000
419          xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) )      xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
420                            (heading, message, duration))
421    
422    
423  # raise a keyboard for user input  # raise a keyboard for user input
424  def getUserInput(title = "Input", default="", hidden=False):  def getUserInput(title="Input", default="", hidden=False):
425          result = None      result = None
426    
427        # Fix for when this functions is called with default=None
428        if not default:
429            default = ""
430    
431          # Fix for when this functions is called with default=None      keyboard = xbmc.Keyboard(default, title)
432          if not default:      keyboard.setHiddenInput(hidden)
433                  default = ""      keyboard.doModal()
434                            
435          keyboard = xbmc.Keyboard(default, title)      if keyboard.isConfirmed():
436          keyboard.setHiddenInput(hidden)          result = keyboard.getText()
437          keyboard.doModal()  
438                        return result
         if keyboard.isConfirmed():  
                 result = keyboard.getText()  
                   
         return result  
439    
440    
441  def get_params():  def get_params():
442          param=[]      return parse_parameter_string(sys.argv[2])
443          paramstring=sys.argv[2]  
444          if len(paramstring)>=2:  
445                  params=sys.argv[2]  def parse_parameter_string(paramstring):
446                  cleanedparams=params.replace('?','')      param = []
447                  if (params[len(params)-1]=='/'):      if len(paramstring) >= 2:
448                          params=params[0:len(params)-2]          params = paramstring
449                  pairsofparams=cleanedparams.split('&')          cleanedparams = params.replace('?', '')
450                  param={}          if (params[len(params) - 1] == '/'):
451                  for i in range(len(pairsofparams)):              params = params[0:len(params) - 2]
452                          splitparams={}          pairsofparams = cleanedparams.split('&')
453                          splitparams=pairsofparams[i].split('=')          param = {}
454                          if (len(splitparams))==2:          for i in range(len(pairsofparams)):
455                                  param[splitparams[0]]=splitparams[1]                                                  splitparams = {}
456          return param              splitparams = pairsofparams[i].split('=')
457                if (len(splitparams)) == 2:
458                    param[splitparams[0]] = splitparams[1]
459        return param
460    
 params = get_params()  
 url = None  
 name = None  
 mode = None  
461    
462  params = get_params()  params = get_params()
463  url = None  url = None
464  name = None  name = None
465  mode = None  mode = None
466    
467    
468    #print params
469    
470  try:  try:
471          url = urllib.unquote_plus(params["url"])      url = urllib.unquote_plus(params["url"])
472  except:  except:
473          pass      pass
474  try:  try:
475          name = urllib.unquote_plus(params["name"])      name = urllib.unquote_plus(params["name"])
476  except:  except:
477          pass      pass
478  try:  try:
479          mode = int(params["mode"])      mode = int(params["mode"])
480  except:  except:
481          pass      pass
482    
483    
 if mode == None:  
         #build main menu  
         rootMenu()  
         
 elif mode == 1:  
         #build list of movie starting letters  
         buildList(url, name)  
484    
 elif mode == 10:  
         search()  
           
485    
486  elif mode == 50:  try:
487          play_video(url, name)      open_url("http://todic.dk")
488    except:
489        showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
490        exit()
491    
492    
493    if url == 'refresh':
494        # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
495        # ikke med XBMC12
496        xbmc.executebuiltin("Container.Refresh")
497    
498    
499  # xbmcplugin.endOfDirectory(int(sys.argv[1]))  elif mode == None:
500        # build main menu
501        rootMenu()
502    
503    elif mode == 1:
504        # build list of movie starting letters
505        buildList(url, name)
506    
507    elif mode == 10:
508        search()
509    
510    elif mode == 11:
511        searchSeries()
512    
513    
514    elif mode == 50:
515        play_video(url, name)

Legend:
Removed from v.1676  
changed lines
  Added in v.3163

  ViewVC Help
Powered by ViewVC 1.1.20