plex-prerolls/modules/plex_connector.py
nwithan8 1616d91ebe - Rewrite nearly the whole thing for better parsing, support, readability, etc. (OOP forever!)
- Consolidate Plex API config with schedules, now in config.yaml
- Changes to schema for schedules
- No more priority of, e.g. date_range over monthly
- No more default
- `misc` reworked to `always`
- Update Docker files
- Update README
- Delete old files, linting stuff (screw mypy)
2023-12-08 17:50:12 -07:00

46 lines
1.3 KiB
Python

from typing import List, Union
from plexapi.server import PlexServer
import modules.logs as logging
def prepare_pre_roll_string(paths: List[str]) -> Union[str, None]:
if not paths:
return None
# Filter out empty paths
paths = [path for path in paths if path]
return ";".join(paths)
class PlexConnector:
def __init__(self, host: str, token: str):
self._host = host
self._token = token
logging.info(f"Connecting to Plex server at {self._host}")
self._plex_server = PlexServer(baseurl=self._host, token=self._token)
def update_pre_roll_paths(self, paths: List[str], testing: bool = False) -> None:
pre_roll_string = prepare_pre_roll_string(paths=paths)
if not pre_roll_string:
logging.info("No pre-roll paths to update")
return
if testing:
logging.debug(f"Testing: Would have updated pre-roll to: {pre_roll_string}")
return
logging.info(f"Updating pre-roll to: {pre_roll_string}")
self._plex_server.settings.get("cinemaTrailersPrerollID").set(pre_roll_string) # type: ignore
try:
self._plex_server.settings.save() # type: ignore
except Exception as e:
logging.error(f"Failed to save pre-roll: {e}")
return
logging.info("Successfully updated pre-roll")