def share_live(): """分享直播接口 (GET) :uri: /lives/share :param live_id: 直播ID :return: {'ret': bool} """ user = request.authed_user live_id = request.values.get('live_id') live = Xlive.get_live(live_id) if not live: return error.LiveError('直播不存在') if user: UserTask.check_user_tasks(str(user._id), SHARE_LIVE, 1) return {'ret': True}
def get_video_id(): """通过直播间id获取视频(GET) :uri: /videos/event_id :param: event_id: 直播间id :return: {'video': Object} """ uid = request.authed_user and str(request.authed_user._id) eid = request.values.get('event_id', None) if not eid: return error.LiveError("直播间id不存在") vid = Video.get_video_by_event_id(eid, uid=uid) video = Video.get_one(vid) if not video: return error.VideoNotExist return {'video': video.format()}
def get_match_lives(): """获取推荐的赛事直播列表 (GET) :uri: /lives/match :param live_user_id: 主播ID :param live_name: 主播间名称 :returns: {'lives': list} """ user_id = request.values.get('live_user_id') name = request.values.get('live_name') lives = Xlive.get_match_live(str(user_id), name) print_log('xlive', '[get_match_lives - lives]: {0}'.format(lives)) if not lives: return error.LiveError('直播不存在') lives = [Xlive.format(l) for l in lives] return {'lives': lives}
def report(): """举报 :uri: /user/report :param target_id: 被举报id :param type: 举报原因 :param content: 举报内容 :param source: 举报来源,0:来自视频,1:来自直播,2:来自评论,3:来自回复 :returns: {} """ params = request.values tid = params.get('target_id', None) _type = int(params.get('type', 0)) content = params.get('content', None) source = int(params.get('source', 0)) from_user = request.authed_user if not tid or not _type or source not in range(4): return error.InvalidArguments if source == 0: video = Video.get_one(tid) if not video: return error.VideoNotExist uid = video.author title = video.title elif source == 1: live = Xlive.get_live(tid) if not live: return error.LiveError('直播不存在') uid = live.get('user_id') title = live.get('name') elif source == 2: comment = Comment.get_one(tid) if not comment: return error.CommentNotExist uid = comment.author title = comment.content elif source == 3: reply = Reply.get_one(tid) if not reply: return error.ReplyNotExist uid = reply.owner title = reply.content key = 'lock:reports:object:%s' % (tid) with util.Lockit(Redis, key) as locked: if locked: return error.ReportVideoFailed('举报失败,请稍后再试') if ReportVideo.check_reported(tid, source, from_user._id): return error.ReportVideoExists rv = ReportVideo.init() rv.uid = uid rv.vid = tid rv.type = _type rv.content = content if content else '' rv.source = source rv.from_user = from_user._id rv.status = 0 rv.deleted = 0 rv.bannef = 0 rv.create_model() rpc_id = ReportConfig.get_limit_config(source) rpconfig = ReportConfig.get_one(rpc_id) if not rpconfig: return {} max_limit = rpconfig.max_limit if max_limit and ReportVideo.reach_max_limit(tid, max_limit): _templates = [ u'用户ID %(id)s,标题为"%(title)s"的视频正在被举报,请尽快审核', u'房间号为%(id)s,标题为"%(title)s"的直播正在被举报,请尽快审核', u'用户%(id)s针对"%(title)s"视频的评论正在被举报,请尽快审核', u'用户%(id)s针对"%(title)s"评论的回复正在被举报,请尽快审核' ] _id = tid if source == 1 else uid try: job = 'send_sms' data = dict(group=str(rpconfig.group), content=_templates[source] % { 'id': str(_id), 'title': title }) gm_client = gearman.GearmanClient( app.config['GEARMAN_SERVERS']) gm_client.submit_job(job, json.dumps(data), background=True, wait_until_complete=False, max_retries=5) except gearman.errors.GearmanError: pass except Exception, e: print_log('send_sms_err', str(e))
def query_new_redpacket(): """ :uri: /lives/redpacket/query_new :param os: 平台 :param channels: 渠道(可选) :param version_code: 版本号 :param live_id: 直播间ID :param active_id: 活动ID :return: """ user = request.authed_user params = request.values os = params.get('os', None) channels = params.get('channels', None) version_code = int(params.get('version_code', 0)) live_id = params.get('live_id', None) active_id = params.get('active_id', None) if not os or not version_code or not active_id or not live_id: return error.InvalidArguments live = Xlive.get_live(live_id) if not live: return error.LiveError(u'直播不存在') activity = LiveRedPacket.get_one(active_id) if not activity: return error.RedPacketError(u'直播红包活动不存在!') uid = None province = None if user: uid = str(user._id) phone = str(user.phone) if user.province: province = user.province if not user.province and util.is_mobile_phone(phone): province = Migu.get_user_info_by_account_name(phone) if not isinstance(province, error.ApiError): user.update_model({'$set': {'province': province}}) else: province = None else: return {'red_packet': activity.format()} live_authors = [] if not activity.live_authors else activity.live_authors.split( '\r\n') live_games = [] if not activity.live_games else activity.live_games.split( '\r\n') key_words = [] if not activity.keyword else activity.keyword.split(u',') # 过滤主播 if live_authors and live['user_id'] not in live_authors: return error.RedPacketError(u'不符合活动条件!') # 过滤游戏 if live_games and live['game_id'] not in live_games: return error.RedPacketError(u'不符合活动条件!') # 过滤关键字 if key_words and not any(map(lambda x: x in live['name'], key_words)): return error.RedPacketError(u'不符合活动条件!') if activity.os not in ['all', os]: return error.RedPacketError(u'不符合活动条件!') if (activity.version_code_mix and activity.version_code_mix > version_code) or \ (activity.version_code_max and activity.version_code_max < version_code): return error.RedPacketError(u'不符合活动条件!') if channels and activity.channels and channels not in activity.channels: return error.RedPacketError(u'不符合活动条件!') if activity.login == 'login' and ( not uid or not activity.user_in_group(str(activity.group), uid)): return error.RedPacketError(u'不符合活动条件!') if activity.province and not province: return error.RedPacketError(u'不符合活动条件!') if activity.province and province and province not in activity.province: return error.RedPacketError(u'不符合活动条件!') key = 'lock:receive_red_packet:%s' % (uid) with util.Lockit(Redis, key) as locked: if locked: return error.RedPacketError(u'领取红包失败,请稍后再试!') # 查看用户是否已领取该红包 if UserRedPacket.check_live_redpacket(uid, activity._id): return error.RedPacketError(u'用户已领取该红包!') _urp = UserRedPacket.init() _urp.active_id = activity._id _urp.campaign_id = activity.campaign_id _urp.chance = activity.chance _urp.expire_at = activity.expire_at _urp.user_id = uid _urp.source = 0 # 不可被分享 _urp.create_model() return {'red_packet': _urp.format(activity.chance)}
def live_redpacket(): """直播间红包活动(GET) :uri: /lives/redpacket/info :param os: 平台 :param channels: 渠道(可选) :param version_code: 版本号 :param live_id: 直播间ID :return: {'ret': bool} """ user = request.authed_user params = request.values os = params.get('os', None) channels = params.get('channels', None) version_code = int(params.get('version_code', 0)) live_id = params.get('live_id', None) if not os or not version_code or not live_id: return error.InvalidArguments uid = None province = None if user: uid = str(user._id) phone = str(user.phone) if user.province: province = user.province if not user.province and util.is_mobile_phone(phone): province = Migu.get_user_info_by_account_name(phone) if not isinstance(province, error.ApiError): user.update_model({'$set': {'province': province}}) else: province = None live = Xlive.get_live(live_id) # live = Xlive.test_lives()[0] if not live: return error.LiveError('直播不存在') red_packet = None red_packet_count = 0 # 先获取已存在的红包,以及已经参与的直播间抢红包 _ids = UserRedPacket.user_red_packets(uid) # user_rps = [] lrp_ids = [] for urp in UserRedPacket.get_list(_ids): # 查找用户直播间抽取红包记录 # if urp.resource_id is None: # user_rps.append((urp.campaign_id, urp.resource_id)) lrp_ids.append(str(urp.active_id)) if not LiveRedPacket.get_one(urp.active_id): continue if urp.chance <= 0: continue if red_packet is None or red_packet.expire_at > urp.expire_at: red_packet = urp red_packet_count += urp.chance red_packet = red_packet.format( red_packet_count) if red_packet else red_packet cdrp = None rp_ids = LiveRedPacket.all_ids() for rp in LiveRedPacket.get_list(rp_ids): # 过滤非观看时长红包 if rp.mode != 2: continue # 过滤已参与的红包活动id if str(rp._id) in lrp_ids: continue if rp.os and rp.os not in ['all', os]: continue if (rp.version_code_mix and rp.version_code_mix > version_code) or \ (rp.version_code_max and rp.version_code_max < version_code): continue if channels and rp.channels and channels not in rp.channels: continue if rp.login == 'login' and (not uid or not rp.user_in_group(str(rp.group), uid)): continue if rp.province and not province: continue if rp.province and province and province not in rp.province: continue # 过滤主播 live_authors = [] if not rp.live_authors else rp.live_authors.split( '\r\n') if live_authors and live['user_id'] not in live_authors: continue # 过滤游戏 live_games = [] if not rp.live_games else rp.live_games.split('\r\n') if live_games and live['game_id'] not in live_games: continue # 过滤关键字 key_words = [] if not rp.keyword else rp.keyword.split(u',') if key_words and not any(map(lambda x: x in live['name'], key_words)): continue cdrp = rp.format() break return {'red_packet': red_packet, 'cdrp': cdrp}
def play_live(): """观看直播接口 (GET) :uri: /lives/play :param live_id: 直播ID :param os: 平台 :param channels: 渠道(可选) :param version_code: 版本 :return: {'ret': bool} """ user = request.authed_user params = request.values live_id = params.get('live_id', None) os = params.get('os', None) channels = params.get('channels', None) version_code = int(params.get('version_code', 0)) if not os or not version_code or live_id is None: return error.InvalidArguments live = Xlive.get_live(live_id) if not live: return error.LiveError('直播不存在') uid = None province = None if user: uid = str(user._id) UserTask.check_user_tasks(uid, PLAY_LIVE, 1) task_ids = WatchLiveTask.get_user_tids(uid) phone = str(user.phone) if user.province: province = user.province if not user.province and util.is_mobile_phone(phone): province = Migu.get_user_info_by_account_name(phone) if not isinstance(province, error.ApiError): user.update_model({'$set': {'province': province}}) else: province = None else: task_ids = WatchLiveTask.get_live_tids() task = None for b in WatchLiveTask.get_list(task_ids): if b.os and b.os not in ['all', os]: continue if (b.version_code_mix and b.version_code_mix > version_code) or \ (b.version_code_max and b.version_code_max < version_code): continue if channels and b.channels and channels not in b.channels: continue if b.login == 'login' and (not uid or not b.user_in_group(str(b.group), uid)): continue if b.province and not province: continue if b.province and province and province not in b.province: continue task = b.format(uid) break red_packet, red_packet_count = None, 0 _ids = UserRedPacket.user_red_packets(uid) lrp_ids = [] for urp in UserRedPacket.get_list(_ids): lrp_ids.append(str(urp.active_id)) if not LiveRedPacket.get_one(urp.active_id): continue if urp.source == 0: # 过滤掉所有直播间抽奖机会 continue if urp.chance <= 0: continue if red_packet is None or red_packet.expire_at > urp.expire_at: red_packet = urp red_packet_count += 1 red_packet = red_packet.format( red_packet_count) if red_packet else red_packet cdrp = None rp_ids = LiveRedPacket.all_ids() for rp in LiveRedPacket.get_list(rp_ids): # 过滤非观看时长红包 if rp.mode != 2: continue # 过滤已参与的红包活动id if str(rp._id) in lrp_ids: continue if rp.os and rp.os not in ['all', os]: continue if (rp.version_code_mix and rp.version_code_mix > version_code) or \ (rp.version_code_max and rp.version_code_max < version_code): continue if channels and rp.channels and channels not in rp.channels: continue if rp.login == 'login' and (not uid or not rp.user_in_group(str(rp.group), uid)): continue if rp.province and not province: continue if rp.province and province and province not in rp.province: continue # 过滤主播 live_authors = [] if not rp.live_authors else rp.live_authors.split( '\r\n') if live_authors and live['user_id'] not in live_authors: continue # 过滤游戏 live_games = [] if not rp.live_games else rp.live_games.split('\r\n') if live_games and live['game_id'] not in live_games: continue # 过滤关键字 key_words = [] if not rp.keyword else rp.keyword.split(u',') if key_words and not any(map(lambda x: x in live['name'], key_words)): continue cdrp = rp.format() break return {'ret': True, 'task': task, 'red_packet': red_packet, 'cdrp': cdrp}