Last active
January 12, 2022 22:46
-
-
Save rcguy/28f95d3c556faadfcff2f6d47f4e40c5 to your computer and use it in GitHub Desktop.
Remove and delete unregistered torrents from rTorrent using XML-RPC
This file contains 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
#!/usr/bin/env python3 | |
# | |
# rtorrent_delete_unregistered.py - Remove and delete unregistered torrents from rTorrent using XML-RPC | |
# v2021-01-12 | |
# | |
# import required modules | |
import xmlrpc.client, re, os, shutil, logging | |
# 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('~') | |
log_file = os.path.join(home_dir, '.log', 'rtorrent_delete_unregistered.log') | |
# 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 tracker status | |
torrent_tracker_status = rtorrent.d.message(torrent) | |
# Check if tracker status is unregistered | |
unregistered = re.search("Unregistered", torrent_tracker_status) | |
# Only remove torrents that are unregistered | |
if unregistered: | |
# Get torrent info | |
torrent_hash = rtorrent.d.hash(torrent) | |
torrent_name = rtorrent.d.name(torrent) | |
torrent_path = rtorrent.d.base_path(torrent) | |
# Remove torrent from client | |
rtorrent.d.stop(torrent_hash) | |
rtorrent.d.delete_tied(torrent_hash) | |
rtorrent.d.erase(torrent_hash) | |
# Delete file from disk | |
if os.path.exists(torrent_path): | |
if os.path.isfile(torrent_path): | |
os.remove(torrent_path) | |
logging.info("Torrent Deleted: %s", torrent_name) | |
elif os.path.isdir(torrent_path): | |
shutil.rmtree(torrent_path) | |
logging.info("Torrent Deleted: %s", torrent_name) | |
else: | |
logging.error("%s could not be deleted!", torrent_name) | |
else: | |
logging.error("Torrent Path does not exist: %s", torrent_path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment