__init__.py 3.5 KB

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