本文整理汇总了Python中pajbot.managers.RedisManager类的典型用法代码示例。如果您正苦于以下问题:Python RedisManager类的具体用法?Python RedisManager怎么用?Python RedisManager使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了RedisManager类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: on_stream_stop
def on_stream_stop(self):
if self.current_quest is None:
log.info('No quest active on stream stop.')
return False
self.current_quest.stop_quest()
self.current_quest = None
self.bot.say('Stream ended, quest has been reset.')
redis = RedisManager.get()
# Remove any mentions of the current quest
redis.delete(self.current_quest_key)
last_stream_id = StreamHelper.get_last_stream_id()
if last_stream_id is False:
log.error('No last stream ID found.')
# No last stream ID found. why?
return False
# XXX: Should we use a pipeline for any of this?
# Go through user tokens and remove any from more than 2 streams ago
for key in redis.keys('{streamer}:*:tokens'.format(streamer=StreamHelper.get_streamer())):
all_tokens = redis.hgetall(key)
for stream_id_str in all_tokens:
try:
stream_id = int(stream_id_str)
except (TypeError, ValueError):
log.error('Invalid stream id in tokens by {}'.format(key))
continue
if last_stream_id - stream_id > 1:
log.info('Removing tokens for stream {}'.format(stream_id))
redis.hdel(key, stream_id)
示例2: on_duel_complete
def on_duel_complete(self, winner, loser, points_won, points_bet):
if points_won < 1:
# This duel did not award any points.
# That means it's entirely irrelevant to us
return
if winner.username in self.progress:
total_points_won = self.progress[winner.username]
if total_points_won >= self.points_required:
# The user has already won enough points, and been rewarded already.
return
else:
total_points_won = 0
# If we get here, this means the user has not completed the quest yet.
# And the user won some points in this duel
total_points_won += points_won
redis = RedisManager.get()
if total_points_won >= self.points_required:
# Reward the user with some tokens
winner.award_tokens(self.REWARD, redis=redis)
# Save the users "points won" progress
self.progress[winner.username] = total_points_won
redis.hset(self.progress_key, winner.username, total_points_won)
示例3: start_quest
def start_quest(self):
HandlerManager.add_handler('on_duel_complete', self.on_duel_complete)
redis = RedisManager.get()
self.progress = {}
old_progress = redis.hgetall(self.progress_key)
for user, progress in old_progress.items():
try:
self.progress[user.decode('utf8')] = int(progress)
except (TypeError, ValueError):
pass
self.points_required = redis.get(self.points_required_key)
try:
self.points_required = int(self.points_required)
except (TypeError, ValueError):
pass
if self.points_required is None:
try:
self.points_required = random.randint(self.settings['min_value'], self.settings['max_value'] + 1)
except ValueError:
# someone fucked up
self.points_required = 500
redis.set(self.points_required_key, self.points_required)
示例4: test
def test():
redis = RedisManager.get()
current_quest_key = '{streamer}:current_quest'.format(streamer=StreamHelper.get_streamer())
current_quest_id = redis.get(current_quest_key)
current_quest = module_manager[current_quest_id]
current_quest.load_data()
return render_template('test.html', current_quest=current_quest)
示例5: award_tokens
def award_tokens(self, tokens, redis=None, force=False):
""" Returns True if tokens were awarded properly.
Returns False if not.
Tokens can only be rewarded once per stream ID.
"""
streamer = StreamHelper.get_streamer()
stream_id = StreamHelper.get_current_stream_id()
if stream_id is False:
return False
if redis is None:
redis = RedisManager.get()
key = '{streamer}:{username}:tokens'.format(
streamer=streamer, username=self.username)
if force:
res = True
redis.hset(key, stream_id, tokens)
else:
res = True if redis.hsetnx(key, stream_id, tokens) == 1 else False
if res is True:
HandlerManager.trigger('on_user_gain_tokens', self, tokens)
return res
示例6: progress_quest
def progress_quest(self, amount, limit, completion_reward):
""" Progress the quest for `stream_id` by `amount`.
We load data from redis in case no progress has been made yet.
"""
streamer = StreamHelper.get_streamer()
stream_id = StreamHelper.get_current_stream_id()
if stream_id is False:
return False
redis = RedisManager.get()
quest_progress_key = '{streamer}:{stream_id}:{username}:quest_progress'.format(
streamer=streamer,
stream_id=stream_id,
username=self.username)
if stream_id not in self.quest_progress:
# Load the old progress, or set it to 0 if no progress was found
self.init_quest_progress()
if self.quest_progress[stream_id] >= limit:
# The user has already completed this quest.
return False
self.quest_progress[stream_id] += amount
if self.quest_progress[stream_id] >= limit:
# The user just completed the quest for the first time
self.award_tokens(completion_reward, redis=redis)
return False
redis.set(quest_progress_key, self.quest_progress[stream_id])
示例7: start_quest
def start_quest(self):
HandlerManager.add_handler('on_message', self.on_message)
redis = RedisManager.get()
self.load_progress(redis=redis)
self.load_data(redis=redis)
示例8: get_follow_relationship
def get_follow_relationship(self, username, streamer):
"""Returns the follow relationship between the user and a streamer.
Returns False if `username` is not following `streamer`.
Otherwise, return a datetime object.
This value is cached in Redis for 2 minutes.
"""
redis = RedisManager.get()
fr_key = "fr_{username}_{streamer}".format(username=username, streamer=streamer)
follow_relationship = redis.get(fr_key)
if follow_relationship is None:
try:
data = self.get(endpoints=["users", username, "follows", "channels", streamer], base=self.kraken_url)
created_at = data["created_at"]
redis.setex(fr_key, time=120, value=created_at)
return TwitchAPI.parse_datetime(created_at)
except urllib.error.HTTPError:
redis.setex(fr_key, time=120, value="-1")
return False
except:
log.exception("Unhandled exception in get_follow_relationship")
return False
else:
if follow_relationship == b"-1":
return False
else:
return TwitchAPI.parse_datetime(follow_relationship.decode("utf-8"))
示例9: get_follow_relationship
def get_follow_relationship(self, username, streamer):
"""Returns the follow relationship between the user and a streamer.
Returns False if `username` is not following `streamer`.
Otherwise, return a datetime object.
This value is cached in Redis for 2 minutes.
"""
redis = RedisManager.get()
fr_key = 'fr_{username}_{streamer}'.format(username=username, streamer=streamer)
follow_relationship = redis.get(fr_key)
if follow_relationship is None:
try:
data = self.get(endpoints=['users', username, 'follows', 'channels', streamer], base=self.kraken_url)
created_at = data['created_at']
redis.setex(fr_key, time=120, value=created_at)
return TwitchAPI.parse_datetime(created_at)
except urllib.error.HTTPError:
redis.setex(fr_key, time=120, value='-1')
return False
except:
log.exception('Unhandled exception in get_follow_relationship')
return False
else:
if follow_relationship == '-1':
return False
else:
return TwitchAPI.parse_datetime(follow_relationship)
示例10: stop_quest
def stop_quest(self):
HandlerManager.remove_handler('on_message', self.on_message)
redis = RedisManager.get()
self.reset_progress(redis=redis)
redis.delete(self.current_emote_key)
示例11: stop_quest
def stop_quest(self):
HandlerManager.remove_handler('on_duel_complete', self.on_duel_complete)
redis = RedisManager.get()
self.reset_progress(redis=redis)
redis.delete(self.points_required_key)
示例12: timeout
def timeout(self, timeout_length, warning_module=None, use_warnings=True):
""" Returns a tuple with the follow data:
How long to timeout the user for, and what the punishment string is
set to.
The punishment string is used to clarify whether this was a warning or the real deal.
"""
punishment = 'timed out for {} seconds'.format(timeout_length)
if use_warnings and warning_module is not None:
redis = RedisManager.get()
""" How many chances the user has before receiving a full timeout. """
total_chances = warning_module.settings['total_chances']
warning_keys = self.get_warning_keys(total_chances, warning_module.settings['redis_prefix'])
warnings = self.get_warnings(redis, warning_keys)
chances_used = self.get_chances_used(warnings)
if chances_used < total_chances:
""" The user used up one of his warnings.
Calculate for how long we should time him out. """
timeout_length = warning_module.settings['base_timeout'] * (chances_used + 1)
punishment = 'timed out for {} seconds (warning)'.format(timeout_length)
self.add_warning(redis, warning_module.settings['length'], warning_keys, warnings)
return (timeout_length, punishment)
示例13: spend_tokens
def spend_tokens(self, tokens_to_spend, redis=None):
if redis is None:
redis = RedisManager.get()
user_token_key = '{streamer}:{username}:tokens'.format(
streamer=StreamHelper.get_streamer(), username=self.username)
token_dict = redis.hgetall(user_token_key)
for stream_id in token_dict:
try:
num_tokens = int(token_dict[stream_id])
except (TypeError, ValueError):
continue
if num_tokens == 0:
continue
decrease_by = min(tokens_to_spend, num_tokens)
tokens_to_spend -= decrease_by
num_tokens -= decrease_by
redis.hset(user_token_key, stream_id, num_tokens)
if tokens_to_spend == 0:
return True
return False
示例14: get_tags
def get_tags(self, redis=None):
if redis is None:
redis = RedisManager.get()
val = redis.hget('global:usertags', self.username)
if val:
return json.loads(val)
else:
return {}
示例15: redis_test2
def redis_test2(self, username):
redis = RedisManager.get()
values = [
redis.hget('pajlada:users:points', username),
redis.hget('pajlada:users:ignored', username),
redis.hget('pajlada:users:banned', username),
redis.hget('pajlada:users:last_active', username),
]
return values