2022-08-30 15:25:21 +02:00
|
|
|
import os
|
2022-11-24 16:56:23 +01:00
|
|
|
import logging
|
2022-06-08 14:22:58 +02:00
|
|
|
from flask import Flask
|
2022-08-30 15:25:21 +02:00
|
|
|
from apscheduler.schedulers.background import BackgroundScheduler
|
|
|
|
from tlapbot.db import get_db
|
2023-01-12 14:23:32 +01:00
|
|
|
from tlapbot.owncast_helpers import (is_stream_live, give_points_to_chat,
|
|
|
|
remove_inactive_redeems)
|
2022-10-17 18:47:30 +02:00
|
|
|
|
2022-06-08 14:22:58 +02:00
|
|
|
def create_app(test_config=None):
|
|
|
|
app = Flask(__name__, instance_relative_config=True)
|
2022-11-24 16:56:23 +01:00
|
|
|
|
2022-06-08 14:22:58 +02:00
|
|
|
# ensure the instance folder exists
|
|
|
|
try:
|
|
|
|
os.makedirs(app.instance_path)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
2022-09-26 11:10:58 +02:00
|
|
|
# Prepare config: set db to instance folder, then load default, then
|
2022-10-18 14:44:46 +02:00
|
|
|
# overwrite it with config.py and redeems.py
|
2022-09-26 11:10:58 +02:00
|
|
|
app.config.from_mapping(
|
|
|
|
DATABASE=os.path.join(app.instance_path, "tlapbot.sqlite")
|
|
|
|
)
|
|
|
|
app.config.from_object('tlapbot.default_config')
|
2022-10-18 14:44:46 +02:00
|
|
|
app.config.from_object('tlapbot.default_redeems')
|
2022-11-07 16:29:02 +01:00
|
|
|
app.config.from_pyfile('config.py', silent=True)
|
|
|
|
app.config.from_pyfile('redeems.py', silent=True)
|
2023-01-12 14:23:32 +01:00
|
|
|
app.config.from_mapping(
|
|
|
|
REDEEMS=remove_inactive_redeems(app.config['REDEEMS'],
|
|
|
|
app.config['ACTIVE_TYPES'])
|
|
|
|
)
|
2022-09-26 11:10:58 +02:00
|
|
|
|
2022-11-24 16:56:23 +01:00
|
|
|
# Make logging work for gunicorn-ran instances of tlapbot.
|
2022-11-30 18:00:30 +01:00
|
|
|
if app.config['GUNICORN']:
|
2022-11-24 16:56:23 +01:00
|
|
|
gunicorn_logger = logging.getLogger('gunicorn.error')
|
|
|
|
app.logger.handlers = gunicorn_logger.handlers
|
|
|
|
app.logger.setLevel(gunicorn_logger.level)
|
2023-01-04 12:38:58 +01:00
|
|
|
|
|
|
|
# Check for wrong config that would break Tlapbot
|
|
|
|
if len(app.config['PREFIX']) != 1:
|
|
|
|
raise RuntimeError("Prefix is >1 character. "
|
|
|
|
"Change your config to set 1-character prefix.")
|
2022-11-24 16:56:23 +01:00
|
|
|
|
2022-09-26 11:10:58 +02:00
|
|
|
# prepare webhooks and redeem dashboard blueprints
|
2022-08-24 13:47:55 +02:00
|
|
|
from . import owncast_webhooks
|
2022-09-07 16:44:59 +02:00
|
|
|
from . import owncast_redeem_dashboard
|
2022-08-24 13:47:55 +02:00
|
|
|
app.register_blueprint(owncast_webhooks.bp)
|
2022-09-07 16:44:59 +02:00
|
|
|
app.register_blueprint(owncast_redeem_dashboard.bp)
|
2022-10-17 18:47:30 +02:00
|
|
|
|
2022-11-07 16:45:29 +01:00
|
|
|
# add db CLI commands
|
2022-09-26 11:10:58 +02:00
|
|
|
from . import db
|
2022-06-08 14:22:58 +02:00
|
|
|
db.init_app(app)
|
2022-11-07 16:45:29 +01:00
|
|
|
app.cli.add_command(db.clear_queue_command)
|
|
|
|
app.cli.add_command(db.refresh_counters_command)
|
2023-01-13 11:04:01 +01:00
|
|
|
app.cli.add_command(db.refresh_and_clear_command)
|
2022-11-07 16:45:29 +01:00
|
|
|
|
2022-09-26 11:10:58 +02:00
|
|
|
# scheduler job for giving points to users
|
2022-08-30 15:25:21 +02:00
|
|
|
def proxy_job():
|
|
|
|
with app.app_context():
|
2022-09-14 14:05:10 +02:00
|
|
|
if is_stream_live():
|
2022-11-24 16:06:01 +01:00
|
|
|
app.logger.info("Stream is LIVE. Giving points to chat.")
|
2022-09-14 14:05:10 +02:00
|
|
|
give_points_to_chat(get_db())
|
2022-11-24 16:06:01 +01:00
|
|
|
else:
|
|
|
|
app.logger.info("Stream is NOT LIVE. (Not giving points to chat.)")
|
2022-08-30 15:25:21 +02:00
|
|
|
|
2022-09-26 11:10:58 +02:00
|
|
|
# start scheduler that will give points to users
|
2022-08-30 15:25:21 +02:00
|
|
|
points_giver = BackgroundScheduler()
|
2022-10-17 18:47:30 +02:00
|
|
|
points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME'])
|
2022-08-30 15:25:21 +02:00
|
|
|
points_giver.start()
|
|
|
|
|
2022-06-08 14:22:58 +02:00
|
|
|
return app
|
|
|
|
|
2022-08-17 16:33:40 +02:00
|
|
|
|
2022-06-08 14:22:58 +02:00
|
|
|
if __name__ == '__main__':
|
2022-10-17 18:47:30 +02:00
|
|
|
create_app()
|