示例#1
0
    def search(self, query):
        """ Search through the catalog.

        :param str query:               The query to search for.

        :returns:                        A list of results.
        :rtype: list[resources.lib.solocoo.util.Channel|resources.lib.solocoo.util.Program]
        """
        if not query:
            return []

        _LOGGER.debug('Requesting entitlements')
        entitlements = self._auth.list_entitlements()
        offers = entitlements.get('offers', [])

        _LOGGER.debug('Requesting search listing')
        reply = util.http_get(SOLOCOO_API + '/search', params=dict(query=query), token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        results = []

        # Parse EPG
        results_epg = next((c for c in data.get('collection') if c.get('label') == 'sg.ui.search.epg'), {})
        results.extend([util.parse_channel(asset, offers)
                        for asset in results_epg.get('assets', [])
                        if asset.get('type') == ASSET_TYPE_CHANNEL])

        # Parse replay
        replay = next((c for c in data.get('collection') if c.get('label') == 'sg.ui.search.replay'), {})
        results.extend([util.parse_program(asset, offers)
                        for asset in replay.get('assets', [])])

        return results
    def get_current_epg(self, channels):
        """ Get the currently playing program.

        :returns: A dictionary with the channels as key and a list of Programs as value.
        :rtype: dict[str, list[resources.lib.solocoo.util.Program]]
        """
        # We fetch all programs between now and 3 hours in the future
        date_now = datetime.now(dateutil.tz.UTC)
        date_from = date_now.replace(minute=0, second=0, microsecond=0)
        date_to = (date_from + timedelta(hours=3))

        reply = util.http_get(SOLOCOO_API + '/schedule',
                              params={
                                  'channels': ','.join(channels),
                                  'from': date_from.isoformat().replace('+00:00', ''),
                                  'until': date_to.isoformat().replace('+00:00', ''),
                              },
                              token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        # Parse to a dict (channel: list[Program])
        epg = defaultdict(list)
        for channel, programs in data.get('epg', []).items():
            for program in programs:
                parsed_program = parse_program(program)
                if parsed_program.end > date_now:
                    epg[channel].append(parsed_program)

        return epg
    def get_series(self, series_id):
        """ Get a list of programs of the specified series.

        :param str series_id:          The ID of the series.

        :returns:                       A list of Programs.
        :rtype: list[resources.lib.solocoo.util.Program]
        """
        entitlements = self._auth.list_entitlements()
        offers = entitlements.get('offers', [])

        # Execute query
        reply = util.http_get(SOLOCOO_API + '/assets',
                              params={
                                  'query': 'replayepisodes,' + series_id,
                                  'limit': 1000,
                              },
                              token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        # Parse list to Program objects
        programs = [
            parse_program(program, offers)
            for program in data.get('assets', [])
        ]

        return programs
    def get_replay(self, channel_id):
        """ Get a list of programs that are replayable from the given channel.

        :param str channel_id:          The ID of the asset.

        :returns:                       A list of Programs.
        :rtype: list[resources.lib.solocoo.util.Program]
        """
        entitlements = self._auth.list_entitlements()
        offers = entitlements.get('offers', [])

        # Execute query
        reply = util.http_get(SOLOCOO_API + '/assets',
                              params={
                                  'query':
                                  'replay,groupedseries,station,' + channel_id,
                                  'limit': 1000,
                              },
                              token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        # Parse list to Program objects
        programs = [
            parse_program(program, offers)
            for program in data.get('assets', [])
        ]

        return programs
示例#5
0
    def get_guide(self, channels, date_from=None, date_to=None):
        """ Get the guide for the specified channels and date.

        :param list|str channels:       A single channel or a list of channels to fetch.
        :param str|datetime date_from:  The date of the guide we want to fetch.
        :param str|datetime date_to:    The date of the guide we want to fetch.

        :returns:                       A parsed dict with EPG data.
        :rtype: dict[str, list[resources.lib.solocoo.util.Program]]
        """
        # Allow to specify one channel, and we map it to a list
        if not isinstance(channels, list):
            channels = [channels]

        entitlements = self._auth.list_entitlements()
        offers = entitlements.get('offers', [])

        # Generate dates in UTC format
        if date_from is not None:
            date_from = self._parse_date(date_from)
        else:
            date_from = self._parse_date('today')

        if date_to is not None:
            date_to = self._parse_date(date_to)
        else:
            date_to = (date_from + timedelta(days=1))

        programs = {}

        for i in range(0, len(channels), self.EPG_CHUNK_SIZE):
            _LOGGER.debug('Fetching EPG at index %d', i)

            reply = util.http_get(
                SOLOCOO_API + '/schedule',
                params={
                    'channels': ','.join(channels[i:i + self.EPG_CHUNK_SIZE]),
                    'from': date_from.isoformat().replace('+00:00', ''),
                    'until': date_to.isoformat().replace('+00:00', ''),
                    'maxProgramsPerChannel':
                    2147483647,  # The android app also does this
                },
                token_bearer=self._tokens.jwt_token)
            data = json.loads(reply.text)

            # Parse to a dict (channel: list[Program])
            programs.update({
                channel:
                [parse_program(program, offers) for program in programs]
                for channel, programs in data.get('epg', []).items()
            })

        return programs
示例#6
0
    def get_program(self, uid):
        """ Get program details by calling the API.

        :param str uid:                 The ID of the program.
        :rtype: Program
        """
        reply = util.http_get(SOLOCOO_API + '/assets/{uid}'.format(uid=uid),
                              token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        # Parse to a Program object
        return parse_program(data)
    def get_asset(self, asset_id):
        """ Get channel information for the requested asset.

        :param str asset_id:          The ID of the asset

        :returns: The requested asset.
        :rtype: resources.lib.solocoo.util.Channel|resources.lib.solocoo.util.Program
        """
        _LOGGER.debug('Requesting channel %s', asset_id)
        reply = util.http_get(SOLOCOO_API + '/assets/{asset_id}'.format(asset_id=asset_id),
                              token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        if data.get('type') == ASSET_TYPE_PROGRAM:
            return parse_program(data)

        if data.get('type') == ASSET_TYPE_CHANNEL:
            return parse_channel(data)

        raise Exception('Unknown asset type: %s' % data.get('type'))
示例#8
0
    def get_guide(self, channels, date):
        """ Get the guide for the specified channels and date.

        :param list|str channels:       A single channel or a list of channels to fetch.
        :param str date:                The date of the guide we want to fetch.
        :rtype: dict[str, list[Program]]
        """
        # Allow to specify one channel, and we map it to a list
        if not isinstance(channels, list):
            channels = [channels]

        # _LOGGER.debug('Requesting entitlements')
        # entitlements = self._auth.list_entitlements()

        # Generate dates in UTC format
        # TODO: this could be cleaner. We need times in Zulu timezone.
        date = self._parse_date(date)
        date_from = date.isoformat().replace('+00:00', '')
        date_to = (date + timedelta(days=1)).isoformat().replace('+00:00', '')

        reply = util.http_get(
            SOLOCOO_API + '/schedule',
            params={
                'channels': ','.join(channels),
                'from': date_from,
                'until': date_to,
                'maxProgramsPerChannel':
                2147483647,  # The android app also does this
            },
            token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        # Parse to a dict (channel: list[Program])
        programs = {
            channel: [parse_program(program) for program in programs]
            for channel, programs in data.get('epg', []).items()
        }

        return programs
    def search(self, query):
        """ Search through the catalog.

        :returns: A list of results.

        :rtype: List[]
        """
        _LOGGER.debug('Requesting entitlements')
        entitlements = self._auth.list_entitlements()

        _LOGGER.debug('Requesting search listing')
        reply = util.http_get(SOLOCOO_API + '/search',
                              params=dict(query=query),
                              token_bearer=self._tokens.jwt_token)
        data = json.loads(reply.text)

        results = []

        # Parse EPG
        results_epg = next((c for c in data.get('collection')
                            if c.get('label') == 'sg.ui.search.epg'), {})
        results.extend([
            util.parse_channel(asset)
            for asset in results_epg.get('assets', [])
            if asset.get('type') == ASSET_TYPE_CHANNEL
        ])

        # Parse replay
        replay = next((c for c in data.get('collection')
                       if c.get('label') == 'sg.ui.search.replay'), {})
        results.extend(
            [util.parse_program(asset) for asset in replay.get('assets', [])])

        # Filter only available channels
        results = util.filter_unavailable_assets(
            results, entitlements.get('offers', []))

        return results