def donations(widget_id, **options): redis = RedisManager.get() widget = redis.hget( '{streamer}:clr:donations'.format(streamer=StreamHelper.get_streamer()), widget_id) if widget is None: abort(404) widget = json.loads(widget) # Assign indices to all styles and conditions i = 0 log.info(widget['styles']) for style in widget['styles']: style['index'] = i i += 1 log.info(widget['styles']) i = 0 j = 0 for condition in widget['conditions']: condition['index'] = i i += 1 for style in condition['styles']: style['index'] = j j += 1 log.info(widget) operator_order = { '==': 100, '>=': 50, '<=': 50, } widget['conditions'].sort(key=lambda c: (operator_order[c['operator']], c['amount'])) tts_authentication = '' if 'extra' in config: tts_authentication = config['extra'].get('tts_authentication', '') redis = RedisManager.get() twitch_emotes = redis.hgetall('global:emotes:twitch') bttv_emotes = redis.hgetall('global:emotes:bttv') emotes = [] for emote in twitch_emotes: emotes.append({ 'code': emote, 'emote_id': twitch_emotes[emote], }) for emote in bttv_emotes: emotes.append({ 'code': emote, 'emote_hash': bttv_emotes[emote], }) emotes.sort(key=lambda emote: len(emote['code']), reverse=True) return render_template('clr/donations.html', widget=widget, emotes=emotes, tts_authentication=tts_authentication)
def upgrade(): bind = op.get_bind() session = Session(bind=bind) config_data = RedisManager.get().config_get('maxmemory') max_memory = config_data['maxmemory'] print('redis max memory: {}'.format(max_memory)) RedisManager.get().config_set('maxmemory', str(int(max_memory) * 10)) with RedisManager.pipeline_context() as pipeline: streamer = pb_config['main']['streamer'] num_lines_key = '{streamer}:users:num_lines'.format(streamer=streamer) ignored_key = '{streamer}:users:ignored'.format(streamer=streamer) last_active_key = '{streamer}:users:last_active'.format(streamer=streamer) last_seen_key = '{streamer}:users:last_seen'.format(streamer=streamer) banned_key = '{streamer}:users:banned'.format(streamer=streamer) username_raw_key = '{streamer}:users:username_raw'.format(streamer=streamer) pipeline.delete(num_lines_key, ignored_key, last_active_key, last_seen_key, banned_key, username_raw_key) for user in session.query(User): if user.num_lines > 0: pipeline.zadd(num_lines_key, user.username, user.num_lines) if user.ignored: pipeline.hset(ignored_key, user.username, 1) if user.banned: pipeline.hset(banned_key, user.username, 1) if user.username != user.username_raw: pipeline.hset(username_raw_key, user.username, user.username_raw) if user._last_seen: pipeline.hset(last_seen_key, user.username, user._last_seen.timestamp()) if user._last_active: pipeline.hset(last_active_key, user.username, user._last_active.timestamp()) RedisManager.get().config_set('maxmemory', int(max_memory)) ### commands auto generated by Alembic - please adjust! ### with op.batch_alter_table('tb_user') as batch_op: batch_op.drop_column('num_lines') batch_op.drop_column('ignored') batch_op.drop_column('last_active') batch_op.drop_column('last_seen') batch_op.drop_column('banned') ### end Alembic commands ### session.commit()
def update_global_emotes(self): # Try to get cached global emotes from redis _global_emotes = RedisManager.get().get('global:emotes:bttv_global') if _global_emotes and len(_global_emotes) > 0: log.info('Got cached BTTV global emotes!') _global_emotes = json.loads(_global_emotes) else: _global_emotes = self.api.get_global_emotes() if _global_emotes and len(_global_emotes) > 0: RedisManager.get().setex('global:emotes:bttv_global', time=3600, value=json.dumps(_global_emotes, separators=(',', ':'))) if _global_emotes and len(_global_emotes) > 0: self.global_emotes = {} for emote in _global_emotes: self.global_emotes[emote['code']] = emote['emote_hash']
def __init__(self): super().__init__() self.bets = {} redis = RedisManager.get() self.last_game_start = None self.last_game_id = None try: last_game_start_timestamp = int(redis.get('{streamer}:last_hsbet_game_start'.format(streamer=StreamHelper.get_streamer()))) self.last_game_start = datetime.fromtimestamp(last_game_start_timestamp) except (TypeError, ValueError): # Issue with the int-cast pass except (OverflowError, OSError): # Issue with datetime.fromtimestamp pass try: self.last_game_id = int(redis.get('{streamer}:last_hsbet_game_id'.format(streamer=StreamHelper.get_streamer()))) except (TypeError, ValueError): pass self.scheduler = BackgroundScheduler() self.scheduler.start() self.job = self.scheduler.add_job(self.poll_trackobot, 'interval', seconds=15) self.job.pause() self.reminder_job = self.scheduler.add_job(self.reminder_bet, 'interval', seconds=1) self.reminder_job.pause()
def post(self, **options): # Add a single tag to the email args = self.post_parser.parse_args() email = args['email'].lower() new_tag = args['tag'].lower() if len(new_tag) == 0: return { 'message': 'The tag must be at least 1 character long.' }, 400 streamer = StreamHelper.get_streamer() key = '{streamer}:email_tags'.format(streamer=streamer) redis = RedisManager.get() tags_str = redis.hget(key, email) if tags_str is None: tags = [] else: tags = json.loads(tags_str) # Is the tag already active? if new_tag in tags: return { 'message': 'This tag is already set on the email.' }, 409 tags.append(new_tag) redis.hset(key, email, json.dumps(tags)) return { 'message': 'Successfully added the tag {} to {}'.format(new_tag, email) }
def stop_quest(self): HandlerManager.remove_handler('on_user_win_hs_bet', self.on_user_win_hs_bet) redis = RedisManager.get() self.reset_progress(redis=redis) redis.delete(self.hsbet_points_key)
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)
def delete(self, **options): # Add a single tag to the email args = self.delete_parser.parse_args() email = args['email'].lower() new_tag = args['tag'].lower() streamer = StreamHelper.get_streamer() key = '{streamer}:email_tags'.format(streamer=streamer) redis = RedisManager.get() tags_str = redis.hget(key, email) if tags_str is None: tags = [] else: tags = json.loads(tags_str) # Is the tag already active? if new_tag not in tags: return { 'message': 'This tag is not set on the email.' }, 409 tags.remove(new_tag) if len(tags) > 0: redis.hset(key, email, json.dumps(tags)) else: redis.hdel(key, email) return { 'message': 'Successfully removed the tag {} from {}'.format(new_tag, email) }
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)
def __init__(self): super().__init__() self.bets = {} redis = RedisManager.get() self.last_game_start = None self.last_game_id = None try: last_game_start_timestamp = int(redis.get('{streamer}:last_hsbet_game_start'.format(streamer=StreamHelper.get_streamer()))) self.last_game_start = datetime.fromtimestamp(last_game_start_timestamp) except (TypeError, ValueError): # Issue with the int-cast pass except (OverflowError, OSError): # Issue with datetime.fromtimestamp pass try: self.last_game_id = int(redis.get('{streamer}:last_hsbet_game_id'.format(streamer=StreamHelper.get_streamer()))) except (TypeError, ValueError): pass self.job = ScheduleManager.execute_every(15, self.poll_trackobot) self.job.pause() self.reminder_job = ScheduleManager.execute_every(1, self.reminder_bet) self.reminder_job.pause()
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)
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. """ # XXX TODO FIXME from pajbot.managers.redis import RedisManager 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)
def stats(): bot_commands_list = pajbot.web.utils.get_cached_commands() top_5_commands = sorted(bot_commands_list, key=lambda c: c['data']['num_uses'] if c['data'] is not None else -1, reverse=True)[:5] redis = RedisManager.get() # TODO: Make this hideable through some magic setting (NOT config.ini @_@) with DBManager.create_session_scope() as db_session: top_5_line_farmers = [] for redis_user in redis.zrevrangebyscore( '{streamer}:users:num_lines'.format(streamer=StreamHelper.get_streamer()), '+inf', '-inf', start=0, num=5, withscores=True, score_cast_func=int): user = UserManager.get_static(redis_user[0], db_session=db_session) user.save_to_redis = False user.num_lines = redis_user[1] top_5_line_farmers.append(user) return render_template('stats.html', top_5_commands=top_5_commands, top_5_line_farmers=top_5_line_farmers)
def refresh_stream_status_stage2(self, status): try: redis = RedisManager.get() redis.hmset('stream_data', { '{streamer}:online'.format(streamer=self.bot.streamer): status['online'], '{streamer}:viewers'.format(streamer=self.bot.streamer): status['viewers'], '{streamer}:game'.format(streamer=self.bot.streamer): status['game'], }) self.num_viewers = status['viewers'] if status['online']: if self.current_stream is None: self.create_stream(status) if self.current_stream_chunk is None: self.create_stream_chunk(status) if self.current_stream_chunk.broadcast_id != status['broadcast_id']: log.debug('Detected a new chunk!') self.create_stream_chunk(status) self.num_offlines = 0 self.first_offline = None else: if self.online is True: log.info('Offline. {0}'.format(self.num_offlines)) if self.first_offline is None: self.first_offline = datetime.datetime.now() if self.num_offlines >= 10: log.info('Switching to offline state!') self.go_offline() self.num_offlines += 1 except: log.exception('Uncaught exception while refreshing stream status (Stage 2)')
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)
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 {}
def __init__(self): from pajbot.apiwrappers import BTTVApi self.bttv_api = BTTVApi() self.global_emotes = [] streamer = StreamHelper.get_streamer() redis = RedisManager.get() self.channel_emotes = redis.lrange('{streamer}:emotes:bttv_channel_emotes'.format(streamer=streamer), 0, -1) self.all_emotes = []
def get_emote_epmrecord(self, emote_code): redis = RedisManager.get() streamer = StreamHelper.get_streamer() emote_count = redis.zscore('{streamer}:emotes:epmrecord'.format(streamer=streamer), emote_code) if emote_count: return int(emote_count) return None
def __init__(self, username, redis=None): self.username = username self.redis_loaded = False self.save_to_redis = True self.values = {} if redis: self.redis = redis else: self.redis = RedisManager.get()
def start_quest(self): HandlerManager.add_handler('on_user_win_hs_bet', self.on_user_win_hs_bet) redis = RedisManager.get() self.load_progress(redis=redis) self.load_data(redis=redis) self.LIMIT = self.hsbet_points_required
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
def start_quest(self): HandlerManager.add_handler('on_duel_complete', self.on_duel_complete) redis = RedisManager.get() self.load_progress(redis=redis) self.load_data(redis=redis) self.LIMIT = self.points_required
def __init__(self): from pajbot.apiwrappers import BTTVApi self.bttv_api = BTTVApi() self.global_emotes = [] streamer = StreamHelper.get_streamer() _global_emotes = RedisManager.get().hgetall('global:emotes:bttv') try: _channel_emotes = RedisManager.get().hgetall('{streamer}:emotes:bttv_channel_emotes'.format(streamer=streamer)) except: _channel_emotes = {} self.channel_emotes = list(_channel_emotes.keys()) _all_emotes = _global_emotes.copy() _all_emotes.update(_channel_emotes) self.all_emotes = [] for emote_code, emote_hash in _all_emotes.items(): self.all_emotes.append(self.build_emote(emote_code, emote_hash))
def load_progress(self, redis=None): if redis is None: redis = RedisManager.get() self.progress = {} old_progress = redis.hgetall(self.progress_key) for user, progress in old_progress.items(): try: self.progress[user] = int(progress) except (TypeError, ValueError): pass
def get(self, redis=None): if redis is None: redis = RedisManager.get() try: raw_value = redis.hget(self.key, self.id) value = int(raw_value) except (TypeError, ValueError): value = 0 return value
def init(streamer_name): SongRequestQueueManager.streamer_name = streamer_name SongRequestQueueManager.redis = RedisManager.get() SongRequestQueueManager.song_playing_id = SongRequestQueueManager.redis.get( f"{SongRequestQueueManager.streamer_name}:song-playing-id") SongRequestQueueManager.song_queues = { "song-queue": SongRequestQueueManager._get_init_redis("song-queue"), "backup-song-queue": SongRequestQueueManager._get_init_redis("backup-song-queue"), }
def up(cursor, bot): cursor.execute( """ CREATE TABLE admin_log_entry( id INT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, type TEXT NOT NULL, user_id TEXT REFERENCES "user"(id) ON DELETE SET NULL, message TEXT NOT NULL, created_at TIMESTAMPTZ NOT NULL, data JSONB NOT NULL ) """ ) cursor.execute("CREATE INDEX ON admin_log_entry(created_at)") def safe_get_time(time_raw): try: # Attempt #1: 2016-04-13 19:42:19 UTC return datetime.datetime.strptime(time_raw, "%Y-%m-%d %H:%M:%S %Z") except ValueError: pass try: # Attempt #2: 2016-04-13 19:42:19 UTC+00:00 return datetime.datetime.strptime(time_raw, "%Y-%m-%d %H:%M:%S %Z%z") except ValueError: pass # Attempt 3: 2016-04-13 19:42:19.908536 return datetime.datetime.strptime(time_raw, "%Y-%m-%d %H:%M:%S.%f") redis = RedisManager.get() redis_list_raw = redis.lrange(f"{bot.streamer.login}:logs:admin", 0, -1) # 0 redis_list = [json.loads(raw_entry) for raw_entry in redis_list_raw] def redis_to_db_row(redis_entry): created_at_raw = redis_entry["created_at"] created_at = safe_get_time(created_at_raw).replace(tzinfo=datetime.timezone.utc) return ( redis_entry["type"], redis_entry["user_id"], redis_entry["message"], created_at, Json(redis_entry["data"]), # for usage with the JSONB column ) rows = [redis_to_db_row(redis_entry) for redis_entry in redis_list] psycopg2.extras.execute_values( cursor, "INSERT INTO admin_log_entry(type, user_id, message, created_at, data) VALUES %s", rows ) # on success, delete admin logs in redis redis.delete(f"{bot.streamer.login}:logs:admin")
def update_valid_emotes(self): self.valid_emotes = [] for emote_code, emote_hash in self.global_emotes.items(): self.valid_emotes.append(self.build_emote(emote_code, emote_hash)) streamer = StreamHelper.get_streamer() key = '{streamer}:emotes:bttv_channel_emotes'.format(streamer=streamer) for emote_code, emote_hash in list( RedisManager.get().hgetall(key).items()): self.valid_emotes.append(self.build_emote(emote_code, emote_hash))
def load_cached_subemotes(redis_key): try: redis = RedisManager.get() redis_result = redis.get(redis_key) if redis_result is None: return None, None, None obj = {key: [Emote(**args) for args in value] for key, value in json.loads(redis_result).items()} return obj["1"], obj["2"], obj["3"] except: log.exception("Failed to get subemotes from key {} from redis".format(redis_key)) return [], [], []
def winraffle_progress_quest(self, winner): user_progress = self.get_user_progress(winner.username, 0) + 1 if user_progress > 1: # User has already finished this quest return redis = RedisManager.get() self.finish_quest(redis, winner) self.set_user_progress(winner.username, user_progress, redis=redis)
def winraffle_progress_quest(self, winner): user_progress = self.get_user_progress(winner.username, 0) + 1 if user_progress > 1: # User has already finished this quest return redis = RedisManager.get() winner.award_tokens(self.REWARD, redis=redis) self.set_user_progress(winner.username, user_progress, redis=redis)
def load_cached_emotes(redis_key): try: redis = RedisManager.get() redis_result = redis.get(redis_key) if redis_result is None: return None return [Emote(**args) for args in json.loads(redis_result)] except: log.exception("Failed to get emotes from key {} from redis".format( redis_key)) return []
def refresh_sounds(self, **options): self.valid_samples = [] possibleCategories = [ 'bulldog', 'gachi', 'others', 'personalities', 'weeb' ] for category in possibleCategories: for sampleName, sampleURL in RedisManager.get().hgetall( 'playsounds:{}'.format(category)).items(): self.valid_samples.append(sampleName) options['bot'].whisper(options['source'].username, 'Sounds refreshed.')
def __init__(self): super().__init__() self.valid_samples = [] self.sample_cache = [] possibleCategories = [ 'bulldog', 'gachi', 'others', 'personalities', 'weeb' ] for category in possibleCategories: for sampleName, sampleURL in RedisManager.get().hgetall( 'playsounds:{}'.format(category)).items(): self.valid_samples.append(sampleName)
def add_entry(type, source, message, data={}): redis = RedisManager.get() payload = { 'type': type, 'user_id': source.id, 'message': message, 'created_at': str(datetime.datetime.now()), 'data': data, } redis.lpush(AdminLogManager.get_key(), json.dumps(payload))
def redis_test(self, username): try: pipeline = RedisManager.get().pipeline() pipeline.hget('pajlada:users:points', username) pipeline.hget('pajlada:users:ignored', username) pipeline.hget('pajlada:users:banned', username) pipeline.hget('pajlada:users:last_active', username) except: pipeline.reset() finally: b = pipeline.execute() log.info(b)
def load_data(self, redis=None): if redis is None: redis = RedisManager.get() self.current_emote = redis.get(self.current_emote_key) if self.current_emote is None: # randomize an emote global_twitch_emotes = self.bot.emotes.get_global_emotes() self.current_emote = random.choice(global_twitch_emotes) redis.set(self.current_emote_key, self.current_emote) else: self.current_emote = self.current_emote
def add_entry(type, source, message, data={}): redis = RedisManager.get() payload = { "type": type, "user_id": source.id, "message": message, "created_at": str(datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S UTC")), "data": data, } redis.lpush(AdminLogManager.get_key(), json.dumps(payload))
def get_entries(offset=0, limit=50): redis = RedisManager.get() entries = [] for entry in redis.lrange(AdminLogManager.get_key(), offset, limit): try: entries.append(json.loads(entry)) except: log.exception("babyrage") return entries
def __init__(self): from pajbot.apiwrappers import BTTVApi self.api = BTTVApi() self.global_emotes = [] streamer = StreamHelper.get_streamer() _global_emotes = RedisManager.get().hgetall('global:emotes:bttv') try: _channel_emotes = RedisManager.get().hgetall( '{streamer}:emotes:bttv_channel_emotes'.format( streamer=streamer)) except: _channel_emotes = {} self.channel_emotes = list(_channel_emotes.keys()) _all_emotes = _global_emotes.copy() _all_emotes.update(_channel_emotes) self.all_emotes = [] for emote_code, emote_hash in _all_emotes.items(): self.all_emotes.append(self.build_emote(emote_code, emote_hash))
def __init__(self, streamer_name): self.handlers = {} self.pubsub = RedisManager.get().pubsub() self.running = True self.streamer_name = streamer_name self.pubsub.subscribe("test") # need this for keepalive? idk self.thread = threading.Thread(target=self.start, name="SocketManagerThread") self.thread.daemon = True self.thread.start()
def add_entry(type, source, message, data={}): redis = RedisManager.get() payload = { 'type': type, 'user_id': source.id, 'message': message, 'created_at': str(datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S UTC')), 'data': data, } redis.lpush(AdminLogManager.get_key(), json.dumps(payload))
def save_cached_subemotes(redis_key, tier_one, tier_two, tier_three): try: redis = RedisManager.get() # emotes expire after 1 hour dict = {"1": tier_one, "2": tier_two, "3": tier_three} dict = { key: [emote.jsonify() for emote in emotes] for key, emotes in dict.items() } redis.setex(redis_key, 60 * 60, json.dumps(dict)) except: log.exception( "Error saving subemotes to redis key {}".format(redis_key))
def add_entry(entry_type, source, message, data={}): redis = RedisManager.get() payload = { "type": entry_type, "user_id": source.id, "message": message, "created_at": str(pajbot.utils.now().strftime("%Y-%m-%d %H:%M:%S %Z")), "data": data, } redis.lpush(AdminLogManager.get_key(), json.dumps(payload))
def __init__(self, config): self.nickname = config["main"]["nickname"] self.client_id = config["webtwitchapi"]["client_id"] self.client_secret = config["webtwitchapi"]["client_secret"] token = RedisManager.get().get("{}:token".format(self.nickname)) if not token: raise ValueError( "No token set for bot. Log into the bot using the web interface /bot_login route" ) self.token = json.loads(token) self.access_token_expires_at = None
def get(self, redis: Optional[RedisType] = None) -> int: if redis is None: redis = RedisManager.get() value: int = 0 try: raw_value = redis.hget(self.key, self.id) if raw_value: value = int(raw_value) except (TypeError, ValueError): value = 0 return value
def load_cached_channel_emotes(self): streamer = StreamHelper.get_streamer() # Try to get the list of global emotes from redis try: _channel_emotes = RedisManager.get().hgetall( '{streamer}:emotes:bttv_channel_emotes'.format( streamer=streamer)) except: _channel_emotes = {} for code in _channel_emotes: emote_hash = _channel_emotes[code] self.channel_emotes[code] = emote_hash
def __init__(self, streamer_name: str, callback: Callable[[Handler, Any], None]) -> None: self.handlers: Dict[str, List[Handler]] = {} self.pubsub = RedisManager.get().pubsub() self.running = True self.streamer_name = streamer_name self.callback = callback self.pubsub.subscribe("test") # need this for keepalive? idk self.thread = threading.Thread(target=self.start, name="SocketManagerThread") self.thread.daemon = True self.thread.start()
def home(): custom_content = '' redis = RedisManager.get() streamer = StreamHelper.get_streamer() keys = ('online', 'viewers', 'game') stream_data_keys = [ '{streamer}:{key}'.format(streamer=streamer, key=key) for key in keys ] stream_data_list = redis.hmget('stream_data', stream_data_keys) stream_data = { keys[x]: stream_data_list[x] for x in range(0, len(keys)) } keys = StreamHelper.social_keys streamer_info_keys = [ '{streamer}:{key}'.format(streamer=streamer, key=key) for key in keys.keys() ] log.info(streamer_info_keys) streamer_info_list = redis.hmget('streamer_info', streamer_info_keys) streamer_info = collections.OrderedDict() for key in keys: value = streamer_info_list.pop(0) if value: streamer_info[key] = { 'value': keys[key]['format'].format(value), 'title': keys[key]['title'], 'format': keys[key]['format'], } current_quest_key = '{streamer}:current_quest'.format( streamer=StreamHelper.get_streamer()) current_quest_id = redis.get(current_quest_key) if current_quest_id is not None: current_quest = app.module_manager[current_quest_id] if current_quest: current_quest.load_data() else: current_quest = None return render_template('home.html', custom_content=custom_content, current_quest=current_quest, stream_data=stream_data, streamer_info=streamer_info)
def spotify_auth(): try: resp = spotify.authorized_response(spotify=True) except OAuthException as e: log.error(e) log.exception("An exception was caught while authorizing") next_url = get_next_url(request, "state") return redirect(next_url) except Exception as e: log.error(e) log.exception("Unhandled exception while authorizing") return render_template("login_error.html") session["spotify_token"] = (resp["access_token"], ) if resp is None: if "error" in request.args and "error_description" in request.args: log.warning( f"Access denied: reason={request.args['error']}, error={request.args['error_description']}" ) next_url = get_next_url(request, "state") return redirect(next_url) elif type(resp) is OAuthException: log.warning(resp.message) log.warning(resp.data) log.warning(resp.type) next_url = get_next_url(request, "state") return redirect(next_url) data = f'{app.bot_config["spotify"]["client_id"]}:{app.bot_config["spotify"]["client_secret"]}' encoded = str(base64.b64encode(data.encode("utf-8")), "utf-8") headers = {"Authorization": f"Basic {encoded}"} me_api_response = spotify.get("me", headers=headers) redis = RedisManager.get() token_json = UserAccessToken.from_api_response(resp).jsonify() redis.set( f"authentication:spotify-access-token:{me_api_response.data['id']}", json.dumps(token_json)) redis.set( f"authentication:user-refresh-token:{me_api_response.data['id']}", token_json["refresh_token"]) log.info( f"Successfully updated spotify token in redis for user {me_api_response.data['id']}" ) next_url = get_next_url(request, "state") return redirect(next_url)
def load_data(self, redis=None): if redis is None: redis = RedisManager.get() self.hsbet_points_required = redis.get(self.hsbet_points_key) try: self.hsbet_points_required = int(self.hsbet_points_required) except (TypeError, ValueError): pass if self.hsbet_points_required is None: try: self.hsbet_points_required = random.randint(self.settings["min_value"], self.settings["max_value"] + 1) except ValueError: self.hsbet_points_required = 500 redis.set(self.hsbet_points_key, self.hsbet_points_required)
def get_viewer_data(self, redis=None): if self.offline: return False if not redis: redis = RedisManager.get() data = redis.hget("{streamer}:viewer_data".format(streamer=self.bot.streamer), self.current_stream.id) if data is None: data = {} else: data = json.loads(data) return data
def get(username): user = UserManager.find_static(username) if not user: return {"error": "Not found"}, 404 redis = RedisManager.get() key = "{streamer}:users:num_lines".format( streamer=StreamHelper.get_streamer()) rank = redis.zrevrank(key, user.username) if rank is None: rank = redis.zcard(key) else: rank = rank + 1 return user.jsonify()
def on_duel_complete(self, winner, loser, points_won, points_bet): if points_won < 1: return user_progress = self.get_user_progress(winner.username, default=0) if user_progress >= self.get_limit(): return user_progress += 1 redis = RedisManager.get() if user_progress == self.get_limit(): self.finish_quest(redis, winner) self.set_user_progress(winner.username, user_progress, redis=redis)
def on_user_win_hs_bet(self, user, points_won, **rest): if points_won < 1: return user_progress = self.get_user_progress(user, default=0) if user_progress >= self.hsbet_points_required: return user_progress += points_won redis = RedisManager.get() if user_progress >= self.hsbet_points_required: self.finish_quest(redis, user) self.set_user_progress(user, user_progress, redis=redis)
def post(self, widget_id, **options): args = self.post_parser.parse_args() # special case for boolean values args['tts'] = args['tts'] == 'true' # parse json from these string values args['styles'] = json.loads(args['styles']) args['conditions'] = json.loads(args['conditions']) streamer = StreamHelper.get_streamer() key = '{streamer}:clr:donations'.format(streamer=streamer) redis = RedisManager.get() redis.hset(key, widget_id, json.dumps(args)) return {'message': 'GOT EM'}, 200
def _load_secret_key(bot_id: str, streamer_id: str) -> str: from secrets import token_urlsafe from pajbot.managers.redis import RedisManager redis = RedisManager.get() key = f"web_secret_key:{bot_id}:{streamer_id}" value = redis.get(key) if value is None: value = token_urlsafe(64) redis.set(key, value, ex=SECRET_KEY_EXPIRY_SECONDS) return value