globals.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import logging
  2. import os
  3. import socket
  4. from configparser import ConfigParser
  5. import discord
  6. import pytz
  7. import feedparser
  8. import mariadb
  9. import psycopg2
  10. import pytz
  11. class ImproperlyConfigured(Exception): pass
  12. BASE_DIR = os.path.dirname(os.path.abspath(__file__))
  13. HOME_DIR = os.path.expanduser("~")
  14. DEFAULT_CONFIG_PATHS = [
  15. os.path.join("myanimebot.conf"),
  16. os.path.join(BASE_DIR, "myanimebot.conf"),
  17. os.path.join("/etc/myanimebot/myanimebot.conf"),
  18. os.path.join(HOME_DIR, "myanimebot.conf")
  19. ]
  20. def get_config():
  21. config = ConfigParser()
  22. config_paths = []
  23. for path in DEFAULT_CONFIG_PATHS:
  24. if os.path.isfile(path):
  25. config_paths.append(path)
  26. break
  27. else: raise ImproperlyConfigured("No configuration file found")
  28. config.read(config_paths)
  29. return config
  30. # Loading configuration
  31. try:
  32. config=get_config()
  33. except Exception as e:
  34. print ("Cannot read configuration: " + str(e))
  35. exit (1)
  36. CONFIG=config["MYANIMEBOT"]
  37. logLevel=CONFIG.get("logLevel", "INFO")
  38. dbType=CONFIG.get("database.type", "mariadb")
  39. dbMariaHost=CONFIG.get("mariadb.host", "127.0.0.1")
  40. dbMariaPort=CONFIG.get("mariadb.port", "3306")
  41. dbMariaUser=CONFIG.get("mariadb.user", "myanimebot")
  42. dbMariaPassword=CONFIG.get("mariadb.password")
  43. dbMariaName=CONFIG.get("mariadb.name", "myanimebot")
  44. dbMariaSSLenabled=CONFIG.getboolean("mariadb.ssl", False)
  45. dbMariaSSLca=CONFIG.get("mariadb.ssl.ca")
  46. dbMariaSSLcert=CONFIG.get("mariadb.ssl.cert")
  47. dbMariaSSLkey=CONFIG.get("mariadb.ssl.key")
  48. dbPgHost=CONFIG.get("postgresql.host", "127.0.0.1")
  49. dbPgPort=CONFIG.get("postgresql.port", "5432")
  50. dbPgUser=CONFIG.get("postgresql.user", "myanimebot")
  51. dbPgPassword=CONFIG.get("postgresql.password")
  52. dbPgName=CONFIG.get("postgresql.name", "myanimebot")
  53. logPath=CONFIG.get("logPath", "myanimebot.log")
  54. timezone=pytz.timezone(CONFIG.get("timezone", "utc"))
  55. secondMax=CONFIG.getint("secondMax", 7200)
  56. token=CONFIG.get("token")
  57. prefix=CONFIG.get("prefix", "!mab")
  58. MYANIMELIST_SECONDS_BETWEEN_REQUESTS=CONFIG.getint("myanimelist_seconds_between_requests", 2)
  59. iconBot=CONFIG.get("iconBot", "http://myanimebot.pentou.eu/rsc/bot_avatar.jpg")
  60. ANILIST_SECONDS_BETWEEN_FETCHES=CONFIG.getint("anilist_seconds_between_fetches", 60)
  61. MAL_ICON_URL=CONFIG.get("iconMAL", "https://cdn.myanimelist.net/img/sp/icon/apple-touch-icon-256.png")
  62. ANILIST_ICON_URL=CONFIG.get("iconAniList", "https://anilist.co/img/icons/android-chrome-512x512.png")
  63. SERVICE_ANILIST="ani"
  64. SERVICE_MAL="mal"
  65. MAL_URL="https://myanimelist.net/"
  66. MAL_PROFILE_URL="https://myanimelist.net/profile/"
  67. ANILIST_PROFILE_URL="https://anilist.co/user/"
  68. DB_USER_NAME="mal_user" # Column's name for usernames in the t_users table
  69. MAL_ENABLED=CONFIG.getboolean("mal_enabled", True)
  70. ANI_ENABLED=CONFIG.getboolean("ani_enabled", True)
  71. HEALTHCHECK_ENABLED=CONFIG.getboolean("healthcheck_enabled", False)
  72. HEALTHCHECK_PORT=CONFIG.getint("healthcheck_port", 15200)
  73. HEALTHCHECK_IP=CONFIG.get("healthcheck_ip", "0.0.0.0")
  74. # Log configuration
  75. log_format='%(asctime)-13s : %(name)-15s : %(levelname)-8s : %(message)s'
  76. logging.basicConfig(handlers=[logging.FileHandler(logPath, 'a', 'utf-8')], format=log_format, level=logLevel)
  77. console = logging.StreamHandler()
  78. console.setLevel(logging.INFO)
  79. console.setFormatter(logging.Formatter(log_format))
  80. logger = logging.getLogger("myanimebot")
  81. logger.setLevel(logLevel)
  82. logging.getLogger('').addHandler(console)
  83. # Script version
  84. VERSION = "1.0.0a"
  85. logger.info("Booting MyAnimeBot " + VERSION + "...")
  86. logger.debug("DEBUG log: OK")
  87. # feedparser.PREFERRED_XML_PARSERS.remove("drv_libxml2")
  88. # Initialization of the database
  89. try:
  90. # Main database connection
  91. if (dbType.lower() == "mariadb") or (dbType.lower() == "mysql") :
  92. if (dbSSLenabled) :
  93. conn = mariadb.connect(host=dbMariaHost, user=dbMariaUser, password=dbMariaPassword, database=dbMariaName, port=dbMariaPort, ssl_ca=dbMariaSSLca, ssl_cert=dbMariaSSLcert, ssl_key=dbMariaSSLkey)
  94. else :
  95. conn = mariadb.connect(host=dbMariaHost, user=dbMariaUser, password=dbMariaPassword, database=dbMariaName)
  96. elif (dbType.lower() == "postgresql") or (dbType.lower() == "pgsql") or (dbType.lower() == "posgres") :
  97. conn = psycopg2.connect(host=dbPgHost, user=dbPgUser, password=dbPgPassword, database=dbPgName, port=dbPgPort)
  98. else:
  99. logger.critical("'{}' is not a supported database type!".format(dbType))
  100. quit()
  101. except Exception as e:
  102. logger.critical("Can't connect to the database: {}".format(e))
  103. quit()
  104. # Initialization of the Discord client
  105. client = None
  106. task_feed = None
  107. task_feed_anilist = None
  108. task_gameplayed = None
  109. task_thumbnail = None