Example #1
0
def game_videos(gid):
    """获取游戏的视频 (GET)

    :uri: /games/<string:gid>/videos
    :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页
    :param page: 页码(数据可能有重复, 建议按照maxs分页)
    :param nbr: 每页数量
    :param orderby: 排序方式 ('create_at': 创建时间, 'vv':播放次数)
    :returns: {'videos': list, 'end_page': bool, 'maxs': timestamp}
    """
    params = request.values
    orderby = params.get('orderby', 'create_at')
    maxs = params.get('maxs', None)
    if orderby == 'vv':
        maxs = 10000000 if maxs is not None and int(
            maxs) == 0 else maxs and int(maxs)
    else:
        maxs = time.time() if maxs is not None and int(
            float(maxs)) == 0 else maxs and float(maxs)
    page = int(params.get('page', 1))
    pagesize = int(params.get('nbr', 10))

    videos = list()
    vids = list()
    while len(videos) < pagesize:
        if orderby == 'vv':
            vids = Video.game_hotvideo_ids(gid, page, pagesize, maxs)
        else:
            vids = Video.game_video_ids(gid, page, pagesize, maxs)
        ex_fields = [
            'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed'
        ]
        videos.extend(
            [v.format(exclude_fields=ex_fields) for v in Video.get_list(vids)])

        # 如果按照maxs分页, 不足pagesize个记录则继续查询
        if maxs is not None:
            obj = Video.get_one(vids[-1], check_online=False) if vids else None
            if orderby == 'vv':
                maxs = obj.vv if obj else -1
            else:
                maxs = obj.create_at if obj else 1000

            if len(vids) < pagesize:
                break
        else:
            break

    return {'videos': videos, 'end_page': len(vids) != pagesize, 'maxs': maxs}
Example #2
0
def sub_game_videos(uid):
    """获取用户已订阅游戏的视频 (GET)

    :uri: /users/<string:uid>/subscriptions
    :returns: {'videos': list}
    """
    gids = UserSubGame.sub_game_ids(uid)
    games = Game.get_list(gids)
    ret = []
    for game in games:
        vids = Video.game_video_ids(str(game._id), 1, 4, time.time())
        ex_fields = [
            'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed'
        ]
        videos = [
            v.format(exclude_fields=ex_fields) for v in Video.get_list(vids)
        ]
        ret.append({'game': game.format(), 'videos': videos})
    return {'videos': ret}