def stats(session): show_id = flask.request.values.get("show") shows = botinteract.get_data(["shows"]) if show_id is not None: show_id = show_id.lower() games = shows.get(show_id, {}).get("games", {}) for game in games.values(): game["show_id"] = show_id else: games = {} for show_id, show in shows.items(): show_name = show.get("name", show_id) for game_id, game in show['games'].items(): game["show_id"] = show_id game["show"] = show_name games["%s-%s" % (show_id, game_id)] = game show_id = None shows = [{"name": show.get("name", name), "id": name} for name, show in shows.items()] shows = sorted(shows, key=lambda show: show["name"].lower()) stats = botinteract.get_data(["stats"]) # Calculate totals for statkey, stat in stats.items(): stat['total'] = sum(g.get('stats',{}).get(statkey,0) for g in games.values()) # Make our lives easier in the template - make sure all our defaults exist, and turn our major dicts into lists for gamekey, game in games.items(): game.setdefault('display', game['name']) game.setdefault('stats', {}) for statkey in stats.keys(): game['stats'].setdefault(statkey, 0) game.setdefault('gamekey', gamekey) game.setdefault('votes', {}) game['votecount'] = len(game['votes']) game['votegood'] = sum(game['votes'].values()) if game['votecount']: game['voteperc'] = 100.0 * game['votegood'] / game['votecount'] else: game['voteperc'] = 0.0 for statkey, stat in stats.items(): stat.setdefault('singular', statkey) stat.setdefault('statkey', statkey) games = list(games.values()) games.sort(key=lambda g: g['display'].upper()) votegames = [g for g in games if g['votecount']] votegames.sort(key=lambda g: -g['voteperc']) stats = list(stats.values()) stats.sort(key=lambda s: -s['total']) # Calculate graph datasets if 'show' in game: game_name_format = "%(display)s (%(show)s)" else: game_name_format = "%(display)s" for stat in stats: stat['graphdata'] = [(game_name_format % game, game['stats'][stat['statkey']]) for game in games if game['stats'][stat['statkey']]] stat['graphdata'].sort(key=lambda pt:-pt[1]) return flask.render_template('stats.html', games=games, votegames=votegames, stats=stats, session=session, shows=shows, show_id=show_id)
def votes(session): data = botinteract.get_data('shows') current_game_id = botinteract.get_current_game() current_show_id = botinteract.get_show() if current_game_id not in data[current_show_id]["games"]: current_game_id = None shows = [{ "id": show_id, "name": show.get("name", show_id), "games": sorted([{ "id": game_id, "name": game["name"], "display": game.get("display", game["name"]), "vote": game.get("votes", {}).get(session["user"]), } for game_id, game in show['games'].items()], key=lambda game: (1 if game['id'] == current_game_id and show_id == current_show_id else 2, game['display'].upper())) } for show_id, show in data.items()] shows.sort(key=lambda show: (1 if show["id"] == current_show_id and current_game_id is not None else 2, show["name"].upper())) return flask.render_template("votes.html", shows=shows, current_show_id=current_show_id, current_game_id=current_game_id, session=session)
def commands(session): mode = flask.request.values.get('mode', 'responses') assert (mode in ('responses', 'explanations')) data = botinteract.get_data(mode) # Prepare the data, and group equivalent commands together data_reverse = {} for command, response_data in data.items(): if isinstance(response_data['response'], list): response_data['response'] = tuple(response_data['response']) elif not isinstance(response_data['response'], tuple): response_data['response'] = (response_data['response'], ) response_data = (response_data['response'], response_data['access']) data_reverse.setdefault(response_data, []).append(command) # Sort some things for commands in data_reverse.values(): commands.sort() data = [(commands, response[0], response[1]) for response, commands in data_reverse.items()] data.sort() return flask.render_template("commands.html", commands=data, len=len, mode=mode, session=session)
def api_votes(): game_id = botinteract.get_current_game() if game_id is None: return "-" show = botinteract.get_show() data = botinteract.get_data(["shows", show, "games", game_id, "votes"]) count = len(data) good = sum(data.values()) return "%.0f%% (%d/%d)" % (100*good/count, good, count)
def api_stats(stat): game_id = botinteract.get_current_game() if game_id is None: return "-" show = botinteract.get_show() count = botinteract.get_data(["shows", show, "games", game_id, "stats", stat]) if not count: count = 0 return str(count)
def api_votes(): game_id = botinteract.get_current_game() if game_id is None: return "-" show = botinteract.get_show() data = botinteract.get_data(["shows", show, "games", game_id, "votes"]) count = len(data) good = sum(data.values()) return "%.0f%% (%d/%d)" % (100 * good / count, good, count)
def api_stats(stat): game_id = botinteract.get_current_game() if game_id is None: return "-" show = botinteract.get_show() count = botinteract.get_data( ["shows", show, "games", game_id, "stats", stat]) if not count: count = 0 return str(count)
def spam_find(conn, cur, session): rules = botinteract.get_data('spam_rules') for rule in rules: rule['re'] = re.compile(rule['re']) starttime = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta(days=14) cur.execute("SELECT source, message, time FROM log WHERE time >= %s AND 'cleared' = ANY(specialuser) ORDER BY time ASC", ( starttime, )) data = [row + (do_check(row[1], rules),) for row in cur] return flask.render_template("spam_find.html", data=data, session=session)
def spam_find(conn, cur, session): rules = botinteract.get_data('spam_rules') for rule in rules: rule['re'] = re.compile(rule['re']) starttime = datetime.datetime.now(tz=pytz.utc) - datetime.timedelta( days=14) cur.execute( "SELECT source, message, time FROM log WHERE time >= %s AND 'cleared' = ANY(specialuser) ORDER BY time ASC", (starttime, )) data = [row + (do_check(row[1], rules), ) for row in cur] return flask.render_template("spam_find.html", data=data, session=session)
def commands(session): mode = flask.request.values.get("mode", "responses") assert mode in ("responses", "explanations") data = botinteract.get_data(mode) # Prepare the data, and group equivalent commands together data_reverse = {} for command, response_data in data.items(): if isinstance(response_data["response"], list): response_data["response"] = tuple(response_data["response"]) elif not isinstance(response_data["response"], tuple): response_data["response"] = (response_data["response"],) response_data = (response_data["response"], response_data["access"]) data_reverse.setdefault(response_data, []).append(command) # Sort some things for commands in data_reverse.values(): commands.sort() data = [(commands, response[0], response[1]) for response, commands in data_reverse.items()] data.sort() return flask.render_template("commands.html", commands=data, len=len, mode=mode, session=session)
def votes(session): data = botinteract.get_data("shows") current_game_id = botinteract.get_current_game() current_show_id = botinteract.get_show() if current_game_id not in data[current_show_id]["games"]: current_game_id = None shows = [ { "id": show_id, "name": show.get("name", show_id), "games": sorted( [ { "id": game_id, "name": game["name"], "display": game.get("display", game["name"]), "vote": game.get("votes", {}).get(session["user"]), } for game_id, game in show["games"].items() ], key=lambda game: ( 1 if game["id"] == current_game_id and show_id == current_show_id else 2, game["display"].upper(), ), ), } for show_id, show in data.items() ] shows.sort( key=lambda show: ( 1 if show["id"] == current_show_id and current_game_id is not None else 2, show["name"].upper(), ) ) return flask.render_template( "votes.html", shows=shows, current_show_id=current_show_id, current_game_id=current_game_id, session=session )
def spam(session): data = botinteract.get_data('spam_rules') return flask.render_template("spam.html", rules=data, session=session)
def spam(session): link_spam = "link_spam" in flask.request.values data = botinteract.get_data('link_spam_rules' if link_spam else 'spam_rules') return flask.render_template("spam.html", rules=data, link_spam=link_spam, session=session)
def stormcount(): today = datetime.datetime.now(config["timezone"]).date().toordinal() data = botinteract.get_data("storm") if data.get("date") != today: return "0" return str(data.get("count", 0))
def stats(session): show_id = flask.request.values.get("show") shows = botinteract.get_data(["shows"]) if show_id is not None: show_id = show_id.lower() games = shows.get(show_id, {}).get("games", {}) for game in games.values(): game["show_id"] = show_id else: games = {} for show_id, show in shows.items(): show_name = show.get("name", show_id) for game_id, game in show['games'].items(): game["show_id"] = show_id game["show"] = show_name games["%s-%s" % (show_id, game_id)] = game show_id = None shows = [{ "name": show.get("name", name), "id": name } for name, show in shows.items()] shows = sorted(shows, key=lambda show: show["name"].lower()) stats = botinteract.get_data(["stats"]) # Calculate totals for statkey, stat in stats.items(): stat['total'] = sum( g.get('stats', {}).get(statkey, 0) for g in games.values()) # Make our lives easier in the template - make sure all our defaults exist, and turn our major dicts into lists for gamekey, game in games.items(): game.setdefault('display', game['name']) game.setdefault('stats', {}) for statkey in stats.keys(): game['stats'].setdefault(statkey, 0) game.setdefault('gamekey', gamekey) game.setdefault('votes', {}) game['votecount'] = len(game['votes']) game['votegood'] = sum(game['votes'].values()) if game['votecount']: game['voteperc'] = 100.0 * game['votegood'] / game['votecount'] else: game['voteperc'] = 0.0 for statkey, stat in stats.items(): stat.setdefault('singular', statkey) stat.setdefault('statkey', statkey) games = list(games.values()) games.sort(key=lambda g: g['display'].upper()) votegames = [g for g in games if g['votecount']] votegames.sort(key=lambda g: -g['voteperc']) stats = list(stats.values()) stats.sort(key=lambda s: -s['total']) # Calculate graph datasets if 'show' in game: game_name_format = "%(display)s (%(show)s)" else: game_name_format = "%(display)s" for stat in stats: stat['graphdata'] = [(game_name_format % game, game['stats'][stat['statkey']]) for game in games if game['stats'][stat['statkey']]] stat['graphdata'].sort(key=lambda pt: -pt[1]) return flask.render_template('stats.html', games=games, votegames=votegames, stats=stats, session=session, shows=shows, show_id=show_id)