コード例 #1
0
def search(aid):
    """搜索 (GET|POST)

    :uri: /activity/live/search/<string:aid>
    :param type: 搜索类型{'user':用户}
    :param keyword: 关键字
    :returns: {'user':list}
    """
    params = request.values
    stype = params.get('type', 'user')
    keyword = params.get('keyword', '')
    keyword = keyword.strip()

    if not stype or not keyword:
        return error.InvalidArguments

    users = list()

    activity_config = ActivityConfig.get_one(aid, check_online=True)
    if not activity_config:
        return error.ActivityNotExist

    if stype in ['user']:
        uids = User.search(keyword)
        _uids = Live_Activity.get_activity_live_by_authors(aid, uids)
        for _uid in _uids:
            user = User.get_one(_uid).format()
            top = Live_Activity.get_live_user_top(aid, _uid,
                                                  activity_config.begin_at,
                                                  activity_config.end_at)
            gold = UserGiftLog.get_user_total_gold(_uid,
                                                   activity_config.begin_at,
                                                   activity_config.end_at)
            users.append({'user': user, 'top': top, 'gold': gold})

    return {'users': users}
コード例 #2
0
def search():
    """搜索 (GET|POST)

    :uri: /search
    :param type: 搜索类型{'all':全部, 'user':用户, 'game':游戏, 'video':视频,
                           'activity_video':活动视频, 'live_number':直播间房号}
    :param keyword: 关键字
    :returns: {'user':list, 'game':list, 'video':list}
    """
    ua = request.headers.get('User-Agent')
    params = request.values
    stype = params.get('type', 'all')
    keyword = params.get('keyword', '')
    keyword = keyword.strip()
    platform = params.get('os')
    version_code = params.get('version_code', 0)

    if not stype or not keyword:
        return error.InvalidArguments

    users = games = videos = activity_videos = lives = list()

    if stype in ['user', 'all']:
        uids = User.search(keyword)
        users = [u.format() for u in User.get_list(uids)]
        users = sorted(users, key=lambda x: x['follower_count'])

    if stype in ['game', 'all']:
        ua_filter = None
        if ua and platform == 'android' and int(version_code) >= 64:
            ua_filter = ua
        gids = Game.search(keyword, ua_filter)
        games = [g.format() for g in Game.get_list(gids, check_online=False)]

    if stype in ['video', 'all']:
        vids = Video.search(keyword)
        videos = [v.format() for v in Video.get_list(vids)]

    if stype in ['activity_video']:
        activity_id = params.get('activity_id', None)
        uids = User.search(keyword)
        _ids = ActivityVideo.get_activity_video_by_authors(uids, activity_id)
        avids = ActivityVideo.search(keyword, activity_id)
        avids.extend(_ids)
        activity_videos = [
            v.format() for v in ActivityVideo.get_list(set(avids))
        ]

    if stype in ['live_number', 'all']:
        uids = User.search_live_number(keyword)
        livers = [u.format() for u in User.get_list(uids)]
        livers = sorted(livers, key=lambda x: x['follower_count'])
        lives_map = {}
        for live in Xlive.get_all_lives():
            lives_map.update({live['user_id']: live})
        lives = list()
        ex_fields = ['user__is_followed', 'game__subscribed']
        for uid in uids:
            uid = str(uid)
            if uid in lives_map:
                ulive = Xlive.format(lives_map[uid], exclude_fields=ex_fields)
                lives.append({'live': ulive, 'user': None})
                continue
            lives.append({'live': None, 'user': User.get_one(uid).format()})

    return {
        'users': users,
        'games': games,
        'videos': videos,
        'activity_videos': activity_videos,
        'lives': lives
    }