/[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 1646 by torben, Sat Dec 3 12:01:56 2011 UTC revision 3152 by torben, Fri Nov 25 16:17:45 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.0
7  '''  '''
8    
9  import sys  import sys
10  import cgi as urlparse  import os
11    
12    
13  import xbmc  import xbmc
14  import xbmcaddon  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'), '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    
92        def setName(self, name):
93            self.name = name
94    
95        def setDescription(self, description):
96            self.description = description
97    
98    
99    class TodicPlayer(xbmc.Player):
100    
101        def __init__(self, *args, **kwargs):
102            # xbmc.Player.__init__(selv,*args,**kwargs)
103            xbmc.Player.__init__(self, xbmc.PLAYER_CORE_MPLAYER)
104            self.stopped = False
105            self.started = False
106            self.playingPosition = 0.0
107            self.lastReport = 0
108            print "[TodicPlayer] init"
109    
110        def onPlayBackStarted(self):
111            self.started = True
112            print "[TodicPlayer] : started"
113    
114        #When user presses stop, we report back the the position registered in the last call to self.tick()
115        def onPlayBackStopped(self):
116            self.stopped = True
117            print "[TodicPlayer] : stopped"
118            self.reportPlaytime("stopped")
119    
120    
121    
122        def onPlayBackEnded(self):
123            self.stopped = True
124            print "[TodicPlayer] : ended"
125            self.reportPlaytime("ended")
126    
127        def tick(self):
128            if ( self.isPlaying() ):
129                self.playingPosition = self.getTime()
130                now = time()
131                #print "[Todic] tick " + str(now) + " " + str(self.lastReport) + " : " +str(now - self.lastReport)
132                if ( (now - self.lastReport) > 60.0):
133                    self.lastReport = now
134                    self.reportPlaytime("playing")
135    
136        def reportPlaytime(self, subaction):
137            if (self.playingPosition > 60):
138                url = __backend__ + "&action=playbacktime&subaction=" + subaction + "&time=" + str( self.playingPosition )
139                print "[Todic] reportPlaytime:" + url
140                open_url_safe(url)
141    
142                    
143    
144    
145    def getText2(nodelist):
146        rc = []
147        for node in nodelist:
148            if node.nodeType == node.TEXT_NODE:
149                rc.append(node.data)
150            else:
151                rc.append(getText(node.childNodes))
152        return ''.join(rc)
153    
154    
155    def getText(nodelist):
156        if nodelist.length == 0:
157            return ''
158        else:
159            if nodelist[0].childNodes.length == 0:
160                return ''
161            else:
162                return nodelist[0].childNodes[0].nodeValue
163    
164    
165    
166    def SaveFile(path, data):
167        file = open(path, 'w')
168        file.write(data)
169        file.close()
170    
171    
172    
173  def open_url(url):  def open_url(url):
174          req = urllib2.Request(url)      req = urllib2.Request(url)
175          content = urllib2.urlopen(req)      content = urllib2.urlopen(req)
176          data = content.read()      data = content.read()
177          content.close()      content.close()
178          return data      return data
179    
180    
181    # wraps open url in a catch-all exception handler
182    # usefull for periodic back-reporting that should not interrupt the program flow
183    def open_url_safe(url):
184        try:
185            return open_url(url)
186        except:
187            print "[Todic ]Some error during open_url call to ", url
188    
189    
190    
191  def rootMenu():  def rootMenu():
192          link = open_url(__backend__)  
193          m=re.compile('<title>(.+?)</title><url>(.+?)</url>').findall(link)      msg = open_url(__backend__ + "&action=messages")
194          l = len(m)      msg = msg.strip()
195          for name,url in m:  
196                  listitem = xbmcgui.ListItem(label = name, iconImage = 'DefaultFolder.png', thumbnailImage = 'DefaultFolder.png')      if msg != "":
197                  u = sys.argv[0] + "?mode=1&name=" + urllib.quote_plus(name) + "&url=" + urllib.quote_plus(url)          dialog = xbmcgui.Dialog()
198                  ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = True, totalItems = l)          dialog.ok('XBMC Todic', msg)
199    
200          listitem = xbmcgui.ListItem(label = "Søg film ...", iconImage = 'DefaultFolder.png', thumbnailImage = 'DefaultFolder.png')      buildList(__backend__, "", False)  # call default list
201          u = sys.argv[0] + "?mode=10&name=" + urllib.quote_plus(name)  
202          ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = True, totalItems = l)      # Adde xtra items to root menu
203        listitem = xbmcgui.ListItem(
204          xbmcplugin.endOfDirectory(int(sys.argv[1]))          label="Søg film ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
205        listitem.setProperty('Fanart_Image', fanartImage)
206    
207  def buildList(url,title):      u = sys.argv[0] + "?mode=10&name="
208          print '[TODIC]:'+str(url)              xbmcplugin.addDirectoryItem(
209          link = open_url(url)          handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
210          ty=re.compile('<meta type=\'(.+?)\'').findall(link)  
211          print '[TODIC]'+str(ty[0])      # add search series
212        listitem = xbmcgui.ListItem(
213          if ty[0] == 'clipList':          label="Søg Serier ...", iconImage='DefaultFolder.png', thumbnailImage='DefaultFolder.png')
214                  mode = '50'      listitem.setProperty('Fanart_Image', fanartImage)
215                  folder = False  
216        u = sys.argv[0] + "?mode=11&name="
217        xbmcplugin.addDirectoryItem(
218            handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=True)
219    
220        xbmcplugin.endOfDirectory(int(sys.argv[1]))
221    
222    
223    def buildList(url, title, endlist=True):
224        print '[TODIC]:' + str(url)
225    
226        link = open_url(url)
227        doc = parseString(link)
228        ty = doc.getElementsByTagName("meta")[0].getAttribute("type")
229        print '[TODIC]' + str(ty)
230    
231        if ty == 'clipList':
232            mode = '50'
233            folder = False
234        else:
235            mode = '1'
236            folder = True
237    
238        entries = doc.getElementsByTagName("entry")
239        l = len(entries)
240        description = ''
241        for entry in entries:
242            name = getText(entry.getElementsByTagName("title"))
243            url = getText(entry.getElementsByTagName("url"))
244            thumb = getText(entry.getElementsByTagName("cover"))
245            description = getText(entry.getElementsByTagName("description"))
246            playcount = getText(entry.getElementsByTagName("playcount"))
247    
248    
249            if playcount == '':
250                playcount = '0'
251            playcount = int(playcount)
252    
253    # print "name:" + name
254    #               print "url:" + url
255    #               print "thumb:" + thumb
256    #               print "description:" + description
257            listitem = xbmcgui.ListItem(
258                label=name, label2='test', iconImage='DefaultFolder.png', thumbnailImage=thumb)
259            listitem.setProperty('Fanart_Image', fanartImage)
260            if mode == '50':
261                infoLabels = {}
262                infoLabels['title'] = name
263                infoLabels['plot'] = description
264                infoLabels['playcount'] = playcount
265                listitem.setInfo('video', infoLabels)
266    
267            name = name.encode('UTF-8')
268            description = description.encode('UTF-8')
269    
270            u = sys.argv[0] + "?mode=" + urllib.quote(mode) + "&name=" + urllib.quote(
271                name) + "&url=" + urllib.quote(url) + "&description=" + urllib.quote(description)
272            xbmcplugin.addDirectoryItem(
273                handle=int(sys.argv[1]), url=u, listitem=listitem, isFolder=folder, totalItems=l)
274    
275        if (endlist == True):
276            xbmcplugin.endOfDirectory(int(sys.argv[1]))
277    
278    
279    def play_video(url, name, description):
280        param1 = parse_parameter_string(url)
281        clipkey = param1["clipkey"]
282    
283        print "[Todic] ClipKey:" + clipkey
284        detailurl = __backend__ + "&action=clipdetails&clipkey=" + clipkey
285        print "[Todic] detailURL = " + detailurl
286    
287        xml = open_url(detailurl)
288    
289        clipDetailsDoc = parseString(xml)
290        savedPosition = int( getText(clipDetailsDoc.getElementsByTagName("position")) )
291        playPosition = 0
292    
293        if (description == None or description == ""):
294            if (savedPosition > 0):
295                dialog = xbmcgui.Dialog()
296                #yes / true -afspil fra position
297                answer = dialog.yesno(heading='Todic', line1='Afspil fra gemt', nolabel='Fra start', yeslabel='Fortsæt')
298                if (answer == True):
299                    playPosition = savedPosition
300            
301            play_real_video(url, name, playPosition)
302    
303        else:
304            d = TodicMovieDialog()
305            d.setDetailsDoc(clipDetailsDoc)
306            d.setName(name)
307            d.setDescription(description)
308    
309            d.doModal()
310    
311    
312    def play_real_video(url, name, position):
313        xml = open_url(url)
314        print '[Todic] url: ' + str(url)
315        print '[Todic] xml: ' + xml
316        print '[Todic] pos: ' + str(position)
317    
318        doc = parseString(xml)
319        url = getText(doc.getElementsByTagName("url"))
320    
321        subtitleurl = getText(doc.getElementsByTagName("subtitles"))
322        subtitlesfile = os.path.join(datapath, 'temp.srt')
323    
324        # if old srt file exists delete it first
325        if os.path.isfile(subtitlesfile):
326            os.unlink(subtitlesfile)
327    
328        print '[TODIC] subs: ' + str(subtitleurl)
329        if len(subtitleurl) > 0:
330            subtitles = open_url(subtitleurl)
331            SaveFile(subtitlesfile, subtitles)
332            print 'TODIC downloaded subtitles'
333    
334        image = xbmc.getInfoImage('ListItem.Thumb')
335        listitem = xbmcgui.ListItem(
336            label=name, iconImage='DefaultVideo.png', thumbnailImage=image)
337        listitem.setInfo(type="Video", infoLabels={"Title": name})
338        listitem.setProperty('ResumeTime', '300')
339        listitem.setProperty('TotalTime', '3000')
340    
341        player = TodicPlayer(xbmc.PLAYER_CORE_AUTO)
342        player.play(str(url), listitem)
343    
344        # kan ikke loade subtitles hvis foerend playeren koerer
345        count = 0
346        while not xbmc.Player().isPlaying():
347            xbmc.sleep(250)
348            count += 1
349            if count > 10:
350                break
351    
352    
353    
354        if xbmc.Player().isPlaying():
355            if os.path.isfile(subtitlesfile):
356                player.setSubtitles(subtitlesfile)
357                print 'TODIC started subtitles'
358          else:          else:
359                  mode = '1'              player.disableSubtitles()
360                  folder = True  
361    
362            if (position > 0):
363                while (player.getTotalTime() == 0.0): #Vent indtil vi har beregnet hvor langt klippet er
364                    xbmc.sleep(250)
365    
366          m=re.compile('<title>(.+?)</title><url>(.+?)</url><cover>(.+?)</cover>').findall(link)              print "[Todic] totalTime " +  str( player.getTotalTime() )
367          l=len(m)              player.seekTime(position)
         for name,url,thumb in m:                          
                 listitem = xbmcgui.ListItem(label = name, iconImage = 'DefaultFolder.png', thumbnailImage = thumb)  
                 u = sys.argv[0] + "?mode=" + urllib.quote_plus(mode) + "&name=" + urllib.quote_plus(name) + "&url=" + urllib.quote_plus(url)  
                 ok = xbmcplugin.addDirectoryItem(handle = int(sys.argv[1]), url = u, listitem = listitem, isFolder = folder, totalItems = l)  
         xbmcplugin.endOfDirectory(int(sys.argv[1]))  
368    
369    
370        #Holder python kørernde indtil at det bliver bedt om at stoppe
371        while (not xbmc.abortRequested):
372            player.tick()
373            xbmc.sleep(500)
374    
375    
 def play_video(url, name):  
         link = open_url(url)  
         match=re.compile('<url>(.+?)</url>').findall(link)  
         url = match[0]  
         print '[TODIC]:'+str(url)  
         image = xbmc.getInfoImage( 'ListItem.Thumb' )  
         listitem = xbmcgui.ListItem(label = name , iconImage = 'DefaultVideo.png', thumbnailImage = image)  
 #       listitem = xbmcgui.ListItem(label = name , iconImage = 'DefaultVideo.png', thumbnailImage = 'DefaultVideo.png')  
         listitem.setInfo( type = "Video", infoLabels={ "Title": name } )  
         xbmc.Player(xbmc.PLAYER_CORE_AUTO).play(str(url), listitem)  
         xbmc.sleep(200)  
376    
377  def search():  def search():
378          search = getUserInput("Todic Søgning")      search = getUserInput("Todic Søgning")
379    
380          if (search != None and search != ""):      if (search != None and search != ""):
381                  url = __backend__ + "&action=search&search=" + urllib.quote_plus(search)          url = __backend__ + "&action=search&search=" + \
382                urllib.quote_plus(search)
383    
384                  #print "[TODIC] Search start: " + search          # print "[TODIC] Search start: " + search
385                  #print "[TODIC] Search url: " + url          # print "[TODIC] Search url: " + url
386    
387                  buildList(url, "søgning")          buildList(url, "søgning")
388    
           
389    
390    def searchSeries():
391        search = getUserInput("Todic Serie Søgning")
392    
393                        if (search != None and search != ""):
394  #=================================== Tool Box =======================================          url = __backend__ + "&action=searchseries&search=" + \
395                urllib.quote_plus(search)
396    
397            # print "[TODIC] Search start: " + search
398            # print "[TODIC] Search url: " + url
399    
400            buildList(url, "serie søgning")
401    
402    
403    #=================================== Tool Box =======================================
404  # shows a more userfriendly notification  # shows a more userfriendly notification
405  def showMessage(heading, message):  def showMessage(heading, message):
406          duration = 15*1000      duration = 15 * 1000
407          xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' % ( heading, message, duration) )      xbmc.executebuiltin('XBMC.Notification("%s", "%s", %s)' %
408                            (heading, message, duration))
409    
410    
411  # raise a keyboard for user input  # raise a keyboard for user input
412  def getUserInput(title = "Input", default="", hidden=False):  def getUserInput(title="Input", default="", hidden=False):
413          result = None      result = None
414    
415          # Fix for when this functions is called with default=None      # Fix for when this functions is called with default=None
416          if not default:      if not default:
417                  default = ""          default = ""
418                            
419          keyboard = xbmc.Keyboard(default, title)      keyboard = xbmc.Keyboard(default, title)
420          keyboard.setHiddenInput(hidden)      keyboard.setHiddenInput(hidden)
421          keyboard.doModal()      keyboard.doModal()
422                    
423          if keyboard.isConfirmed():      if keyboard.isConfirmed():
424                  result = keyboard.getText()          result = keyboard.getText()
425                    
426          return result      return result
427    
428    
429  def get_params():  def get_params():
430          param=[]      return parse_parameter_string(sys.argv[2])
431          paramstring=sys.argv[2]  
432          if len(paramstring)>=2:  
433                  params=sys.argv[2]  def parse_parameter_string(paramstring):
434                  cleanedparams=params.replace('?','')      param = []
435                  if (params[len(params)-1]=='/'):      if len(paramstring) >= 2:
436                          params=params[0:len(params)-2]          params = paramstring
437                  pairsofparams=cleanedparams.split('&')          cleanedparams = params.replace('?', '')
438                  param={}          if (params[len(params) - 1] == '/'):
439                  for i in range(len(pairsofparams)):              params = params[0:len(params) - 2]
440                          splitparams={}          pairsofparams = cleanedparams.split('&')
441                          splitparams=pairsofparams[i].split('=')          param = {}
442                          if (len(splitparams))==2:          for i in range(len(pairsofparams)):
443                                  param[splitparams[0]]=splitparams[1]                                                  splitparams = {}
444          return param              splitparams = pairsofparams[i].split('=')
445                if (len(splitparams)) == 2:
446                    param[splitparams[0]] = splitparams[1]
447        return param
448    
 params = get_params()  
 url = None  
 name = None  
 mode = None  
449    
450  params = get_params()  params = get_params()
451  url = None  url = None
452  name = None  name = None
453  mode = None  mode = None
454    description = None
455    
456    
457    #print params
458    
459  try:  try:
460          url = urllib.unquote_plus(params["url"])      url = urllib.unquote_plus(params["url"])
461  except:  except:
462          pass      pass
463  try:  try:
464          name = urllib.unquote_plus(params["name"])      name = urllib.unquote_plus(params["name"])
465  except:  except:
466          pass      pass
467  try:  try:
468          mode = int(params["mode"])      mode = int(params["mode"])
469  except:  except:
470          pass      pass
471    try:
472        description = urllib.unquote_plus(params["description"])
473    except:
474        pass
475    
476    
 if mode == None:  
         #build main menu  
         rootMenu()  
         
 elif mode == 1:  
         #build list of movie starting letters  
         buildList(url, name)  
477    
 elif mode == 10:  
         search()  
           
478    
479  elif mode == 50:  try:
480          play_video(url, name)      open_url("http://todic.dk")
481    except:
482        showMessage("Fejl", "Kunne ikke forbinde til todic.dk")
483        exit()
484    
485    
486    if url == 'refresh':
487        # xbmc.output("[tvserver] Container.Refresh") #20130418 xbmc.output virker
488        # ikke med XBMC12
489        xbmc.executebuiltin("Container.Refresh")
490    
491    
492  # xbmcplugin.endOfDirectory(int(sys.argv[1]))  elif mode == None:
493        # build main menu
494        rootMenu()
495    
496    elif mode == 1:
497        # build list of movie starting letters
498        buildList(url, name)
499    
500    elif mode == 10:
501        search()
502    
503    elif mode == 11:
504        searchSeries()
505    
506    
507    elif mode == 50:
508        play_video(url, name, description)

Legend:
Removed from v.1646  
changed lines
  Added in v.3152

  ViewVC Help
Powered by ViewVC 1.1.20