Tlapbot/tlapbot/__init__.py

55 lines
1.6 KiB
Python
Raw Normal View History

2022-08-30 15:25:21 +02:00
import os
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
from tlapbot.owncast_helpers import is_stream_live, give_points_to_chat
2022-06-08 14:22:58 +02:00
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
# 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
# overwrite it with config.py
app.config.from_mapping(
DATABASE=os.path.join(app.instance_path, "tlapbot.sqlite")
)
app.config.from_object('tlapbot.default_config')
app.config.from_pyfile('config.py')
# prepare webhooks and redeem dashboard blueprints
2022-08-24 13:47:55 +02:00
from . import owncast_webhooks
from . import owncast_redeem_dashboard
2022-08-24 13:47:55 +02:00
app.register_blueprint(owncast_webhooks.bp)
app.register_blueprint(owncast_redeem_dashboard.bp)
2022-09-26 11:10:58 +02:00
# add db initialization CLI command
from . import db
2022-06-08 14:22:58 +02:00
db.init_app(app)
2022-09-26 11:10:58 +02:00
# add clear queue CLI command
from . import owncast_helpers
app.cli.add_command(owncast_helpers.clear_queue_command)
# scheduler job for giving points to users
2022-08-30 15:25:21 +02:00
def proxy_job():
with app.app_context():
if is_stream_live():
give_points_to_chat(get_db())
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()
points_giver.add_job(proxy_job, 'interval', seconds=app.config['POINTS_CYCLE_TIME']) # change to 10 minutes out of testing
2022-08-30 15:25:21 +02:00
points_giver.start()
2022-06-08 14:22:58 +02:00
return app
2022-06-08 14:22:58 +02:00
if __name__ == '__main__':
create_app()