Example #1
0
    def parse_mediaurl(self, xml, minpriority, maxpriority):
        debug('parse_mediaurl\n' + xml)

        self.streamtitle = '???'
        mediaurls = []

        try:
            doc = libxml2.parseDoc(xml)
        except libxml2.parserError:
            debug('Invalid XML')
            return mediaurls

        root = doc.getRootElement()
        if root is None:
            debug('No root node')
            return mediaurls
        
        urls_and_priorities = []
        node = root.children
        while node:
            if node.name == 'title':
                self.streamtitle = utils.get_content_unicode(node)
            elif node.name == 'url':
                try:
                    priority = int(node.prop('priority'))
                except (ValueError, TypeError):
                    priority = self.DEFAULT_URL_PRIORITY

                content = node.getContent()
                if priority >= minpriority and priority <= maxpriority and content != '':
                    urls_and_priorities.append((priority, content))
            node = node.next
        doc.freeDoc()

        urls_and_priorities.sort()
        urls_and_priorities.reverse()
        mediaurls = [b[1] for b in urls_and_priorities]

        return mediaurls
Example #2
0
    def send_mainmenu(self):
        """Build the XML main menu from the module description files
        in the hard drive.
        """
        if not os.path.isdir(template_path):
            self.request_done(404, "Can't access service directory %s" %
                              template_path)
            return

        debug('Reading XSLT templates from ' + template_path)

        # Find menu items in the service.xml files in the subdirectories
        menuitems = {}
        for f in os.listdir(template_path):
            if f == 'bin':
                continue

            filename = os.path.join(template_path, f, 'service.xml')
            if not os.path.isfile(filename):
                continue

            try:
                doc = libxml2.parseFile(filename)
            except libxml2.parserError:
                debug("Failed to parse " + filename)
                continue

            title = ''
            url = ''

            root = doc.getRootElement()
            if (root is None) or (root.name != 'service'):
                debug("Root node is not 'service' in " + filename)
                doc.freeDoc()
                continue
            node = root.children
            while node is not None:
                if node.name == 'title':
                    title = utils.get_content_unicode(node)
                elif node.name == 'ref':
                    url = utils.get_content_unicode(node)
                node = node.next
            doc.freeDoc()

            if (title == '') or (url == ''):
                debug("Empty <title> or <ref> in " + filename)
                continue
            
            menuitems[title.lower()] = ('<link>\n'
                                        '<label>%s</label>\n'
                                        '<ref>%s</ref>\n'
                                        '</link>\n' %
                                        (libxml2.newText(title),
                                         libxml2.newText(url)))
        # Sort the menu items
        titles = menuitems.keys()
        titles.sort()

        # Build the menu
        mainmenu = ('<?xml version="1.0"?>\n'
                    '<wvmenu>\n'
                    '<title>Select video source</title>\n')
        for t in titles:
            mainmenu += menuitems[t]
        mainmenu += '</wvmenu>'

        self.dl = download.DummyDownloader(mainmenu,
                                           writefunc=self.writewrapper,
                                           donefunc=self.request_done)
        self.dl.start()