1
0

globals.py 3.6 KB

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