__init__.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Init"""
  2. import os
  3. import logging
  4. import telegram
  5. from sqlalchemy import create_engine
  6. from sqlalchemy.orm import sessionmaker
  7. from dotenv import load_dotenv
  8. from apscheduler.schedulers.background import BackgroundScheduler
  9. from app.models import Base, Player, State, Department, DepartmentStat
  10. load_dotenv()
  11. # Telegram
  12. TELEGRAM_BOT = telegram.Bot(os.environ['TELEGRAM_KEY'])
  13. # database
  14. ENGINE = create_engine(os.environ["DATABASE_URI"], client_encoding='utf8')
  15. SESSION = sessionmaker(bind=ENGINE)
  16. # scheduler
  17. SCHEDULER = BackgroundScheduler(
  18. daemon=True,
  19. job_defaults={'misfire_grace_time': 10*60},
  20. )
  21. SCHEDULER.start()
  22. # get logger
  23. LOGGER = logging.getLogger(__name__)
  24. LOGGER.setLevel(logging.INFO)
  25. SCHEDULER_LOGGER = logging.getLogger('apscheduler')
  26. SCHEDULER_LOGGER.setLevel(logging.DEBUG)
  27. # create file handler
  28. FILE_HANDLER = logging.FileHandler('output.log')
  29. FILE_HANDLER.setLevel(logging.DEBUG)
  30. # create console handler
  31. STREAM_HANDLER = logging.StreamHandler()
  32. STREAM_HANDLER.setLevel(logging.INFO)
  33. # create formatter and add it to the handlers
  34. FORMATTER = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
  35. STREAM_HANDLER.setFormatter(FORMATTER)
  36. FILE_HANDLER.setFormatter(FORMATTER)
  37. # add the handlers to logger
  38. LOGGER.addHandler(STREAM_HANDLER)
  39. LOGGER.addHandler(FILE_HANDLER)
  40. SCHEDULER_LOGGER.addHandler(STREAM_HANDLER)
  41. SCHEDULER_LOGGER.addHandler(FILE_HANDLER)
  42. # api
  43. BASE_URL = os.environ["API_URL"]
  44. HEADERS = {
  45. 'Authorization': os.environ["AUTHORIZATION"]
  46. }