__init__.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import logging
  3. from flask import Flask
  4. from ownchatbot.db import get_db
  5. from ownchatbot.owncast_com import live_now, award_points
  6. from apscheduler.schedulers.background import BackgroundScheduler
  7. def create_app(test_config=None):
  8. app = Flask(__name__, instance_relative_config=True)
  9. try:
  10. os.makedirs(app.instance_path)
  11. except OSError:
  12. pass
  13. app.config.from_mapping(
  14. DATABASE=os.path.join(app.instance_path, 'ownchatbot.sqlite')
  15. )
  16. app.config.from_object('ownchatbot.defaults.config') # Read from config files
  17. app.config.from_pyfile('config.py', silent=True)
  18. app.config.from_object('ownchatbot.defaults.rewards')
  19. app.config.from_pyfile('rewards.py', silent=True)
  20. app.config.from_object('ownchatbot.defaults.categories')
  21. app.config.from_pyfile('categories.py', silent=True)
  22. if app.config['GUNICORN']: # Gunicorn logging integration
  23. gunicorn_logger = logging.getLogger('gunicorn.error')
  24. app.logger.handlers = gunicorn_logger.handlers
  25. app.logger.setLevel(gunicorn_logger.level)
  26. from . import webhooks # Set up blueprints
  27. from . import web_panels
  28. app.register_blueprint(webhooks.ocb)
  29. app.register_blueprint(web_panels.ocb)
  30. from . import db # Set up db init cli command
  31. db.init_app(app)
  32. def award_job():
  33. with app.app_context():
  34. if live_now():
  35. award_points(get_db())
  36. banker = BackgroundScheduler()
  37. seconds = app.config['POINTS_INTERVAL'] * 60
  38. banker.add_job(award_job, 'interval', seconds=seconds)
  39. banker.start()
  40. return app
  41. if __name__ == '__main__':
  42. create_app()