plex-prerolls/plexutil.py

172 lines
6.0 KiB
Python
Raw Normal View History

2021-01-01 16:12:22 +00:00
#!/usr/bin/python
"""Plex config parsing utilities
Raises:
FileNotFoundError: [description]
KeyError: [description]
"""
import logging
import logging.config
2022-10-09 01:07:14 +00:00
import os
import sys
2021-01-01 16:12:22 +00:00
from configparser import ConfigParser
2022-10-09 01:07:14 +00:00
from typing import Dict, Optional
from plexapi.server import CONFIG # type: ignore
2021-01-01 16:12:22 +00:00
logger = logging.getLogger(__name__)
filename = os.path.basename(sys.argv[0])
SCRIPT_NAME = os.path.splitext(filename)[0]
2022-10-09 01:07:14 +00:00
def plex_config(config_file: Optional[str] = "") -> Dict[str, str]:
2021-01-01 16:12:22 +00:00
"""Return Plex Config paramaters for connection info {PLEX_URL, PLEX_TOKEN}\n
Attempts to use one of either:\n
2021-01-01 18:31:15 +00:00
* supplier path/to/config file (INI Format)
* local config.ini (primary)
* PlexAPI system config.ini (secondary)
Args:
config_file (str): path/to/config.ini style config file (INI Format)
2021-01-01 16:12:22 +00:00
Raises:
KeyError: Config Params not found in config file(s)
FileNotFoundError: Cannot find a config file
2021-01-01 16:12:22 +00:00
Returns:
dict: Dict of config params {PLEX_URL, PLEX_TOKEN}
"""
2022-10-09 01:07:14 +00:00
cfg: dict[str, str] = {}
plex_url = ""
plex_token = ""
filename = ""
2021-01-01 16:12:22 +00:00
use_local_config = False
use_plexapi_config = False
# Look for a local Config.ini file, use settings if present
local_config = ConfigParser()
2022-10-09 01:07:14 +00:00
if config_file == None or config_file == "":
filename = "config.ini"
else:
filename = str(config_file)
2021-01-01 16:12:22 +00:00
2022-10-09 01:07:14 +00:00
# try reading a local file
2021-01-01 16:12:22 +00:00
local_config.read(filename)
2022-10-09 01:07:14 +00:00
if len(local_config.sections()) > 0: # len(found_config) > 0:
2021-01-01 16:12:22 +00:00
# if local config.ini file found, try to use local first
2022-10-09 01:07:14 +00:00
if local_config.has_section("auth"):
try:
server = local_config["auth"]
plex_url = server["server_baseurl"]
plex_token = server["server_token"]
2021-01-01 16:12:22 +00:00
if len(plex_url) > 1 and len(plex_token) > 1:
use_local_config = True
except KeyError as e:
2022-10-09 01:07:14 +00:00
logger.error("Key Value not found", exc_info=e)
2021-01-01 16:12:22 +00:00
raise e
else:
2022-10-09 01:07:14 +00:00
msg = "[auth] section not found in LOCAL config.ini file"
2021-01-01 16:12:22 +00:00
logger.error(msg)
raise KeyError(msg)
2022-10-09 01:07:14 +00:00
if not use_local_config and len(CONFIG.sections()) > 0: # type: ignore
2021-01-01 16:12:22 +00:00
# use PlexAPI Default ~/.config/plexapi/config.ini OR from PLEXAPI_CONFIG_PATH
# IF not manually set locally in local Config.ini above
# See https://python-plexapi.readthedocs.io/en/latest/configuration.html
2022-10-09 01:07:14 +00:00
if CONFIG.has_section("auth"): # type: ignore
2021-01-01 16:12:22 +00:00
try:
2022-10-09 01:07:14 +00:00
server = CONFIG.data["auth"] # type: ignore
plex_url: str = server.get("server_baseurl") # type: ignore
plex_token: str = server.get("server_token") # type: ignore
2021-01-01 16:12:22 +00:00
if len(plex_url) > 1 and len(plex_token) > 1:
use_plexapi_config = True
except KeyError as e:
2022-10-09 01:07:14 +00:00
logger.error("Key Value not found", exc_info=e)
2021-01-01 16:12:22 +00:00
raise e
else:
msg = "[auth] section not found in PlexAPI MAIN config.ini file"
logger.error(msg)
raise KeyError(msg)
if not use_local_config and not use_plexapi_config:
2022-10-09 01:07:14 +00:00
msg = "ConfigFile Error: No Plex config information found [server_baseurl, server_token]"
2021-01-01 16:12:22 +00:00
logger.error(msg)
2022-10-09 01:07:14 +00:00
raise FileNotFoundError(msg)
2021-01-01 16:12:22 +00:00
2022-10-09 01:07:14 +00:00
cfg["PLEX_URL"] = plex_url
cfg["PLEX_TOKEN"] = plex_token
2021-01-01 16:12:22 +00:00
return cfg
2022-10-09 01:07:14 +00:00
def init_logger(log_config: str) -> None:
2021-01-01 18:31:15 +00:00
"""load and configure a program logger using a supplier logging configuration file \n
if possible the program will attempt to create log folders if not already existing
Args:
log_config (str): path/to/logging.(conf|ini) style config file (INI Format)
2021-01-01 18:31:15 +00:00
Raises:
KeyError: Problems processing logging config files
FileNotFoundError: Problems with log file location, other
"""
2021-01-01 18:31:15 +00:00
if os.path.exists(log_config):
try:
logging.config.fileConfig(log_config, disable_existing_loggers=False)
except FileNotFoundError as e_fnf:
# Assume this is related to a missing Log Folder
# Try to create
2022-10-09 01:07:14 +00:00
if e_fnf.filename and e_fnf.filename[-3:] == "log":
2021-01-01 18:31:15 +00:00
logfile = e_fnf.filename
logdir = os.path.dirname(logfile)
if not os.path.exists(logdir):
try:
2022-10-09 01:07:14 +00:00
logger.debug('Creating log folder "%s"', logdir)
2021-01-01 18:31:15 +00:00
os.makedirs(logdir, exist_ok=True)
except Exception as e:
2022-10-09 01:07:14 +00:00
logger.error('Error creating log folder "%s"', logdir, exc_info=e)
2021-01-01 18:31:15 +00:00
raise e
elif logger.handlers:
# if logger config loaded, but some file error happened
for h in logger.handlers:
if isinstance(h, logging.FileHandler):
logfile = h.baseFilename
logdir = os.path.dirname(logfile)
if not os.path.exists(logdir):
try:
2022-10-09 01:07:14 +00:00
logger.debug('Creating log folder "%s"', logdir)
2021-01-01 18:31:15 +00:00
os.makedirs(logdir, exist_ok=True)
except Exception as e:
2022-10-09 01:07:14 +00:00
logger.error('Error creating log folder "%s"', logdir, exc_info=e)
2021-01-01 18:31:15 +00:00
raise e
else:
# not sure the issue, raise the exception
raise e_fnf
# Assuming one of the create Log Folder worked, try again
logging.config.fileConfig(log_config, disable_existing_loggers=False)
else:
2022-10-09 01:07:14 +00:00
logger.debug('Logging Config file "%s" not available, will be using defaults', log_config)
if __name__ == "__main__":
msg = (
"Script not meant to be run directly, please import into other scripts.\n\n"
+ f"usage:\nimport {SCRIPT_NAME}"
+ "\n"
+ f"cfg = {SCRIPT_NAME}.getPlexConfig()"
+ "\n"
)
logger.error(msg)