__init__.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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['ASSETS_FOLDER'] = os.path.join(app.instance_path, 'assets')
  34. gunicorn_logger = logging.getLogger('gunicorn.error')
  35. app.logger.handlers = gunicorn_logger.handlers
  36. app.logger.setLevel(gunicorn_logger.level)
  37. from . import webhooks # Set up blueprints
  38. from . import web_panels
  39. app.register_blueprint(webhooks.ocb)
  40. app.register_blueprint(web_panels.ocb)
  41. from . import db # Set up cli commands
  42. db.init_app(app)
  43. def announce():
  44. global current_index
  45. app.config.from_pyfile('announce.py', silent=True)
  46. announcements = app.config['ANNOUNCEMENTS']
  47. if current_index >= len(announcements): # If reached the last announement, reset index
  48. current_index = 0
  49. try:
  50. message = announcements[current_index]
  51. send_system_chat(message)
  52. current_index = (current_index + 1) % len(announcements)
  53. except Exception as a_error:
  54. app.logger.error(f'Couldn\'t make announcement: {a_error.args[0]}')
  55. def award_job():
  56. with app.app_context():
  57. if live_now(): # If stream is live
  58. award_points(get_db())
  59. def announce_job():
  60. with app.app_context():
  61. if app.config['ANNOUNCE_ENABLE']: # If announcements are enabled
  62. if live_now(): # If stream is live
  63. announce()
  64. else:
  65. app.logger.debug(f'Not live, so not sending announcement.')
  66. else:
  67. app.logger.debug(f'Announcements not enabled.')
  68. jorel_master_of_scheduling = BackgroundScheduler()
  69. points_seconds = app.config['POINTS_INTERVAL'] * 60
  70. announce_seconds = app.config['ANNOUNCE_INTERVAL'] * 60
  71. jorel_master_of_scheduling.add_job(award_job, 'interval', seconds=points_seconds)
  72. jorel_master_of_scheduling.add_job(announce_job, 'interval', seconds=announce_seconds)
  73. jorel_master_of_scheduling.start()
  74. return app
  75. if __name__ == '__main__':
  76. create_app()