Files
Membarr/app/bot/helper/confighelper.py

199 lines
6.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import configparser
import os
from os import environ, path
from dotenv import load_dotenv
CONFIG_PATH = 'app/config/config.ini'
BOT_SECTION = 'bot_envs'
MEMBARR_VERSION = 1.1
config = configparser.ConfigParser()
CONFIG_KEYS = ['username', 'password', 'discord_bot_token', 'plex_user', 'plex_pass', 'plex_token',
'plex_base_url', 'plex_roles', 'plex_server_name', 'plex_libs', 'owner_id', 'channel_id',
'auto_remove_user', 'jellyfin_api_key', 'jellyfin_server_url', 'jellyfin_roles',
'jellyfin_libs', 'plex_enabled', 'jellyfin_enabled', 'jellyfin_external_url', 'discord_perm']
# settings
Discord_bot_token = ""
plex_roles = None
PLEXUSER = ""
PLEXPASS = ""
PLEX_SERVER_NAME = ""
PLEX_TOKEN = ""
PLEX_BASE_URL = ""
Plex_LIBS = None
JELLYFIN_SERVER_URL = ""
JELLYFIN_API_KEY = ""
jellyfin_libs = ""
jellyfin_roles = None
plex_configured = True
jellyfin_configured = True
DISCORD_SERVER_PERM = ""
switch = 0
# TODO: make this into a class
if(path.exists('bot.env')):
try:
load_dotenv(dotenv_path='bot.env')
# settings
Discord_bot_token = environ.get('discord_bot_token')
switch = 1
except Exception as e:
pass
try:
Discord_bot_token = str(os.environ['token'])
switch = 1
except Exception as e:
pass
if not (path.exists(CONFIG_PATH)):
with open (CONFIG_PATH, 'w') as fp:
pass
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
plex_token_configured = True
try:
PLEX_TOKEN = config.get(BOT_SECTION, 'plex_token')
PLEX_BASE_URL = config.get(BOT_SECTION, 'plex_base_url')
except:
print("Сведения о токене аутентификации Plex не найдены.")
plex_token_configured = False
# Get Plex config
try:
PLEX_SERVER_NAME = config.get(BOT_SECTION, 'plex_server_name')
PLEXUSER = config.get(BOT_SECTION, 'plex_user')
PLEXPASS = config.get(BOT_SECTION, 'plex_pass')
except:
print("Информация для входа в Plex не найдена")
if not plex_token_configured:
print("Не удалось загрузить конфигурацию plex")
plex_configured = False
# Get Plex roles config
try:
plex_roles = config.get(BOT_SECTION, 'plex_roles')
except:
print("Не удалось получить конфигурацию ролей Plex.")
plex_roles = None
if plex_roles:
plex_roles = list(plex_roles.split(','))
else:
plex_roles = []
# Get Plex libs config
try:
Plex_LIBS = config.get(BOT_SECTION, 'plex_libs')
except:
print("Не удалось получить конфигурацию Plex libs. По умолчанию для всех библиотек.")
Plex_LIBS = None
if Plex_LIBS is None:
Plex_LIBS = ["all"]
else:
Plex_LIBS = list(Plex_LIBS.split(','))
# Get Jellyfin config
try:
JELLYFIN_SERVER_URL = config.get(BOT_SECTION, 'jellyfin_server_url')
JELLYFIN_API_KEY = config.get(BOT_SECTION, "jellyfin_api_key")
except:
print("Не удалось загрузить конфигурацию Jellyfin.")
jellyfin_configured = False
try:
JELLYFIN_EXTERNAL_URL = config.get(BOT_SECTION, "jellyfin_external_url")
if not JELLYFIN_EXTERNAL_URL:
JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL
except:
JELLYFIN_EXTERNAL_URL = JELLYFIN_SERVER_URL
print("Не удалось получить внешний URL-адрес Jellyfin. По умолчанию используется URL-адрес сервера.")
# Get Jellyfin roles config
try:
jellyfin_roles = config.get(BOT_SECTION, 'jellyfin_roles')
except:
print("Не удалось получить конфигурацию ролей Jellyfin.")
jellyfin_roles = None
if jellyfin_roles:
jellyfin_roles = list(jellyfin_roles.split(','))
else:
jellyfin_roles = []
# Get Jellyfin libs config
try:
jellyfin_libs = config.get(BOT_SECTION, 'jellyfin_libs')
except:
print("Не удалось получить конфигурацию Jellyfin libs. По умолчанию для всех библиотек.")
jellyfin_libs = None
if jellyfin_libs is None:
jellyfin_libs = ["all"]
else:
jellyfin_libs = list(jellyfin_libs.split(','))
# Get Enable config
try:
USE_JELLYFIN = config.get(BOT_SECTION, 'jellyfin_enabled')
USE_JELLYFIN = USE_JELLYFIN.lower() == "true"
except:
print("Не удалось получить конфигурацию включения Jellyfin. По умолчанию установлено значение False")
USE_JELLYFIN = False
try:
USE_PLEX = config.get(BOT_SECTION, "plex_enabled")
USE_PLEX = USE_PLEX.lower() == "true"
except:
print("Не удалось получить конфигурацию включения Plex. По умолчанию установлено значение False")
USE_PLEX = False
try:
DISCORD_SERVER_PERM = config.get(BOT_SECTION, "discord_perm")
except:
DISCORD_SERVER_PERM = None
print("Не удалось получить id роли для управления ботом. Возможна любая дичь.")
def get_config():
"""
Function to return current config
"""
try:
config.read(CONFIG_PATH)
return config
except Exception as e:
print(e)
print('ошибка чтения конфига')
return None
def change_config(key, value):
"""
Function to change the key, value pair in config
"""
try:
config = configparser.ConfigParser()
config.read(CONFIG_PATH)
except Exception as e:
print(e)
print("Невозможно прочитать конфигурацию.")
try:
config.set(BOT_SECTION, key, str(value))
except Exception as e:
config.add_section(BOT_SECTION)
config.set(BOT_SECTION, key, str(value))
try:
with open(CONFIG_PATH, 'w') as configfile:
config.write(configfile)
except Exception as e:
print(e)
print("Не могу записать в конфиг.")