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

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

  ViewVC Help
Powered by ViewVC 1.1.20