Exemplo n.º 1
0
def response_to_items(provider, context, json_data):
    result = []

    kind = json_data.get('kind', '')
    if kind == u'youtube#searchListResponse' or kind == u'youtube#playlistItemListResponse' or \
                    kind == u'youtube#playlistListResponse' or kind == u'youtube#subscriptionListResponse' or \
                    kind == u'youtube#guideCategoryListResponse' or kind == u'youtube#channelListResponse' or \
                    kind == u'youtube#videoListResponse':
        result.extend(_process_list_response(provider, context, json_data))
        pass
    else:
        raise kodion.KodimonException("Unknown kind '%s'" % kind)

    # next page
    yt_next_page_token = json_data.get('nextPageToken', '')
    if yt_next_page_token:
        new_params = {}
        new_params.update(context.get_params())
        new_params['page_token'] = yt_next_page_token

        new_context = context.clone(new_params=new_params)

        current_page = int(new_context.get_param('page', 1))
        next_page_item = items.NextPageItem(
            new_context, current_page, fanart=provider.get_fanart(new_context))
        result.append(next_page_item)
        pass

    return result
Exemplo n.º 2
0
Arquivo: v3.py Projeto: camster1/RTOTV
def response_to_items(provider,
                      context,
                      json_data,
                      sort=None,
                      reverse_sort=False,
                      process_next_page=True):
    result = []

    kind = json_data.get('kind', '')
    if kind == u'youtube#searchListResponse' or kind == u'youtube#playlistItemListResponse' or \
                    kind == u'youtube#playlistListResponse' or kind == u'youtube#subscriptionListResponse' or \
                    kind == u'youtube#guideCategoryListResponse' or kind == u'youtube#channelListResponse' or \
                    kind == u'youtube#videoListResponse' or kind == u'youtube#activityListResponse':
        result.extend(_process_list_response(provider, context, json_data))
        pass
    else:
        raise kodion.KodionException("Unknown kind '%s'" % kind)

    if sort is not None:
        result = sorted(result, key=sort, reverse=reverse_sort)
        pass

    # no processing of next page item
    if not process_next_page:
        return result

    # next page
    """
    This will try to prevent the issue 7163 (https://code.google.com/p/gdata-issues/issues/detail?id=7163).
    Somehow the APIv3 is missing the token for the next page. We implemented our own calculation for the token
    into the YouTube client...this should work for up to ~2000 entries.
    """
    yt_total_results = int(
        json_data.get('pageInfo', {}).get('totalResults', 0))
    yt_results_per_page = int(
        json_data.get('pageInfo', {}).get('resultsPerPage', 0))
    page = int(context.get_param('page', 1))
    yt_next_page_token = json_data.get('nextPageToken', '')
    if yt_next_page_token or (page * yt_results_per_page < yt_total_results):
        if not yt_next_page_token:
            client = provider.get_client(context)
            yt_next_page_token = client.calculate_next_page_token(
                page + 1, yt_results_per_page)
            pass

        new_params = {}
        new_params.update(context.get_params())
        new_params['page_token'] = yt_next_page_token

        new_context = context.clone(new_params=new_params)

        current_page = int(new_context.get_param('page', 1))
        next_page_item = items.NextPageItem(
            new_context, current_page, fanart=provider.get_fanart(new_context))
        result.append(next_page_item)
        pass

    return result
Exemplo n.º 3
0
def response_to_items(provider, context, json_data):
    video_id_dict = {}
    channel_item_dict = {}

    result = []

    feed = json_data.get('feed', None)
    if feed:
        entry = feed.get('entry', [])
        for item in entry:
            video_id = ''
            video_id_match = re.match(
                'tag:youtube.com,(\d+):video:(?P<video_id>.*)',
                item['id']['$t'])
            if video_id_match:
                video_id = video_id_match.group('video_id')
                pass
            title = item['title']['$t']

            video_item = items.VideoItem(
                title, context.create_uri(['play'], {'video_id': video_id}))
            video_item.set_fanart(provider.get_fanart(context))
            result.append(video_item)

            video_id_dict[video_id] = video_item
            channel_id = item['media$group']['yt$uploaderId']['$t']
            if not channel_id in channel_item_dict:
                channel_item_dict[channel_id] = []
            channel_item_dict[channel_id].append(video_item)
            pass

        utils.update_video_infos(provider, context, video_id_dict)
        utils.update_channel_infos(provider, context, channel_item_dict)

        page = int(context.get_param('page', 1))
        items_per_page = int(
            feed.get('openSearch$itemsPerPage', {}).get('$t', 0))
        total = int(feed.get('openSearch$totalResults', {}).get('$t', 0))
        if page * items_per_page < total:
            new_params = {}
            new_params.update(context.get_params())
            new_params['start-index'] = (page * items_per_page) + 1

            new_context = context.clone(new_params=new_params)

            current_page = int(new_context.get_param('page', 1))
            next_page_item = items.NextPageItem(
                new_context,
                current_page,
                fanart=provider.get_fanart(new_context))
            result.append(next_page_item)
            pass
        pass
    else:
        raise kodion.KodionException("Couldn't find feed")

    return result