Example #1
0
    def get_channels(self):
        """Get channel data from this add-on"""
        _LOGGER.info('Requesting channels from %s...', self.channels_uri)
        if not self.channels_uri:
            return {}

        try:
            data = self._get_data_from_addon(self.channels_uri)
            _LOGGER.debug(data)
        except Exception as exc:  # pylint: disable=broad-except
            _LOGGER.error('Something went wrong while calling %s',
                          self.addon_id,
                          exc_info=1)
            return []

        if data.get('version', 1) > CHANNELS_VERSION:
            _LOGGER.warning(
                'Skipping %s since it uses an unsupported version: %d',
                self.channels_uri, data.get('version'))
            return []

        channels = []
        for channel in data.get('streams', []):
            # Check for required fields
            if not channel.get('name') or not channel.get('stream'):
                _LOGGER.warning('Skipping channel since it is incomplete: %s',
                                channel)
                continue

            # Fix logo path to be absolute
            if channel.get('logo'):
                if not channel.get('logo').startswith(
                    ('http://', 'https://', 'special://', '/')):
                    channel['logo'] = os.path.join(self.addon_path,
                                                   channel.get('logo'))
            else:
                channel['logo'] = kodiutils.addon_icon(self.addon_obj)

            # Add add-on name as group
            if not channel.get('group'):
                channel['group'] = kodiutils.addon_name(self.addon_obj)

            channels.append(channel)

        return channels
Example #2
0
    def get_channels(self):
        """Get channel data from this add-on"""
        _LOGGER.info('Requesting channels from %s...', self.channels_uri)
        if not self.channels_uri:
            return []

        try:
            data = self._get_data_from_addon(self.channels_uri)
            _LOGGER.debug(data)
        except Exception as exc:  # pylint: disable=broad-except
            _LOGGER.error('Something went wrong while calling %s: %s',
                          self.addon_id, exc)
            return []

        # Return M3U8-format as-is without headers
        if not isinstance(data, dict):
            return data.replace('#EXTM3U\n', '')

        # JSON-STREAMS format
        if data.get('version', 1) > CHANNELS_VERSION:
            _LOGGER.warning(
                'Skipping %s since it uses an unsupported version: %d',
                self.channels_uri, data.get('version'))
            return []

        channels = []
        for channel in data.get('streams', []):
            # Check for required fields
            if not channel.get('name') or not channel.get('stream'):
                _LOGGER.warning('Skipping channel since it is incomplete: %s',
                                channel)
                continue

            # Fix logo path to be absolute
            if not channel.get('logo'):
                channel['logo'] = kodiutils.addon_icon(self.addon_obj)
            elif not channel.get('logo').startswith(
                ('http://', 'https://', 'special://', 'resource://', '/')):
                channel['logo'] = os.path.join(self.addon_path,
                                               channel.get('logo'))

            # Ensure group is a set
            if not channel.get('group'):
                channel['group'] = set()
            # Accept string values (backward compatible)
            elif isinstance(channel.get('group'), (bytes, str)):
                channel['group'] = set(channel.get('group').split(';'))
            # Accept string values (backward compatible, py2 version)
            elif sys.version_info.major == 2 and isinstance(
                    channel.get('group'), unicode):  # noqa: F821; pylint: disable=undefined-variable
                channel['group'] = set(channel.get('group').split(';'))
            elif isinstance(channel.get('group'), list):
                channel['group'] = set(list(channel.get('group')))
            else:
                _LOGGER.warning('Channel group is not a list: %s', channel)
                channel['group'] = set()
            # Add add-on name as group, if not already
            channel['group'].add(kodiutils.addon_name(self.addon_obj))

            channels.append(channel)

        return channels