1
0

__init__.py 3.5 KB

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