From afe27fd9cb78db8879146f3c8c00f90deaa6c121 Mon Sep 17 00:00:00 2001 From: Colin Date: Thu, 22 Jun 2023 23:48:00 +0000 Subject: [PATCH] sane-bt-add: support moving torrents to new directories --- .../sane-scripts/src/lib/bt/sane_bt.py | 56 ++++++++++++++----- pkgs/additional/sane-scripts/src/sane-bt-add | 6 +- pkgs/additional/sane-scripts/src/sane-bt-rm | 4 ++ pkgs/additional/sane-scripts/src/sane-bt-show | 4 ++ 4 files changed, 55 insertions(+), 15 deletions(-) diff --git a/pkgs/additional/sane-scripts/src/lib/bt/sane_bt.py b/pkgs/additional/sane-scripts/src/lib/bt/sane_bt.py index e5d57afc..a6052992 100644 --- a/pkgs/additional/sane-scripts/src/lib/bt/sane_bt.py +++ b/pkgs/additional/sane-scripts/src/lib/bt/sane_bt.py @@ -3,9 +3,12 @@ from __future__ import annotations from argparse import ArgumentParser, Namespace from dataclasses import dataclass +import logging import os.path import subprocess +logger = logging.getLogger(__name__) + @dataclass class MediaMeta: title: str | None @@ -87,19 +90,41 @@ class TransmissionApi: return open(self.PASSFILE, "r").read().strip() def add_torrent(self, meta: MediaMeta, torrent: str): - print(f"saving to {meta.fs_path()}") - self.call_transmission([ - "--download-dir", meta.fs_path(), - "--add", torrent, - ]) + self.call_transmission( + description=f"saving to {meta.fs_path()}", + args=[ + "--download-dir", meta.fs_path(), + "--add", torrent, + ] + ) + + def move_torrent(self, meta: MediaMeta, torrent: str): + self.call_transmission( + description=f"moving {torrent} to {meta.fs_path()}", + args=[ + "--torrent", torrent, + "--move", meta.fs_path() + ] + ) + + def add_or_move_torrent(self, meta: MediaMeta, torrent: str): + """ + if "torrent" represents a magnet or file URI, then add else + else assume it's a transmission identifier and move it to the location specified by @p meta. + """ + if torrent.isdigit(): + self.move_torrent(meta, torrent) + else: + self.add_torrent(meta, torrent) def rm_torrent(self, torrent: str | int): - print(f"deleting {torrent}") - self.call_transmission([ - "--torrent", torrent, - "--remove-and-delete" - ]) - + self.call_transmission( + description=f"deleting {torrent}", + args=[ + "--torrent", torrent, + "--remove-and-delete" + ] + ) def list_(self) -> str: return self.call_transmission([ @@ -108,11 +133,14 @@ class TransmissionApi: def info(self, torrent: str) -> str: return self.call_transmission([ - "-t", torrent, - "-i" + "--torrent", torrent, + "--info" ]) - def call_transmission(self, args: list[str]) -> str: + def call_transmission(self, args: list[str], description: str | None = None) -> str: + if description is not None: + logger.info(description) + return self.check_output([ "transmission-remote", self.ENDPOINT, diff --git a/pkgs/additional/sane-scripts/src/sane-bt-add b/pkgs/additional/sane-scripts/src/sane-bt-add index b0936b26..d64651f6 100755 --- a/pkgs/additional/sane-scripts/src/sane-bt-add +++ b/pkgs/additional/sane-scripts/src/sane-bt-add @@ -3,11 +3,15 @@ # vim: set filetype=python : import argparse +import logging from sane_bt import MediaMeta, TransmissionApi def main(): + logging.basicConfig() + logging.getLogger().setLevel(logging.INFO) + parser = argparse.ArgumentParser(description="instruct servo to download some torrent") TransmissionApi.add_arguments(parser) MediaMeta.add_arguments(parser) @@ -18,7 +22,7 @@ def main(): bt_api = TransmissionApi.from_arguments(args) torrent = args.torrent - bt_api.add_torrent(meta, torrent) + bt_api.add_or_move_torrent(meta, torrent) if __name__ == "__main__": main() diff --git a/pkgs/additional/sane-scripts/src/sane-bt-rm b/pkgs/additional/sane-scripts/src/sane-bt-rm index bb5dca29..09c31142 100755 --- a/pkgs/additional/sane-scripts/src/sane-bt-rm +++ b/pkgs/additional/sane-scripts/src/sane-bt-rm @@ -3,11 +3,15 @@ # vim: set filetype=python : import argparse +import logging from sane_bt import TransmissionApi def main(): + logging.basicConfig() + logging.getLogger().setLevel(logging.INFO) + parser = argparse.ArgumentParser(description="remove a torrent and trash its data") TransmissionApi.add_arguments(parser) parser.add_argument("torrent", help="numerical ID of the torrent in the transmission list (see sane-bt-show)") diff --git a/pkgs/additional/sane-scripts/src/sane-bt-show b/pkgs/additional/sane-scripts/src/sane-bt-show index 84ae4316..4054d430 100755 --- a/pkgs/additional/sane-scripts/src/sane-bt-show +++ b/pkgs/additional/sane-scripts/src/sane-bt-show @@ -3,10 +3,14 @@ # vim: set filetype=python : import argparse +import logging from sane_bt import MediaMeta, TransmissionApi def main(): + logging.basicConfig() + logging.getLogger().setLevel(logging.INFO) + parser = argparse.ArgumentParser(description="show information about one or more torrents on servo") TransmissionApi.add_arguments(parser) parser.add_argument("torrents", nargs="*", help="torrent id to show (acquire by first calling with no args)")