From fe375434dc1822d9975a46364fd4a2a971aaabd3 Mon Sep 17 00:00:00 2001 From: Riley Martine Date: Sat, 13 May 2023 12:32:39 -0600 Subject: [PATCH] Make rclone only download specified torrent instead of all of em --- .gitignore | 1 + scripts/get_download_target.py | 39 ++++++++++++++++++++++++++++++++++ scripts/wait_and_download.sh | 8 +++++-- 3 files changed, 46 insertions(+), 2 deletions(-) create mode 100755 scripts/get_download_target.py diff --git a/.gitignore b/.gitignore index 2dfe8ec..f5963ea 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .prev_kindle_ip +/scripts/testtorrents diff --git a/scripts/get_download_target.py b/scripts/get_download_target.py new file mode 100755 index 0000000..b94f6e4 --- /dev/null +++ b/scripts/get_download_target.py @@ -0,0 +1,39 @@ +#!/opt/homebrew/bin/python3.11 +"""Get the top-level directory or file inside the torrent to look for to download.""" + +# Needs to be brew python3.11 for libtorrent bindings + +import pathlib +import sys +import warnings + +import libtorrent + +# libtorrent uses dunder methods for some things and we want clean output +warnings.filterwarnings("ignore", category=DeprecationWarning) + +if len(sys.argv) != 2: + print("Pass an argument.", file=sys.stderr) + sys.exit(1) + +torrent_file = pathlib.Path(sys.argv[1]) +if not torrent_file.is_file(): + print(f"Cannot find file at: {torrent_file}", file=sys.stderr) + sys.exit(1) + +info = libtorrent.torrent_info(torrent_file.resolve().as_posix()) +paths = [file.path for file in info.files()] + +# Assume everything either has one dir containing everything, +# or one file w/o a dir + +chunked_paths = [pathlib.Path(p).parts for p in paths] +if len(["x" for parts in chunked_paths if not len(parts) > 1]) > 1: + print(chunked_paths, file=sys.stderr) + raise NotImplementedError("Expected at most one top level file.") + +if len({len(parts) > 1 for parts in chunked_paths}) != 1: + print(chunked_paths, file=sys.stderr) + raise NotImplementedError("Both a top-level directory and file exist.") + +print(chunked_paths[0][0]) diff --git a/scripts/wait_and_download.sh b/scripts/wait_and_download.sh index e3c3617..3a3c45c 100755 --- a/scripts/wait_and_download.sh +++ b/scripts/wait_and_download.sh @@ -38,9 +38,13 @@ echo "Waiting for $TORRENT_NAME to finish downloading to seedbox..." retry 240 is_complete "$TORRENT_NAME" echo "$TORRENT_NAME has completed download to seedbox. Downloading new torrents to local..." +DOWNLOAD_TARGET="$(./get_download_target.py "$1")" +echo "Grabbing $DOWNLOAD_TARGET" + rclone --config "$RCLONE_CONF" --max-age 24h --no-traverse copy \ - seedbox:/home/"$SEEDBOX_USER"/twatch_out/ ~/Downloads -echo "Done downloading new torrents." + seedbox:/home/"$SEEDBOX_USER"/twatch_out/"$DOWNLOAD_TARGET" \ + ~/Downloads/"$DOWNLOAD_TARGET" +echo "Done downloading torrent." function send_epubs { set -x