def verify_interval(cls, key, interval, valid_count): count = Redis.incr(key) Redis.expire(key, interval) if count > valid_count: return False return True
def verify_request_interval(cls, device, sig): bkey = cls.BLOCK_KEY % ({'device': device}) key = cls.SIG_KEY % ({'sig': sig}) # 同样sig一分钟访问不能超过5次 valid = cls.verify_interval(key, 60, 5) if not valid: Redis.incr(bkey, 1) Redis.expire(bkey, 600) return False key = cls.DEVICE_KEY % ({'device': device}) # 同样的device一分钟访问不能超过500次 valid = cls.verify_interval(key, 60, 500) if not valid: Redis.incr(bkey, 1) Redis.expire(bkey, 600) return False return True
def verify_market_id(cls, mid): key = cls.MARKET_KEY % {'mid': mid} count = Redis.incr(key, 1) Redis.delete(key) return count == 2
def incr_counter(self): key = self.REWARD_COUNTER % {'aid': self._id} return Redis.incr(key)
def incr_used_today(cls, aid): today = datetime.date.today().strftime('%y%m%d') key = cls.USED_TODAY % ({'day': today, 'aid': aid}) Redis.incr(key, amount=1)
def activity_team_support(): """战队支持 :uri: activity/team_support :param source: 投票来源(app_play, activity, activity_share, video_share) :param device: 设备唯一ID :param battle_id: 对战ID :param team_name: 支持战队名字 :return: {'support_count': int} """ params = request.values team_name = params.get('team_name', None) battle_id = params.get('battle_id', None) source = params.get('source', None) device = params.get('device', None) requ_type = params.get('requ_type', None) if not team_name or not source or not device or not battle_id or not requ_type: return error.InvalidArguments # 增加ip限制,每个ip每天限制20次 user_ip = request.remote_addr ip_key = 'ip_limit:%s:%s:%s:%s' % (user_ip, battle_id, team_name, str(datetime.date.today())) ip_limit = Redis.incr(ip_key) if ip_limit == 1: # 第一次设置key过期时间 Redis.expire(ip_key, 86400) if ip_limit > 2000: return error.ActivityVideoNotExist("超出IP限制") battle = Battle.get_one_data(battle_id) if battle: if team_name != battle['team_1'] and team_name != battle['team_2']: return error.MteamNotExist else: return error.MteamNotExist team_support = TeamSupport.get_team_support(battle_id, team_name) if not team_support: team_support = TeamSupport.init() team_support.team_name = team_name team_support.battle_id = ObjectId(battle_id) team_support.ticket = 0 team_support.create_model() support_count = int(team_support['ticket']) if team_support else 0 team_support_record = TeamSupportRecord.get_team_support_record( device, battle_id, team_name) if requ_type == 'support': if not team_support_record: key = 'lock:team_support:%s:%s:%s' % (device, battle_id, team_name) with util.Lockit(Redis, key) as locked: if locked: return error.VoteVideoFailed team_support_record = TeamSupportRecord.init() team_support_record.device = device team_support_record.source = source team_support_record.team_name = team_name team_support_record.battle_id = ObjectId(battle_id) team_support_record.create_model() support_count = support_count + 1 # 票数加1 elif requ_type == 'cancel_support': if team_support_record: key = 'lock:team_support:%s:%s:%s' % (device, battle_id, team_name) with util.Lockit(Redis, key) as locked: if locked: return error.VoteVideoFailed team_support_record.delete_model() support_count = support_count - 1 # 票数减1 else: return error.InvalidArguments return {'support_count': support_count}
def activity_team_vote(): """战队投票 :uri: activity/team_vote :param source: 投票来源(app_play, activity, activity_share, video_share) :param device: 设备唯一ID :param team_id: 战队ID :param ut: 用户ut :return: {'team_vote_count': int} """ user = request.authed_user print_log('activity/team_vote', '[user]: {0}'.format(user)) params = request.values.to_dict() tid = params.get('team_id', None) source = params.get('source', None) device = params.get('device', None) uid = user and str(user._id) if not tid 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, tid, 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限制") mteam = Mteam.get_one(tid) if not mteam: return error.MteamNotExist vote_count = int(mteam['ticket']) mname = str(mteam['mname']) course = MatchCourse.get_course_by_mname(mname) now = int(time.time()) if course: if int(course.course_end) < now: return error.VoteVideoFailed else: return error.VoteVideoFailed vote = VoteMteam.get_vote(uid=uid, device=device, tid=tid) if not vote: key = 'lock:vote_mteam:%s:%s' % (device, tid) with util.Lockit(Redis, key) as locked: if locked: return error.VoteVideoFailed vote = VoteMteam.init() vote.device = device vote.source = source vote.author = ObjectId(uid) vote.target = ObjectId(tid) vote.mname = ObjectId(mname) vote.create_model() # 票数加1 vote_count = vote_count + 1 return {'vote_count': vote_count}
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}