From 14d403dceb1b131cefec1378430f8d639eab5674 Mon Sep 17 00:00:00 2001 From: Abdussamet Kocak Date: Sun, 26 Jan 2025 11:14:53 +0300 Subject: [PATCH] transmission: Add clean option to move files to trash after successful operation --- transmission.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/transmission.py b/transmission.py index b0dc21e..576bb4c 100755 --- a/transmission.py +++ b/transmission.py @@ -8,10 +8,40 @@ import argparse import base64 import logging from os import getenv +import os +import subprocess import httpx from pathlib import Path +def move_to_trash(file_path: Path): + if not file_path.is_file(): + raise FileNotFoundError + if os.name != "darwin": + args = [ + "osascript", + "-e", + f'tell app "Finder" to move POSIX file "{file_path}" to trash', + ] + subprocess.run( + args, + check=True, + ) + if os.name == "win32": + # move to recycle bin + # Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile("C:\path\to\your\file.txt", 'OnlyErrorDialogs', 'SendToRecycleBin') + args = [ + "powershell", + "-NoProfile", + "-Command", + f'Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile("{file_path}", "OnlyErrorDialogs", "SendToRecycleBin")', + ] + subprocess.run( + args, + check=True, + ) + + def add_torrent(client: httpx.Client, torrent_path: Path): encoded_torrent = base64.b64encode(torrent_path.read_bytes()).decode() @@ -37,6 +67,7 @@ def parse_args(): parser.add_argument("torrent_paths", type=Path, nargs="+", help="Path to the torrent file") parser.add_argument("--host", required=True, default=getenv("TRANSMISSION_HOST", "http://localhost:9091"), help="Transmission RPC host URL") parser.add_argument("--auth", default=getenv("TRANSMISSION_AUTH"), help="Transmission RPC username:password") + parser.add_argument("--clean", action="store_true", help="Delete torrent file after successful upload") return parser.parse_args() @@ -58,6 +89,8 @@ def main(): logging.error(f"Torrent file not found: {args.torrent_path}") raise SystemError(1) add_torrent(client=client, torrent_path=torrent_path) + if args.clean: + move_to_trash(torrent_path) if __name__ == "__main__":