__init__.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import os
  2. import logging
  3. from flask import Flask, g
  4. from flask_cors import CORS
  5. from ownchatbot.db import get_db
  6. from ownchatbot.owncast_com import live_now, award_points, send_system_chat
  7. from apscheduler.schedulers.background import BackgroundScheduler
  8. current_index = 0
  9. def create_app(test_config=None):
  10. app = Flask(__name__, instance_relative_config=True)
  11. CORS(app) # Enable CORS for all routes
  12. try:
  13. os.makedirs(app.instance_path)
  14. except OSError:
  15. pass
  16. app.config.from_mapping(
  17. DATABASE=os.path.join(app.instance_path, 'ownchatbot.sqlite')
  18. )
  19. app.config.from_object('ownchatbot.defaults.config') # Read from config files
  20. app.config.from_pyfile('config.py', silent=True)
  21. app.config.from_object('ownchatbot.defaults.rewards')
  22. app.config.from_pyfile('rewards.py', silent=True)
  23. app.config.from_object('ownchatbot.defaults.categories')
  24. app.config.from_pyfile('categories.py', silent=True)
  25. app.config.from_object('ownchatbot.defaults.kofi')
  26. app.config.from_pyfile('kofi.py', silent=True)
  27. app.config.from_object('ownchatbot.defaults.announce')
  28. app.config.from_pyfile('announce.py', silent=True)
  29. app.config.from_object('ownchatbot.defaults.todo')
  30. app.config.from_pyfile('todo.py', silent=True)
  31. app.config.from_object('ownchatbot.defaults.alerts')
  32. app.config.from_pyfile('alerts.py', silent=True)
  33. app.config['ALERTS_FOLDER'] = os.path.join(app.instance_path, 'alerts')
  34. if app.config['GUNICORN']: # Gunicorn logging integration
  35. gunicorn_logger = logging.getLogger('gunicorn.error')
  36. app.logger.handlers = gunicorn_logger.handlers
  37. app.logger.setLevel(gunicorn_logger.level)
  38. from . import webhooks # Set up blueprints
  39. from . import web_panels
  40. app.register_blueprint(webhooks.ocb)
  41. app.register_blueprint(web_panels.ocb)
  42. from . import db # Set up cli commands
  43. db.init_app(app)
  44. def announce():
  45. global current_index
  46. app.config.from_pyfile('announce.py', silent=True)
  47. announcements = app.config['ANNOUNCEMENTS']
  48. if current_index >= len(announcements): # If reached the last announement, reset index
  49. current_index = 0
  50. try:
  51. message = announcements[current_index]
  52. send_system_chat(message)
  53. current_index = (current_index + 1) % len(announcements)
  54. except Exception as a_error:
  55. app.logger.error(f'Couldn\'t make announcement: {a_error.args[0]}')
  56. def award_job():
  57. with app.app_context():
  58. if live_now(): # If stream is live
  59. award_points(get_db())
  60. def announce_job():
  61. with app.app_context():
  62. if app.config['ANNOUNCE_ENABLE']: # If announcements are enabled
  63. if live_now(): # If stream is live
  64. announce()
  65. else:
  66. app.logger.debug(f'Not live, so not sending announcement.')
  67. else:
  68. app.logger.debug(f'Announcements not enabled.')
  69. jorel_master_of_scheduling = BackgroundScheduler()
  70. points_seconds = app.config['POINTS_INTERVAL'] * 60
  71. announce_seconds = app.config['ANNOUNCE_INTERVAL'] * 60
  72. jorel_master_of_scheduling.add_job(award_job, 'interval', seconds=points_seconds)
  73. jorel_master_of_scheduling.add_job(announce_job, 'interval', seconds=announce_seconds)
  74. jorel_master_of_scheduling.start()
  75. return app
  76. if __name__ == '__main__':
  77. create_app()