__init__.py 3.3 KB

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