base_url='https://api.twitch.tv/kraken/', request_token_url=None, access_token_method='POST', access_token_url='https://api.twitch.tv/kraken/oauth2/token', authorize_url='https://api.twitch.tv/kraken/oauth2/authorize', ) DBManager.init(config['main']['db']) TimeManager.init_timezone(config['main'].get('timezone', 'UTC')) with DBManager.create_session_scope() as db_session: custom_web_content = {} for web_content in db_session.query(WebContent).filter(WebContent.content is not None): custom_web_content[web_content.page] = web_content.content errors.init(app) api.config = config modules = config['web'].get('modules', '').split() bot_commands_list = [] from flask import make_response from functools import wraps, update_wrapper def nocache(view): @wraps(view) def no_cache(*args, **kwargs): response = make_response(view(*args, **kwargs)) response.headers['Last-Modified'] = datetime.datetime.now() response.headers['Cache-Control'] = 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0, max-age=0'
def init(args): import configparser import datetime import logging import subprocess import sys from flask import request from flask import session from flask_scrypt import generate_random_salt import pajbot.web.common import pajbot.web.routes from pajbot.bot import Bot from pajbot.managers.db import DBManager from pajbot.managers.redis import RedisManager from pajbot.managers.time import TimeManager from pajbot.models.module import ModuleManager from pajbot.models.sock import SocketClientManager from pajbot.streamhelper import StreamHelper from pajbot.utils import load_config from pajbot.web.models import errors from pajbot.web.utils import download_logo log = logging.getLogger(__name__) config = configparser.ConfigParser() config = load_config(args.config) config.read('webconfig.ini') if 'web' not in config: log.error('Missing [web] section in config.ini') sys.exit(1) if 'pleblist_password_salt' not in config['web']: salt = generate_random_salt() config.set('web', 'pleblist_password_salt', salt.decode('utf-8')) if 'pleblist_password' not in config['web']: salt = generate_random_salt() config.set('web', 'pleblist_password', salt.decode('utf-8')) if 'secret_key' not in config['web']: salt = generate_random_salt() config.set('web', 'secret_key', salt.decode('utf-8')) if 'logo' not in config['web']: res = download_logo(config['webtwitchapi']['client_id'], config['main']['streamer']) if res: config.set('web', 'logo', 'set') StreamHelper.init_web(config['main']['streamer']) redis_options = {} if 'redis' in config: redis_options = config._sections['redis'] RedisManager.init(**redis_options) with open(args.config, 'w') as configfile: config.write(configfile) app.bot_modules = config['web'].get('modules', '').split() app.bot_commands_list = [] app.bot_config = config app.secret_key = config['web']['secret_key'] if 'sock' in config and 'sock_file' in config['sock']: SocketClientManager.init(config['sock']['sock_file']) DBManager.init(config['main']['db']) TimeManager.init_timezone(config['main'].get('timezone', 'UTC')) app.module_manager = ModuleManager(None).load() pajbot.web.routes.admin.init(app) pajbot.web.routes.api.init(app) pajbot.web.routes.base.init(app) pajbot.web.common.filters.init(app) pajbot.web.common.assets.init(app) pajbot.web.common.tasks.init(app) pajbot.web.common.menu.init(app) app.register_blueprint(pajbot.web.routes.clr.page) errors.init(app, config) pajbot.web.routes.clr.config = config version = Bot.version last_commit = '' commit_number = 0 try: current_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode('utf8').strip() latest_commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf8').strip()[:8] commit_number = subprocess.check_output(['git', 'rev-list', 'HEAD', '--count']).decode('utf8').strip() last_commit = subprocess.check_output(['git', 'log', '-1', '--format=%cd']).decode('utf8').strip() version = '{0} DEV ({1}, {2}, commit {3})'.format(version, current_branch, latest_commit, commit_number) except: pass default_variables = { 'version': version, 'last_commit': last_commit, 'commit_number': commit_number, 'bot': { 'name': config['main']['nickname'], }, 'site': { 'domain': config['web']['domain'], 'deck_tab_images': config.getboolean('web', 'deck_tab_images'), 'websocket': { 'host': config['websocket'].get('host', config['web']['domain']), 'port': config['websocket']['port'], 'ssl': config.getboolean('websocket', 'ssl') } }, 'streamer': { 'name': config['web']['streamer_name'], 'full_name': config['main']['streamer'] }, 'modules': app.bot_modules, 'request': request, 'session': session, 'google_analytics': config['web'].get('google_analytics', None), } @app.context_processor def current_time(): current_time = {} current_time['current_time'] = datetime.datetime.now() return current_time @app.context_processor def inject_default_variables(): return default_variables
def init(args): import subprocess import sys from flask import request from flask import session import pajbot.utils import pajbot.web.common import pajbot.web.routes from pajbot.managers.db import DBManager from pajbot.managers.redis import RedisManager from pajbot.models.module import ModuleManager from pajbot.models.sock import SocketClientManager from pajbot.streamhelper import StreamHelper from pajbot.utils import load_config from pajbot.web.models import errors from pajbot.web.utils import download_logo from pajbot.web.utils import download_sub_badge config = load_config(args.config) api_client_credentials = ClientCredentials( config["twitchapi"]["client_id"], config["twitchapi"]["client_secret"], config["twitchapi"].get( "redirect_uri", f"https://{config['web']['domain']}/login/authorized"), ) redis_options = {} if "redis" in config: redis_options = dict(config["redis"]) RedisManager.init(redis_options) twitch_id_api = TwitchIDAPI(api_client_credentials) app_token_manager = AppAccessTokenManager(twitch_id_api, RedisManager.get()) twitch_helix_api = TwitchHelixAPI(RedisManager.get(), app_token_manager) twitch_badges_api = TwitchBadgesAPI(RedisManager.get()) app.api_client_credentials = api_client_credentials app.twitch_id_api = twitch_id_api app.twitch_helix_api = twitch_helix_api if "web" not in config: log.error("Missing [web] section in config.ini") sys.exit(1) app.streamer = cfg.load_streamer(config, twitch_helix_api) app.streamer_display = app.streamer.name if "streamer_name" in config["web"]: app.streamer_display = config["web"]["streamer_name"] app.bot_user = cfg.load_bot(config, twitch_helix_api) StreamHelper.init_streamer(app.streamer.login, app.streamer.id, app.streamer.name) try: download_logo(twitch_helix_api, app.streamer) except: log.exception("Error downloading the streamers profile picture") subscriber_badge_version = config["web"].get("subscriber_badge_version", "0") # Specifying a value of -1 in the config will disable sub badge downloading. Useful if you want to keep a custom version of a sub badge for a streamer if subscriber_badge_version != "-1": try: download_sub_badge(twitch_badges_api, app.streamer, subscriber_badge_version) except: log.exception("Error downloading the streamers subscriber badge") SocketClientManager.init(app.streamer.login) app.bot_modules = config["web"].get("modules", "").split() app.bot_commands_list = [] app.bot_config = config # https://flask.palletsprojects.com/en/1.1.x/quickstart/#sessions # https://flask.palletsprojects.com/en/1.1.x/api/#sessions # https://flask.palletsprojects.com/en/1.1.x/api/#flask.Flask.secret_key app.secret_key = _load_secret_key(app.bot_user.id, app.streamer.id) app.bot_dev = "flags" in config and "dev" in config["flags"] and config[ "flags"]["dev"] == "1" DBManager.init(config["main"]["db"]) app.module_manager = ModuleManager(None).load() pajbot.web.routes.admin.init(app) pajbot.web.routes.api.init(app) pajbot.web.routes.base.init(app) # Make a CSRF exemption for the /api/v1/banphrases/test endpoint csrf.exempt("pajbot.web.routes.api.banphrases.apibanphrasetest") pajbot.web.common.filters.init(app) pajbot.web.common.assets.init(app) pajbot.web.common.menu.init(app) app.register_blueprint(pajbot.web.routes.clr.page) errors.init(app, config) pajbot.web.routes.clr.config = config version = VERSION last_commit = None if app.bot_dev: version = extend_version_if_possible(VERSION) try: last_commit = subprocess.check_output( ["git", "log", "-1", "--format=%cd"]).decode("utf8").strip() except: log.exception( "Failed to get last_commit, will not show last commit") default_variables = { "version": version, "last_commit": last_commit, "bot": { "name": app.bot_user.login }, "site": { "domain": config["web"]["domain"], "deck_tab_images": cfg.get_boolean(config["web"], "deck_tab_images", False), "websocket": { "host": config["websocket"].get( "host", f"wss://{config['web']['domain']}/clrsocket") }, }, "streamer": { "name": app.streamer_display, "full_name": app.streamer.login, "id": app.streamer.id }, "modules": app.bot_modules, "request": request, "session": session, "google_analytics": config["web"].get("google_analytics", None), } @app.context_processor def current_time(): current_time = {} current_time["current_time"] = pajbot.utils.now() return current_time @app.context_processor def inject_default_variables(): return default_variables
def init(args): import configparser import datetime import logging import subprocess import sys from flask import request from flask import session from flask.ext.scrypt import generate_random_salt import pajbot.web.common import pajbot.web.routes from pajbot.bot import Bot from pajbot.managers.db import DBManager from pajbot.managers.redis import RedisManager from pajbot.managers.time import TimeManager from pajbot.models.module import ModuleManager from pajbot.models.sock import SocketClientManager from pajbot.streamhelper import StreamHelper from pajbot.tbutil import load_config from pajbot.web.models import errors from pajbot.web.utils import download_logo log = logging.getLogger(__name__) log.info('XD') config = configparser.ConfigParser() config = load_config(args.config) config.read('webconfig.ini') if 'web' not in config: log.error('Missing [web] section in config.ini') sys.exit(1) if 'pleblist_password_salt' not in config['web']: salt = generate_random_salt() config.set('web', 'pleblist_password_salt', salt.decode('utf-8')) if 'secret_key' not in config['web']: salt = generate_random_salt() config.set('web', 'secret_key', salt.decode('utf-8')) if 'logo' not in config['web']: res = download_logo(config['main']['streamer']) if res: config.set('web', 'logo', 'set') StreamHelper.init_web(config['main']['streamer']) redis_options = {} if 'redis' in config: redis_options = config._sections['redis'] RedisManager.init(**redis_options) with open(args.config, 'w') as configfile: config.write(configfile) app.bot_modules = config['web'].get('modules', '').split() app.bot_commands_list = [] app.bot_config = config app.secret_key = config['web']['secret_key'] if 'sock' in config and 'sock_file' in config['sock']: SocketClientManager.init(config['sock']['sock_file']) DBManager.init(config['main']['db']) TimeManager.init_timezone(config['main'].get('timezone', 'UTC')) app.module_manager = ModuleManager(None).load() pajbot.web.routes.admin.init(app) pajbot.web.routes.api.init(app) pajbot.web.routes.base.init(app) pajbot.web.common.filters.init(app) pajbot.web.common.assets.init(app) pajbot.web.common.tasks.init(app) pajbot.web.common.menu.init(app) app.register_blueprint(pajbot.web.routes.clr.page) errors.init(app) pajbot.web.routes.clr.config = config version = Bot.version last_commit = '' commit_number = 0 try: current_branch = subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode('utf8').strip() latest_commit = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode('utf8').strip()[:8] commit_number = subprocess.check_output(['git', 'rev-list', 'HEAD', '--count']).decode('utf8').strip() last_commit = subprocess.check_output(['git', 'log', '-1', '--format=%cd']).decode('utf8').strip() version = '{0} DEV ({1}, {2}, commit {3})'.format(version, current_branch, latest_commit, commit_number) except: pass default_variables = { 'version': version, 'last_commit': last_commit, 'commit_number': commit_number, 'bot': { 'name': config['main']['nickname'], }, 'site': { 'domain': config['web']['domain'], 'deck_tab_images': config.getboolean('web', 'deck_tab_images'), 'websocket': { 'host': config['websocket'].get('host', config['web']['domain']), 'port': config['websocket']['port'], 'ssl': config.getboolean('websocket', 'ssl') } }, 'streamer': { 'name': config['web']['streamer_name'], 'full_name': config['main']['streamer'] }, 'modules': app.bot_modules, 'request': request, 'session': session, 'google_analytics': config['web'].get('google_analytics', None), } if 'streamtip' in config: default_variables['streamtip_data'] = { 'client_id': config['streamtip']['client_id'], 'redirect_uri': config['streamtip']['redirect_uri'], } else: default_variables['streamtip_data'] = { 'client_id': 'MISSING', 'redirect_uri': 'MISSING', } if 'twitchalerts' in config: default_variables['twitchalerts_data'] = { 'client_id': config['twitchalerts']['client_id'], 'redirect_uri': config['twitchalerts']['redirect_uri'], } else: default_variables['twitchalerts_data'] = { 'client_id': 'MISSING', 'redirect_uri': 'MISSING', } @app.context_processor def current_time(): current_time = {} current_time['current_time'] = datetime.datetime.now() return current_time @app.context_processor def inject_default_variables(): return default_variables
def init(args): import subprocess import sys from flask import request from flask import session from flask import g from flask_scrypt import generate_random_salt import pajbot.utils import pajbot.web.common import pajbot.web.routes from pajbot.managers.db import DBManager from pajbot.managers.redis import RedisManager from pajbot.managers.schedule import ScheduleManager from pajbot.managers.songrequest_queue_manager import SongRequestQueueManager from pajbot.models.module import ModuleManager from pajbot.models.sock import SocketClientManager from pajbot.streamhelper import StreamHelper from pajbot.utils import load_config from pajbot.web.models import errors from pajbot.web.utils import download_logo from pajbot.web.utils import download_sub_badge ScheduleManager.init() config = load_config(args.config) # ScheduleManager.init() api_client_credentials = ClientCredentials( config["twitchapi"]["client_id"], config["twitchapi"]["client_secret"], config["twitchapi"]["redirect_uri"] ) redis_options = {} if "redis" in config: redis_options = dict(config["redis"]) RedisManager.init(**redis_options) id_api = TwitchIDAPI(api_client_credentials) app_token_manager = AppAccessTokenManager(id_api, RedisManager.get()) twitch_helix_api = TwitchHelixAPI(RedisManager.get(), app_token_manager) twitch_badges_api = TwitchBadgesAPI(RedisManager.get()) if "web" not in config: log.error("Missing [web] section in config.ini") sys.exit(1) if "secret_key" not in config["web"]: salt = generate_random_salt() config.set("web", "secret_key", salt.decode("utf-8")) with open(args.config, "w") as configfile: config.write(configfile) streamer = config["main"]["streamer"] SongRequestQueueManager.init(streamer) streamer_user_id = twitch_helix_api.get_user_id(streamer) if streamer_user_id is None: raise ValueError("The streamer login name you entered under [main] does not exist on twitch.") StreamHelper.init_streamer(streamer, streamer_user_id) try: download_logo(twitch_helix_api, streamer, streamer_user_id) except: log.exception("Error downloading the streamers profile picture") subscriber_badge_version = config["web"].get("subscriber_badge_version", "0") # Specifying a value of -1 in the config will disable sub badge downloading. Useful if you want to keep a custom version of a sub badge for a streamer if subscriber_badge_version != "-1": try: download_sub_badge(twitch_badges_api, streamer, streamer_user_id, subscriber_badge_version) except: log.exception("Error downloading the streamers subscriber badge") SocketClientManager.init(streamer) app.bot_modules = config["web"].get("modules", "").split() app.bot_commands_list = [] app.bot_config = config app.secret_key = config["web"]["secret_key"] app.bot_dev = "flags" in config and "dev" in config["flags"] and config["flags"]["dev"] == "1" DBManager.init(config["main"]["db"]) app.module_manager = ModuleManager(None).load() pajbot.web.routes.admin.init(app) pajbot.web.routes.api.init(app) pajbot.web.routes.base.init(app) pajbot.web.common.filters.init(app) pajbot.web.common.assets.init(app) pajbot.web.common.menu.init(app) app.register_blueprint(pajbot.web.routes.clr.page) errors.init(app, config) pajbot.web.routes.clr.config = config version = VERSION last_commit = None if app.bot_dev: version = extend_version_if_possible(VERSION) try: last_commit = subprocess.check_output(["git", "log", "-1", "--format=%cd"]).decode("utf8").strip() except: log.exception("Failed to get last_commit, will not show last commit") default_variables = { "version": version, "last_commit": last_commit, "bot": {"name": config["main"]["nickname"]}, "site": { "domain": config["web"]["domain"], "deck_tab_images": config.getboolean("web", "deck_tab_images"), "websocket": {"host": config["websocket"].get("host", f"wss://{config['web']['domain']}/clrsocket")}, "songrequestWS": { "host": config["songrequest-websocket"].get( "host", f"wss://{config['web']['domain']}/songrequest_websocket" ) }, }, "streamer": {"name": config["web"]["streamer_name"], "full_name": config["main"]["streamer"]}, "modules": app.bot_modules, "request": request, "session": session, "google_analytics": config["web"].get("google_analytics", None), } @app.context_processor def current_time(): current_time = {} current_time["current_time"] = pajbot.utils.now() return current_time @app.context_processor def inject_default_variables(): return default_variables
def init(args): import configparser import logging import subprocess import sys from flask import request from flask import session from flask_scrypt import generate_random_salt import pajbot.utils import pajbot.web.common import pajbot.web.routes from pajbot.bot import Bot from pajbot.managers.db import DBManager from pajbot.managers.redis import RedisManager from pajbot.managers.time import TimeManager from pajbot.models.module import ModuleManager from pajbot.models.sock import SocketClientManager from pajbot.models.sock import SocketManager from pajbot.streamhelper import StreamHelper from pajbot.utils import load_config from pajbot.web.models import errors from pajbot.web.utils import download_logo log = logging.getLogger(__name__) config = configparser.ConfigParser() config = load_config(args.config) config.read("webconfig.ini") if "web" not in config: log.error("Missing [web] section in config.ini") sys.exit(1) if "pleblist_password_salt" not in config["web"]: salt = generate_random_salt() config.set("web", "pleblist_password_salt", salt.decode("utf-8")) if "pleblist_password" not in config["web"]: salt = generate_random_salt() config.set("web", "pleblist_password", salt.decode("utf-8")) if "secret_key" not in config["web"]: salt = generate_random_salt() config.set("web", "secret_key", salt.decode("utf-8")) if "logo" not in config["web"]: res = download_logo(config["webtwitchapi"]["client_id"], config["main"]["streamer"]) if res: config.set("web", "logo", "set") StreamHelper.init_web(config["main"]["streamer"]) SocketClientManager.init(config["main"]["streamer"]) redis_options = {} if "redis" in config: redis_options = dict(config["redis"]) RedisManager.init(**redis_options) with open(args.config, "w") as configfile: config.write(configfile) app.bot_modules = config["web"].get("modules", "").split() app.bot_commands_list = [] app.bot_config = config app.secret_key = config["web"]["secret_key"] DBManager.init(config["main"]["db"]) TimeManager.init_timezone(config["main"].get("timezone", "UTC")) app.module_manager = ModuleManager(None).load() pajbot.web.routes.admin.init(app) pajbot.web.routes.api.init(app) pajbot.web.routes.base.init(app) pajbot.web.common.filters.init(app) pajbot.web.common.assets.init(app) pajbot.web.common.menu.init(app) app.register_blueprint(pajbot.web.routes.clr.page) errors.init(app, config) pajbot.web.routes.clr.config = config version = Bot.version last_commit = "" commit_number = 0 try: current_branch = subprocess.check_output( ["git", "rev-parse", "--abbrev-ref", "HEAD"]).decode("utf8").strip() latest_commit = subprocess.check_output(["git", "rev-parse", "HEAD" ]).decode("utf8").strip()[:8] commit_number = subprocess.check_output( ["git", "rev-list", "HEAD", "--count"]).decode("utf8").strip() last_commit = subprocess.check_output( ["git", "log", "-1", "--format=%cd"]).decode("utf8").strip() version = "{0} DEV ({1}, {2}, commit {3})".format( version, current_branch, latest_commit, commit_number) except: pass default_variables = { "version": version, "last_commit": last_commit, "commit_number": commit_number, "bot": { "name": config["main"]["nickname"] }, "site": { "domain": config["web"]["domain"], "deck_tab_images": config.getboolean("web", "deck_tab_images"), "websocket": { "host": config["websocket"].get( "host", "wss://{}/clrsocket".format(config["web"]["domain"])) }, }, "streamer": { "name": config["web"]["streamer_name"], "full_name": config["main"]["streamer"] }, "modules": app.bot_modules, "request": request, "session": session, "google_analytics": config["web"].get("google_analytics", None), } @app.context_processor def current_time(): current_time = {} current_time["current_time"] = pajbot.utils.now() return current_time @app.context_processor def inject_default_variables(): return default_variables
def init(args): import configparser import logging import subprocess import sys from flask import request from flask import session from flask_scrypt import generate_random_salt import pajbot.utils import pajbot.web.common import pajbot.web.routes from pajbot import constants from pajbot.managers.db import DBManager from pajbot.managers.redis import RedisManager from pajbot.managers.time import TimeManager from pajbot.models.module import ModuleManager from pajbot.models.sock import SocketClientManager from pajbot.streamhelper import StreamHelper from pajbot.utils import load_config from pajbot.web.models import errors from pajbot.web.utils import download_logo log = logging.getLogger(__name__) config = configparser.ConfigParser() config = load_config(args.config) config.read("webconfig.ini") api_client_credentials = ClientCredentials( config["twitchapi"]["client_id"], config["twitchapi"]["client_secret"], config["twitchapi"]["redirect_uri"] ) redis_options = {} if "redis" in config: redis_options = dict(config["redis"]) RedisManager.init(**redis_options) id_api = TwitchIDAPI(api_client_credentials) app_token_manager = AppAccessTokenManager(id_api, RedisManager.get()) twitch_helix_api = TwitchHelixAPI(RedisManager.get(), app_token_manager) if "web" not in config: log.error("Missing [web] section in config.ini") sys.exit(1) if "pleblist_password_salt" not in config["web"]: salt = generate_random_salt() config.set("web", "pleblist_password_salt", salt.decode("utf-8")) if "pleblist_password" not in config["web"]: salt = generate_random_salt() config.set("web", "pleblist_password", salt.decode("utf-8")) if "secret_key" not in config["web"]: salt = generate_random_salt() config.set("web", "secret_key", salt.decode("utf-8")) if "logo" not in config["web"]: try: download_logo(twitch_helix_api, config["main"]["streamer"]) config.set("web", "logo", "set") except: log.exception("Error downloading logo") StreamHelper.init_web(config["main"]["streamer"]) SocketClientManager.init(config["main"]["streamer"]) with open(args.config, "w") as configfile: config.write(configfile) app.bot_modules = config["web"].get("modules", "").split() app.bot_commands_list = [] app.bot_config = config app.secret_key = config["web"]["secret_key"] DBManager.init(config["main"]["db"]) TimeManager.init_timezone(config["main"].get("timezone", "UTC")) app.module_manager = ModuleManager(None).load() pajbot.web.routes.admin.init(app) pajbot.web.routes.api.init(app) pajbot.web.routes.base.init(app) pajbot.web.common.filters.init(app) pajbot.web.common.assets.init(app) pajbot.web.common.menu.init(app) app.register_blueprint(pajbot.web.routes.clr.page) errors.init(app, config) pajbot.web.routes.clr.config = config version = extend_version_if_possible(VERSION) try: last_commit = subprocess.check_output(["git", "log", "-1", "--format=%cd"]).decode("utf8").strip() except: log.exception("Failed to get last_commit, will not show last commit") last_commit = None default_variables = { "version": version, "last_commit": last_commit, "bot": {"name": config["main"]["nickname"]}, "site": { "domain": config["web"]["domain"], "deck_tab_images": config.getboolean("web", "deck_tab_images"), "websocket": { "host": config["websocket"].get("host", "wss://{}/clrsocket".format(config["web"]["domain"])) }, }, "streamer": {"name": config["web"]["streamer_name"], "full_name": config["main"]["streamer"]}, "modules": app.bot_modules, "request": request, "session": session, "google_analytics": config["web"].get("google_analytics", None), } @app.context_processor def current_time(): current_time = {} current_time["current_time"] = pajbot.utils.now() return current_time @app.context_processor def inject_default_variables(): return default_variables