Beispiel #1
0
def activity_video_config(vid):
    """获取app内视频的活动相关配置

    :uri: activity/video/config/<string:vid>
    :return:{"activity": Object, "is_voted": bool, "is_join": bool, "activity_status": bool}
    """
    user = request.authed_user
    params = request.values
    device = params.get('device', None)
    uid = user and str(user._id)

    activity = dict()
    is_join = False
    activity_status = False

    activity_video = ActivityVideo.get_activity_video_by_vid(vid)
    if activity_video:
        is_join = True
        aid = activity_video["activity_id"]
        activity_config = ActivityConfig.get_one(aid, check_online=False)
        if not activity_config.online:
            activity_status = False
        else:
            activity_status = True
        activity = activity_config.format()

    is_voted = True if VoteVideo.get_vote(uid, device, vid) else False
    return {
        "activity": activity,
        "is_voted": is_voted,
        "is_join": is_join,
        "activity_status": activity_status
    }
Beispiel #2
0
def activity_create_video(aid):
    """创建活动视频 (POST&LOGIN)

    :uri: activity/<string:aid>/new-video
    :param video_id: 原始视频id
    :returns: object
    """
    user = request.authed_user
    vid = request.values['video_id']
    video = Video.get_one(str(vid))
    if not video:
        return error.VideoNotExist

    if str(user._id) != str(video.author):
        return error.AuthFailed

    activity_video = ActivityVideo.get_activity_video_by_vid(vid)
    if activity_video:
        return error.ActivityVideoExist

    activity_video = ActivityVideo.init()
    activity_video.title = video.title
    activity_video.video_id = ObjectId(vid)
    activity_video.like_count = video.like
    activity_video.comment_count = video.comment_count
    activity_video.vv = video.vv
    activity_video.author = ObjectId(str(user._id))
    activity_video.activity_id = ObjectId(str(aid))
    activity_video.cover = video.cover
    activity_video.duration = video.duration
    activity_video.game = video.game

    avid = activity_video.create_model()
    video.update_model({'$set': {'activity_ids': [avid]}})

    # 任务检查
    if user:
        UserTask.check_user_tasks(str(user._id), JOIN_COLLECT, 1,
                                  str(video.game), str(aid))

    return ActivityVideo.get_one(avid, check_online=False).format()
Beispiel #3
0
def get_activity_video(vid):
    """获取参赛视频信息(GET&LOGIN)

    :uri: activity/video/<string:vid>
    :param device: 终端ID
    :return: {'activity_video': Object, 'activity_config': Object}
    """

    activity_video = ActivityVideo.get_activity_video_by_vid(vid)
    if not activity_video:
        return error.ActivityVideoNotExist
    activity_config = ActivityConfig.get_one(activity_video['activity_id'])
    top_compete_videos = ActivityVideo.top_video_ids(
        activity_video['activity_id'], activity_config.sort)

    top = top_compete_videos.index(str(activity_video['_id']))
    activity_video = activity_video.format()
    activity_video['top'] = top + 1

    return {
        'activity_video': activity_video,
        'activity_config': activity_config.format()
    }
Beispiel #4
0
def send_gift():
    """赠送礼物 (GET|POST&LOGIN)

    :uri: /gifts/send_gift
    :param user_id: 主播ID
    :param gift_id: 礼物ID
    :param num: 礼物数量
    :param gift_from: 礼物来源(1:直播, 2:录播)
    :param from_id:来源ID(直播ID或者录播视频ID)
    :return: {'ret: bool}

    :if money need cs(充值来源),SDKVersion,dId 
    """
    user = request.authed_user
    gift_id = int(request.values.get('gift_id'))
    to_user_id = request.values.get('user_id')
    num = int(request.values.get('num', 1))
    gift_from = int(request.values.get('gift_from'))
    from_id = request.values.get('from_id')
    user_ip = request.remote_addr
    device = request.values.get('device', None)

    if not gift_id or not to_user_id or num < 1 or not gift_from:
        return error.InvalidArguments

    if to_user_id == str(user._id):
        return error.GiftError('不能给自己赠送礼物哦')

    to_user = User.get_one(to_user_id, check_online=False)
    if not to_user:
        return error.UserNotExist('该视频没有主播')

    available_num = _gift_num()
    if num not in available_num:
        return error.GiftError('礼物数量不符合规则')

    gift = Gift.get_gift(gift_id)
    if not gift:
        return error.GiftError('该礼物不能赠送')
    money_data = {}
    transactionId = ''
    if gift.credit_type == const.MONEY:
        # today_times = UserGiftLog.user_today_gift_id_times(user._id,gift_id,num)
        gift_data = gift.format()
        # max_times = gift_data['per_piece_limit'].get(num)
        # if max_times <= today_times:
        #     return error.GiftError('该档礼物今天的次数已用完')
        from wanx.platforms.migu import PayByMg
        consumeCode = gift_data['per_piece_id'].get(num)
        cs = 6
        SDKVersion = request.values.get('SDKVersion')
        dId = request.values.get('dId', 'null')
        goodsname = gift_data['product_name'] + '_' + str(num)

        pay_mg_data = PayByMg.get_payurl(user,
                                         cs,
                                         SDKVersion,
                                         dId,
                                         consumeCode,
                                         1,
                                         goodsname=goodsname)

        if isinstance(pay_mg_data, error.ApiError):
            return pay_mg_data
        # 创建订单
        pay_order_obj = PayOrder.init()
        # [set(pay_order_obj,attr,value) for attr,value in pay_mg_data.items()]
        pay_order_obj.pay_mg_data = pay_mg_data
        transactionId = pay_mg_data['resultData']['transactionId']
        pay_order_obj.transactionId = transactionId
        pay_order_obj.phone = user.phone
        pay_order_obj.nickname = user.nickname
        pay_order_obj.credit_value = gift.credit_value
        pay_order_obj.total_value = gift.credit_value * num
        pay_order_obj.gift_num = num
        pay_order_obj.finished = 0
        pay_order_obj.product_name = gift_data['product_name']
        pay_order_obj.check_pay_data = {}
        pay_order_obj.pay_info = {"from_user_id": user._id, 'to_user_id': to_user_id, "num": num,
                                  'gift_id': gift_id, \
                                  'gift_from': gift_from, "from_id": from_id}
        pay_order_obj.create_model()
        money_data = {
            'is_money': True,
            "pay_data": pay_mg_data.get("resultData", {})
        }

    ret = False
    key = 'lock:send_gift:%s' % (str(user._id))
    with util.Lockit(Redis, key) as locked:
        if locked:
            return error.GiftError('赠送礼物失败')

        ret = gift.send_to_user(str(user._id),
                                to_user_id,
                                num,
                                gift_from,
                                from_id,
                                transactionId=transactionId)

    if isinstance(ret, error.ApiError):
        return ret

    if money_data:
        return money_data
    # 录播发送消息到中心消息
    if ret and gift_from == const.FROM_RECORD:
        video = Video.get_one(from_id, check_online=False)
        if video:
            Message.send_gift_msg(str(user._id), from_id, 'gift')
            video.update_model({'$inc': {'gift_count': 1, 'gift_num': num}})

    # 直播发送广播信息
    if ret and gift_from == const.FROM_LIVE:
        total = Xlive.get_user_send_gift_count(from_id, str(user._id), gift_id,
                                               num)
        data = dict(user_id=str(user._id),
                    username=user.nickname or user.name,
                    userphoto=user.get_photo(),
                    gift_name=gift.format()['product_name'],
                    gift_image=gift.format()['product_image'],
                    gift_num=num,
                    event_id=from_id,
                    total=total)
        Xlive.send_live_msg(data)

    # 营销数据入库经分  打赏活动
    from wanx.models.activity import ActivityConfig, ActivityVideo
    from wanx.platforms.migu import Marketing
    activity_config = None
    if gift_from == const.FROM_RECORD:
        activity_video = ActivityVideo.get_activity_video_by_vid(from_id)
        if activity_video:
            activity_config = ActivityConfig.get_one(
                activity_video['activity_id'])
    else:
        aids = ActivityConfig.get_by_type(const.FROM_LIVE)
        for a in ActivityConfig.get_list(aids):
            activity_config = a
            break
    if activity_config:
        data_dict = dict(cmd="deliver_gift",
                         opt="{0}/{1}".format(gift.gold_price, to_user_id),
                         deviceid=request.values.get('device', ''),
                         mobile=user.phone,
                         source=request.values.get('source', 'activity'),
                         activityid=str(activity_config['_id']),
                         activityname=activity_config['name'])
        Marketing.jf_report(data_dict)
    # 1118 task1
    # Marketing.trigger_report(user.partner_migu['id'], user.phone, 'send_gift')
    return {'ret': ret}
Beispiel #5
0
def activity_vote():
    """活动投票

    :uri: activity/vote
    :param source: 投票来源(app_play, activity, activity_share, video_share)
    :param device: 设备唯一ID
    :param video_id: 视频ID
    :param ut: 用户ut
    :return: {'vote_count': int}
    """
    user = request.authed_user
    params = request.values.to_dict()

    if not Guard.verify_sig(params):
        return error.InvalidRequest

    vid = params.get('video_id', None)
    source = params.get('source', None)
    device = params.get('device', None)
    uid = user and str(user._id)

    if not vid or not source or not device:
        return error.InvalidArguments

    # 增加ip限制,每个ip每天限制20次
    user_ip = request.remote_addr
    ip_key = 'ip_limit:%s:%s:%s' % (user_ip, vid, str(datetime.date.today()))
    ip_limit = Redis.incr(ip_key)
    if ip_limit == 1:  # 第一次设置key过期时间
        Redis.expire(ip_key, 86400)

    if ip_limit > 20:
        return error.ActivityVideoNotExist("超出IP限制")

    video = Video.get_one(vid)
    if not video:
        return error.VideoNotExist

    activity_video = ActivityVideo.get_activity_video_by_vid(vid)
    if not activity_video:
        return error.ActivityVideoNotExist("该视频未参赛")

    vote_count = activity_video['vote']
    activity_id = activity_video['activity_id']

    activity_config = ActivityConfig.get_one(activity_id)
    if not activity_config or not activity_config.online:
        return error.ActivityEnd

    is_voted = True if VoteVideo.get_vote(uid, device, vid) else False
    if is_voted:
        return error.VoteVideoLimited

    vote = VoteVideo.get_vote(uid=uid, device=device, vid=vid)
    if not vote:
        key = 'lock:vote_video:%s:%s' % (device, vid)
        with util.Lockit(Redis, key) as locked:
            if locked:
                return error.VoteVideoFailed
            vote = VoteVideo.init()
            vote.device = device
            vote.source = source
            vote.author = ObjectId(uid)
            vote.target = ObjectId(vid)
            vote.activity = ObjectId(activity_id)
            vote.create_model()
            # 票数加1
            vote_count = vote_count + 1

    # 营销数据入库经分  投票活动
    from wanx.platforms.migu import Marketing
    data_dict = dict(cmd="vote",
                     opt=vid,
                     deviceid=params.get('device', ''),
                     mobile=user.phone,
                     source=params.get('source', 'activity'),
                     activityid=str(activity_video['activity_id']),
                     activityname=activity_config['name'])
    Marketing.jf_report(data_dict)

    return {'vote_count': vote_count}