__init__.py 1.5 KB

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