Skip to content

Commit

Permalink
Support remote path mappings
Browse files Browse the repository at this point in the history
  • Loading branch information
wthueb committed Apr 6, 2024
1 parent 0c97490 commit 85846da
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
5 changes: 4 additions & 1 deletion config.yaml.template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -49,7 +53,6 @@ pushover:
transcoding:
# -hwaccel in ffmpeg, optional
hwaccel: cuda

profiles:
# name of profile must match name in radarr/sonarr
good:
Expand Down
6 changes: 6 additions & 0 deletions wi1_bot/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
39 changes: 36 additions & 3 deletions wi1_bot/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__)

Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)

0 comments on commit 85846da

Please sign in to comment.