__init__.py 3.2 KB

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