Skip to content

Instantly share code, notes, and snippets.

@rcguy
Created January 12, 2022 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rcguy/dd40fc1bab7be15fabbbee91f4a9f074 to your computer and use it in GitHub Desktop.
Save rcguy/dd40fc1bab7be15fabbbee91f4a9f074 to your computer and use it in GitHub Desktop.
Copy torrent metafile to folder matching label from rTorrent using XML-RPC
#!/usr/bin/env python3
"""
rtorrent_copy_torrents.py
v2021-01-12
Copy torrent metafile to folder matching label from rTorrent using XML-RPC.
This script probably does not do what you want, its for a very specific use-case.
Only works when the torrent label matches the root folder the torrent is saved in.
label: 1080p-web
torrent path: /mnt/storage/downloads/1080p-web/torrent.mkv
output path: /home/user/copied_torrents/1080p-web/torrent.mkv.torrent
"""
# import required modules
import xmlrpc.client, re, os, shutil, logging, sys
# Create an object to represent our server. Uses ruTorrent xmlrpc socket, http basic auth
server_url = "http://USER:PASSWORD@127.0.0.1:80/xmlrpc"
rtorrent = xmlrpc.client.Server(server_url)
# Paths
home_dir = os.path.expanduser('~')
rtorrent_session_dir = os.path.join(home_dir, '.session')
output_base_path = os.path.join(home_dir, 'copied_torrents')
log_file = os.path.join(home_dir, '.log' , 'rtorrent_copy_torrents.log')
# Input
try:
torrent_label_input = sys.argv[1]
except IndexError as e:
#print('ERROR:', e)
print('Torrent label must be supplied as the first argument!')
exit()
# Open log file
logging.basicConfig(filename=log_file, filemode='a', format='%(asctime)s - %(message)s', datefmt='%m/%d/%Y %H:%M:%S %z', level=logging.DEBUG)
# Get torrents in the main view
mainview = rtorrent.download_list("", "main")
# For each torrent in the main view
for torrent in mainview:
# Get torrent info
torrent_label = rtorrent.d.custom1(torrent)
torrent_label_regex = re.search(torrent_label_input, torrent_label)
# Only remove torrents that match label search
if torrent_label_regex:
# Get torrent info
torrent_hash = rtorrent.d.hash(torrent)
torrent_name = rtorrent.d.name(torrent)
torrent_path = rtorrent.d.base_path(torrent)
# Build output path
torrent_base_path = os.path.dirname(torrent_path)
torrent_output_dir_regex = "^/[\w\-/]*(?=/%s)"%(torrent_label)
torrent_output_full_path = re.sub(torrent_output_dir_regex, output_base_path, torrent_base_path)
# Torrent files
torrent_ext = '.torrent'
torrent_input_file = os.path.join(rtorrent_session_dir, torrent_hash + torrent_ext)
torrent_output_file = os.path.join(torrent_output_full_path, torrent_name + torrent_ext)
# Print some info
print()
print(torrent_name)
print(torrent_input_file)
print(torrent_output_file)
# Make output dir
if not os.path.exists(torrent_output_full_path):
os.makedirs(torrent_output_full_path)
# Copy torrent files
if not os.path.exists(torrent_output_file):
if os.path.isfile(torrent_input_file):
shutil.copy2(torrent_input_file, torrent_output_file)
logging.info("Torrent Copied: %s", torrent_output_file)
else:
logging.warning("Torrent Exists: %s", torrent_output_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment