Created
January 7, 2025 00:46
-
-
Save ashleyconnor/4408514f8e8bbc971849b8a62cc4b906 to your computer and use it in GitHub Desktop.
move_to_folder.py is a short script that detects if a torrent entry is a single file and moves it to a directory generated from the torrent name. This is primarily for applications that don't deal well with files not being in their own folder e.g. Plex audiobook collections.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
# move_to_folder.py is a short script that detects if a torrent entry is a single file | |
# and moves it to a directory generated from the torrent name. | |
# This is primarily for applications that don't deal well with files not being in their own folder | |
# e.g. Plex audiobook collections. | |
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # | |
import os | |
import re | |
from pathlib import Path | |
import transmission_rpc # pip install transmission-rpc | |
# config | |
TRANSMISSION_HOST = '192.168.4.105' | |
TRANSMISSION_PORT = 9092 | |
TRANSMISSION_USER = 'admin' | |
TRANSMISSION_PASSWORD = 'admin' | |
TARGET_FOLDER = Path('/downloads/complete/audiobooks') | |
def torrent_name_to_path(input_string): | |
input_string = os.path.splitext(input_string)[0] | |
input_string = input_string.lower() | |
input_string = input_string.replace(" ", "_") | |
input_string = re.sub(r'[^a-z0-9_]', '', input_string) | |
return Path(input_string) | |
def path_is_filename(path_str): | |
path = Path(path_str) | |
return path.parent == Path("") | |
def move_torrents_to_folder(): | |
try: | |
# connect to Transmission | |
client = transmission_rpc.Client( | |
host=TRANSMISSION_HOST, | |
port=TRANSMISSION_PORT, | |
username=TRANSMISSION_USER, | |
password=TRANSMISSION_PASSWORD | |
) | |
# get all torrents | |
torrents = client.get_torrents() | |
for torrent in torrents: | |
# get list of files in the torrent | |
files = torrent.get_files() | |
file_paths = [f for f in files if path_is_filename(f.name)] | |
torrent_download_dir = Path(torrent.download_dir) | |
# check if any files are at the torrent root (i.e. not in a folder) | |
if torrent_download_dir == TARGET_FOLDER and file_paths: | |
dest_path = Path.joinpath(torrent_download_dir, torrent_name_to_path(torrent.name)) | |
print(f"Moving torrent '{torrent.name}' from '{torrent.download_dir}' to '{dest_path}'") | |
client.move_torrent_data(torrent.id, dest_path.resolve()) | |
print("All applicable torrents have been moved.") | |
except Exception as e: | |
print(f"An error occurred: {e}") | |
if __name__ == "__main__": | |
move_torrents_to_folder() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment