Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

Diff of /plugin.video.dr.dk.bonanza/addon.py [dd3ee3] .. [5ea834] Maximize Restore

  Switch to side-by-side view

--- a/plugin.video.dr.dk.bonanza/addon.py
+++ b/plugin.video.dr.dk.bonanza/addon.py
@@ -2,159 +2,207 @@
 import re
 import sys
 import simplejson
+import cgi as urlparse
+import urllib2
+from htmlentitydefs import name2codepoint
 
 import xbmc
 import xbmcgui
 import xbmcplugin
-
-import danishaddons
-import danishaddons.web
-import danishaddons.info
+import xbmcaddon
 
 BASE_URL = 'http://www.dr.dk/Bonanza/'
 
-def search():
-    keyboard = xbmc.Keyboard('', danishaddons.msg(30001))
-    keyboard.doModal()
-    if keyboard.isConfirmed():
-        html = danishaddons.web.downloadUrl('http://www.dr.dk/bonanza/search.htm?&type=video&limit=120&needle=' + keyboard.getText().replace(' ', '+')) # don't cache search results
-        addContent(html)
-        xbmcplugin.endOfDirectory(danishaddons.ADDON_HANDLE)
-        xbmcplugin.setContent(danishaddons.ADDON_HANDLE, 'episodes')
-
-
-def showCategories():
-    xbmcplugin.setContent(danishaddons.ADDON_HANDLE, 'tvshows')
-
-    html = danishaddons.web.downloadAndCacheUrl(BASE_URL, os.path.join(danishaddons.ADDON_DATA_PATH, 'categories.html'), 24 * 60)
-    icon = os.path.join(os.getcwd(), 'icon.png')
-
-    item = xbmcgui.ListItem(danishaddons.msg(30001), iconImage = icon)
-    xbmcplugin.addDirectoryItem(danishaddons.ADDON_HANDLE, danishaddons.ADDON_PATH + '?mode=search', item, True)
-    item = xbmcgui.ListItem(danishaddons.msg(30002), iconImage = icon)
-    xbmcplugin.addDirectoryItem(danishaddons.ADDON_HANDLE, danishaddons.ADDON_PATH + '?mode=recommend', item, True)
-
-    for m in re.finditer('<a href="(/Bonanza/kategori/.*\.htm)">(.*)</a>', html):
-        path = m.group(1)
-        title = m.group(2)
-
-        item = xbmcgui.ListItem(title, iconImage = icon)
-        item.setInfo(type = 'video', infoLabels = {
-            'title' : title
-        })
-        url = danishaddons.ADDON_PATH + '?mode=subcat&url=http://www.dr.dk' + path + '&title=' + title
-        xbmcplugin.addDirectoryItem(danishaddons.ADDON_HANDLE, url, item, True)
-
-    xbmcplugin.addSortMethod(danishaddons.ADDON_HANDLE, xbmcplugin.SORT_METHOD_TITLE)
-    xbmcplugin.endOfDirectory(danishaddons.ADDON_HANDLE)
-
-
-def showRecommendations():
-    xbmcplugin.setContent(danishaddons.ADDON_HANDLE, 'tvshows')
-
-    html = danishaddons.web.downloadAndCacheUrl(BASE_URL, os.path.join(danishaddons.ADDON_DATA_PATH, 'recommendations.html'), 24 * 60)
-
-    # remove anything but 'Redaktionens favoritter'
-    html = html[html.find('<span class="tabTitle">Redaktionens favoritter</span>'):]
-    addSubCategories(html)
-    xbmcplugin.addSortMethod(danishaddons.ADDON_HANDLE, xbmcplugin.SORT_METHOD_TITLE)
-    xbmcplugin.endOfDirectory(danishaddons.ADDON_HANDLE)
-
-
-def showSubCategories(url, title):
-    xbmcplugin.setContent(danishaddons.ADDON_HANDLE, 'tvshows')
-
-    html = danishaddons.web.downloadAndCacheUrl(url.replace(' ', '+'), os.path.join(danishaddons.ADDON_DATA_PATH, 'category-' + title + '.html'), 24 * 60)
-
-    # remove 'Redaktionens favoritter' as they are located on every page
-    html = html[:html.find('<span class="tabTitle">Redaktionens favoritter</span>')]
-
-    addSubCategories(html)
-    xbmcplugin.addSortMethod(danishaddons.ADDON_HANDLE, xbmcplugin.SORT_METHOD_TITLE)
-    xbmcplugin.endOfDirectory(danishaddons.ADDON_HANDLE)
-
-def showContent(url, title):
-    html = danishaddons.web.downloadAndCacheUrl(url, os.path.join(danishaddons.ADDON_DATA_PATH, 'content-' + title + '.html'), 60)
-    addContent(html)
-
-    xbmcplugin.endOfDirectory(danishaddons.ADDON_HANDLE)
-    xbmcplugin.setContent(danishaddons.ADDON_HANDLE, 'episodes')
-
-
-
-def addSubCategories(html):
-    xbmcplugin.setContent(danishaddons.ADDON_HANDLE, 'tvshows')
-
-    for m in re.finditer('<a href="(http://www\.dr\.dk/bonanza/serie/[^\.]+\.htm)"[^>]+>..<img src="(http://downol\.dr\.dk/download/bonanza/collectionThumbs/[^"]+)"[^>]+>..<b>([^<]+)</b>..<span>([^<]+)</span>..</a>', html, re.DOTALL):
-        url = m.group(1)
-        image = m.group(2)
-        title = m.group(3)
-        description = m.group(4)
-
-        item = xbmcgui.ListItem(title, iconImage = image)
-        item.setInfo(type = 'video', infoLabels = {
-            'title' : title,
-            'plot' : description
-        })
-        url = danishaddons.ADDON_PATH + '?mode=content&url=' + url + '&title=' + title
-        xbmcplugin.addDirectoryItem(danishaddons.ADDON_HANDLE, url, item, True)
-
-
-def addContent(html):
-    for m in re.finditer('newPlaylist\(([^"]+)"', html):
-        raw = m.group(1)[:-2].replace('&quot;', '"')
-        json = simplejson.loads(raw)
-
-        infoLabels = {}
-        if json['Title'] is not None:
-            infoLabels['title'] = danishaddons.web.decodeHtmlEntities(json['Title'])
-        if json['Description'] is not None:
-            infoLabels['plot'] = danishaddons.web.decodeHtmlEntities(json['Description'])
-        if json['Colophon'] is not None:
-            infoLabels['writer'] = danishaddons.web.decodeHtmlEntities(json['Colophon'])
-        if json['Actors'] is not None:
-            infoLabels['cast'] = danishaddons.web.decodeHtmlEntities(json['Actors'])
-        if json['Rating'] is not None:
-            infoLabels['rating'] = json['Rating']
-        if json['FirstPublished'] is not None:
-            infoLabels['year'] = int(json['FirstPublished'][:4])
-        if json['Duration'] is not None:
-            infoLabels['duration'] = danishaddons.info.secondsToDuration(int(json['Duration']) / 1000)
-
-        item = xbmcgui.ListItem(infoLabels['title'], iconImage = findFileLocation(json, 'Thumb'))
-        item.setInfo('video', infoLabels)
-
-        rtmp_url = findFileLocation(json, 'VideoHigh')
-        if rtmp_url is None:
-            rtmp_url = findFileLocation(json, 'VideoMid')
-        if rtmp_url is None:
-            rtmp_url = findFileLocation(json, 'VideoLow')
-
-        # patch rtmp_url to work with mplayer
-        m = re.match('(rtmp://.*?)/(.*)', rtmp_url)
-        rtmp_url = '%s/bonanza/%s' % (m.group(1), m.group(2))
-        xbmcplugin.addDirectoryItem(danishaddons.ADDON_HANDLE, rtmp_url, item, False)
-
-
-
-def findFileLocation(json, type):
-    for file in json['Files']:
-        if file['Type'] == type:
-            return file['Location']
-    return None
+class Bonanza(object):
+    def search(self):
+        keyboard = xbmc.Keyboard('', ADDON.getLocalizedString(30001))
+        keyboard.doModal()
+        if keyboard.isConfirmed():
+            html = self._downloadUrl('http://www.dr.dk/bonanza/search.htm?&type=video&limit=120&needle=' + keyboard.getText().replace(' ', '+'))
+            self.addContent(html)
+            xbmcplugin.endOfDirectory(HANDLE)
+
+
+    def showCategories(self):
+        html = self._downloadUrl(BASE_URL)
+
+        item = xbmcgui.ListItem(ADDON.getLocalizedString(30001), iconImage = ICON)
+        item.setProperty('Fanart_Image', FANART)
+        xbmcplugin.addDirectoryItem(HANDLE, PATH + '?mode=search', item, True)
+        item = xbmcgui.ListItem(ADDON.getLocalizedString(30002), iconImage = ICON)
+        item.setProperty('Fanart_Image', FANART)
+        xbmcplugin.addDirectoryItem(HANDLE, PATH + '?mode=recommend', item, True)
+
+        for m in re.finditer('<a href="(/Bonanza/kategori/.*\.htm)">(.*)</a>', html):
+            path = m.group(1)
+            title = m.group(2)
+
+            item = xbmcgui.ListItem(title, iconImage = ICON)
+            item.setProperty('Fanart_Image', FANART)
+            item.setInfo(type = 'video', infoLabels = {
+                'title' : title
+            })
+            url = PATH + '?mode=subcat&url=http://www.dr.dk' + path
+            xbmcplugin.addDirectoryItem(HANDLE, url, item, True)
+
+        xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE)
+        xbmcplugin.endOfDirectory(HANDLE)
+
+
+    def showRecommendations(self):
+        html = self._downloadUrl(BASE_URL)
+
+        # remove anything but 'Redaktionens favoritter'
+        html = html[html.find('<span class="tabTitle">Redaktionens Favoritter</span>'):]
+        self.addSubCategories(html)
+        xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE)
+        xbmcplugin.endOfDirectory(HANDLE)
+
+
+    def showSubCategories(self, url):
+        html = self._downloadUrl(url.replace(' ', '+'))
+
+        # remove 'Redaktionens favoritter' as they are located on every page
+        html = html[:html.find('<span class="tabTitle">Redaktionens Favoritter</span>')]
+
+        self.addSubCategories(html)
+        xbmcplugin.addSortMethod(HANDLE, xbmcplugin.SORT_METHOD_TITLE)
+        xbmcplugin.endOfDirectory(HANDLE)
+
+    def showContent(self, url):
+        html = self._downloadUrl(url)
+        self.addContent(html)
+
+        xbmcplugin.endOfDirectory(HANDLE)
+
+    def addSubCategories(self, html):
+        for m in re.finditer('<a href="(http://www\.dr\.dk/bonanza/serie/[^\.]+\.htm)"[^>]+>..<img src="(http://downol\.dr\.dk/download/bonanza/collectionThumbs/[^"]+)"[^>]+>..<b>([^<]+)</b>..<span>([^<]+)</span>..</a>', html, re.DOTALL):
+            url = m.group(1)
+            image = m.group(2)
+            title = m.group(3)
+            description = m.group(4)
+
+            item = xbmcgui.ListItem(title, iconImage = image)
+            item.setProperty('Fanart_Image', FANART)
+            item.setInfo(type = 'video', infoLabels = {
+                'title' : title,
+                'plot' : description
+            })
+            url = PATH + '?mode=content&url=' + url
+            xbmcplugin.addDirectoryItem(HANDLE, url, item, True)
+
+
+    def addContent(self, html):
+        for m in re.finditer('newPlaylist\(([^"]+)"', html):
+            raw = m.group(1)[:-2].replace('&quot;', '"')
+            json = simplejson.loads(raw)
+
+            infoLabels = {}
+            if json.has_key('Title') and json['Title'] is not None:
+                infoLabels['title'] = self._decodeHtmlEntities(json['Title'])
+            if json.has_key('Description') and json['Description'] is not None:
+                infoLabels['plot'] = self._decodeHtmlEntities(json['Description'])
+            if json.has_key('Colophon') and json['Colophon'] is not None:
+                infoLabels['writer'] = self._decodeHtmlEntities(json['Colophon'])
+            if json.has_key('Actors') and json['Actors'] is not None:
+                infoLabels['cast'] = self._decodeHtmlEntities(json['Actors'])
+            if json.has_key('Rating') and json['Rating'] is not None:
+                infoLabels['rating'] = json['Rating']
+            if json.has_key('FirstPublished') and json['FirstPublished'] is not None:
+                infoLabels['year'] = int(json['FirstPublished'][:4])
+            if json.has_key('Duration') and json['Duration'] is not None:
+                infoLabels['duration'] = self._secondsToDuration(int(json['Duration']) / 1000)
+
+            item = xbmcgui.ListItem(infoLabels['title'], iconImage = self.findFileLocation(json, 'Thumb'))
+            item.setProperty('Fanart_Image', FANART)
+            item.setInfo('video', infoLabels)
+
+            rtmp_url = self.findFileLocation(json, 'VideoHigh')
+            if rtmp_url is None:
+                rtmp_url = self.findFileLocation(json, 'VideoMid')
+            if rtmp_url is None:
+                rtmp_url = self.findFileLocation(json, 'VideoLow')
+
+            # patch rtmp_url to work with mplayer
+            m = re.match('(rtmp://.*?)/(.*)', rtmp_url)
+            rtmp_url = '%s/bonanza/%s' % (m.group(1), m.group(2))
+            xbmcplugin.addDirectoryItem(HANDLE, rtmp_url, item, False)
+
+    def findFileLocation(self, json, type):
+        for file in json['Files']:
+            if file['Type'] == type:
+                return file['Location']
+        return None
+    
+    def _downloadUrl(self, url):
+        u = urllib2.urlopen(url)
+        data = u.read()
+        u.close()
+        return data
+
+    def _decodeHtmlEntities(self, string):
+        """Decodes the HTML entities found in the string and returns the modified string.
+
+        Both decimal (&#000;) and hexadecimal (&x00;) are supported as well as HTML entities,
+        such as &aelig;
+
+        Keyword arguments:
+        string -- the string with HTML entities
+
+        """
+        def substituteEntity(match):
+            ent = match.group(3)
+            if match.group(1) == "#":
+                # decoding by number
+                if match.group(2) == '':
+                    # number is in decimal
+                    return unichr(int(ent))
+            elif match.group(2) == 'x':
+                # number is in hex
+                return unichr(int('0x'+ent, 16))
+            else:
+                # they were using a name
+                cp = name2codepoint.get(ent)
+                if cp:
+                    return unichr(cp)
+                else:
+                    return match.group()
+
+        entity_re = re.compile(r'&(#?)(x?)(\w+);')
+        return entity_re.subn(substituteEntity, string)[0]
+
+    def _secondsToDuration(self, input):
+        """Formats the seconds to a duration string as used by XBMC.
+    
+        Keyword arguments:
+        input -- the duration in seconds
+    
+        """
+        hours = input / 3600
+        minutes = (input % 3600) / 60
+        seconds = (input % 3600) % 60 
+    
+        return "%02d:%02d:%02d" % (hours, minutes, seconds)
 
 
 if __name__ == '__main__':
-    danishaddons.init(sys.argv)
-    
-    if danishaddons.ADDON_PARAMS.has_key('mode') and danishaddons.ADDON_PARAMS['mode'] == 'subcat':
-        showSubCategories(danishaddons.ADDON_PARAMS['url'], danishaddons.ADDON_PARAMS['title'])
-    elif danishaddons.ADDON_PARAMS.has_key('mode') and danishaddons.ADDON_PARAMS['mode'] == 'content':
-        showContent(danishaddons.ADDON_PARAMS['url'], danishaddons.ADDON_PARAMS['title'])
-    elif danishaddons.ADDON_PARAMS.has_key('mode') and danishaddons.ADDON_PARAMS['mode'] == 'search':
-        search()
-    elif danishaddons.ADDON_PARAMS.has_key('mode') and danishaddons.ADDON_PARAMS['mode'] == 'recommend':
-        showRecommendations()
+    ADDON = xbmcaddon.Addon(id = 'plugin.video.dr.dk.bonanza')
+    PATH = sys.argv[0]
+    HANDLE = int(sys.argv[1])
+    PARAMS = urlparse.parse_qs(sys.argv[2][1:])
+
+    ICON = os.path.join(ADDON.getAddonInfo('path'), 'icon.png')
+    FANART = os.path.join(ADDON.getAddonInfo('path'), 'fanart.jpg')
+
+    b = Bonanza()
+    if PARAMS.has_key('mode') and PARAMS['mode'][0] == 'subcat':
+        b.showSubCategories(PARAMS['url'][0])
+    elif PARAMS.has_key('mode') and PARAMS['mode'][0] == 'content':
+        b.showContent(PARAMS['url'][0])
+    elif PARAMS.has_key('mode') and PARAMS['mode'][0] == 'search':
+        b.search()
+    elif PARAMS.has_key('mode') and PARAMS['mode'][0] == 'recommend':
+        b.showRecommendations()
     else:
-        showCategories()
-
+        b.showCategories()
+