sane-bt-add: support moving torrents to new directories

This commit is contained in:
Colin 2023-06-22 23:48:00 +00:00
parent e8265807a9
commit afe27fd9cb
4 changed files with 55 additions and 15 deletions

View File

@ -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,

View File

@ -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()

View File

@ -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)")

View File

@ -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)")