diff --git a/config.yaml.template b/config.yaml.template index a4228ff..41c16aa 100644 --- a/config.yaml.template +++ b/config.yaml.template @@ -2,6 +2,10 @@ general: # log file directory, optional log_dir: /var/log/wi1-bot + # remote path mappings, optional + remote_path_mappings: + - remote: /data + local: /mnt/plex radarr: # radarr url you use to get to the dashboard @@ -49,7 +53,6 @@ pushover: transcoding: # -hwaccel in ffmpeg, optional hwaccel: cuda - profiles: # name of profile must match name in radarr/sonarr good: diff --git a/wi1_bot/config.py b/wi1_bot/config.py index c73cfc6..3d38cb0 100644 --- a/wi1_bot/config.py +++ b/wi1_bot/config.py @@ -99,8 +99,14 @@ class TranscodingConfig(TranscodingConfigOptional): profiles: dict[str, TranscodingProfile] +class RemotePathMapping(TypedDict): + remote: str + local: str + + class GeneralConfigOptional(TypedDict, total=False): log_dir: str + remote_path_mappings: list[RemotePathMapping] class GeneralConfig(GeneralConfigOptional): diff --git a/wi1_bot/webhook.py b/wi1_bot/webhook.py index 10e6f31..1b4153d 100644 --- a/wi1_bot/webhook.py +++ b/wi1_bot/webhook.py @@ -8,7 +8,7 @@ from wi1_bot import push, transcoder from wi1_bot.arr import Radarr, Sonarr -from wi1_bot.config import config +from wi1_bot.config import RemotePathMapping, config app = Flask(__name__) @@ -26,6 +26,37 @@ def on_grab(req: dict[str, Any]) -> None: ) +def replace_remote_paths(path: pathlib.Path) -> pathlib.Path: + if "general" not in config or "remote_path_mappings" not in config["general"]: + return path + + mappings = config["general"]["remote_path_mappings"] + + most_specific: RemotePathMapping | None = None + + for mapping in mappings: + if path.is_relative_to(mapping["remote"]): + mapping_len = len(pathlib.Path(mapping["remote"]).parts) + most_specific_len = ( + len(pathlib.Path(most_specific["remote"]).parts) + if most_specific is not None + else 0 + ) + + if mapping_len > most_specific_len: + most_specific = mapping + + if most_specific is not None: + remote_path = path + path = pathlib.Path(most_specific["local"]) / path.relative_to( + most_specific["remote"] + ) + + logger.debug(f"replaced remote path mapping: {remote_path} -> {path}") + + return path + + def on_download(req: dict[str, Any]) -> None: path: pathlib.Path content_id: int @@ -65,6 +96,8 @@ def on_download(req: dict[str, Any]) -> None: else: raise ValueError("unknown download request") + path = replace_remote_paths(path) + try: quality_options = config["transcoding"]["profiles"][quality_profile] except KeyError: @@ -119,10 +152,10 @@ def index() -> Any: def start() -> None: logger.info("starting webhook listener") - t = threading.Thread(target=app.run, kwargs={"host": "localhost", "port": 9000}) + t = threading.Thread(target=app.run, kwargs={"host": "0.0.0.0", "port": 9000}) t.daemon = True t.start() if __name__ == "__main__": - app.run(host="localhost", port=9000) + app.run(host="0.0.0.0", port=9000)