feat(qbittorrent_api): add path mappings for docker container
This commit is contained in:
1 parent
501caee8b9
commit
1ad39ee042
2 files changed
+43
-5
No files matched your search
@@ -1,6 +1,7 @@
|
||||
import argparse
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from serverlibrary import parser_add_loglevel, setup_logging
|
||||
@@ -26,6 +27,9 @@ def main():
|
||||
args = parser.parse_args()
|
||||
setup_logging(args.log_level)
|
||||
logger.info("Starting qBittorrent CLI")
|
||||
logger.info("Working directory: %s", Path.cwd())
|
||||
entry_point = getattr(sys.modules["__main__"], "__file__", sys.argv[0])
|
||||
logger.info("Execution directory: %s", Path(entry_point).resolve().parent)
|
||||
|
||||
if args.copy_to is not None and not args.id:
|
||||
parser.error("--copy-to requires --id")
|
||||
@@ -36,8 +40,23 @@ def main():
|
||||
logger.error("Required qBittorrent API keys are not set")
|
||||
parser.error("Set QBITTORRENT_PRIVATE_API_KEY and QBITTORRENT_PUBLIC_API_KEY")
|
||||
|
||||
private = Qbittorrent("192.168.0.2:8080", "PRIVATE", private_key)
|
||||
public = Qbittorrent("192.168.0.2:8082", "PUBLIC", public_key)
|
||||
private = Qbittorrent(
|
||||
"192.168.0.2:8080", "PRIVATE", private_key,
|
||||
path_mappings={
|
||||
"/downloads-private": "/torrent/qbittorrent-private/downloads",
|
||||
"/torrents": "/torrent/qbittorrent-private/torrents",
|
||||
"/mediahost": "/torrent/qbittorrent/mediahost",
|
||||
},
|
||||
)
|
||||
public = Qbittorrent(
|
||||
"192.168.0.2:8082", "PUBLIC", public_key,
|
||||
path_mappings={
|
||||
"/downloads-public": "/torrent/qbittorrent-public/downloads",
|
||||
"/downloads-special": "/special-torrent/torrent",
|
||||
"/torrents": "/torrent/qbittorrent-public/torrents",
|
||||
"/mediahost": "/torrent/qbittorrent/mediahost",
|
||||
},
|
||||
)
|
||||
|
||||
private.show_torrents(sort_by=args.sort, reverse=not args.asc, id=args.show_id)
|
||||
public.show_torrents(sort_by=args.sort, reverse=not args.asc, id=args.show_id)
|
||||
|
||||
@@ -2,7 +2,7 @@ import logging
|
||||
import math
|
||||
import shutil
|
||||
import socket
|
||||
from pathlib import Path
|
||||
from pathlib import Path, PurePosixPath
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
import requests
|
||||
@@ -28,12 +28,30 @@ def _is_local_host(host):
|
||||
|
||||
|
||||
class Qbittorrent:
|
||||
def __init__(self, ip_adress, name, api_key) -> None:
|
||||
def __init__(self, ip_adress, name, api_key, path_mappings=None) -> None:
|
||||
self.ip_adress = ip_adress
|
||||
self.name = name
|
||||
self.api_key = api_key
|
||||
self.path_mappings = path_mappings or {}
|
||||
logger.debug("Configured qBittorrent instance %s at %s", name, ip_adress)
|
||||
|
||||
def _host_save_path(self, save_path):
|
||||
container_path = PurePosixPath(save_path)
|
||||
if ".." in container_path.parts:
|
||||
raise ValueError(f"Invalid torrent save path: {save_path!r}")
|
||||
|
||||
mappings = sorted(
|
||||
self.path_mappings.items(),
|
||||
key=lambda mapping: len(PurePosixPath(mapping[0]).parts),
|
||||
reverse=True,
|
||||
)
|
||||
for container_root, host_root in mappings:
|
||||
container_root = PurePosixPath(container_root)
|
||||
if container_path.is_relative_to(container_root):
|
||||
relative = container_path.relative_to(container_root)
|
||||
return Path(host_root).joinpath(*relative.parts)
|
||||
return Path(save_path)
|
||||
|
||||
def get_torrents(self):
|
||||
logger.debug("Fetching torrents from %s", self.name)
|
||||
response = requests.get(
|
||||
@@ -135,6 +153,7 @@ class Qbittorrent:
|
||||
if save_path is None:
|
||||
raise ValueError(f"Torrent {identifier!r} has no save path")
|
||||
|
||||
source_directory = self._host_save_path(save_path)
|
||||
host = urlsplit(f"//{self.ip_adress}").hostname
|
||||
if not dry_run and (host is None or not _is_local_host(host)):
|
||||
logger.error(
|
||||
@@ -152,7 +171,7 @@ class Qbittorrent:
|
||||
if relative_path.is_absolute() or ".." in relative_path.parts:
|
||||
raise ValueError(f"Invalid torrent file path: {file['name']!r}")
|
||||
target = destination / relative_path
|
||||
source = Path(save_path) / relative_path
|
||||
source = source_directory / relative_path
|
||||
if dry_run:
|
||||
logger.debug("Dry run: would copy %s to %s", source, target)
|
||||
else:
|
||||
|
||||
Reference in new issue
Block a user