Skip to content

Instantly share code, notes, and snippets.

@SingularReza
Created October 9, 2024 08:05
Show Gist options
  • Select an option

  • Save SingularReza/623a966f94234d2c5d8dcc362b9b386d to your computer and use it in GitHub Desktop.

Select an option

Save SingularReza/623a966f94234d2c5d8dcc362b9b386d to your computer and use it in GitHub Desktop.
python script to add trackers to multiple torrents at once in qbittorrent, from https://github.com/qbittorrent/qBittorrent/issues/18539#issuecomment-2395072253
import qbittorrentapi
# API Configuration
qb = qbittorrentapi.Client(host='http://0.0.0.0:8080', username='username', password='password')
# New tracker URL to replace existing trackers
new_tracker_url = "new Tracker URL"
# Attempt to log in to qBittorrent
try:
qb.auth_log_in()
except qbittorrentapi.LoginFailed as e:
print(f"Connection error: {e}")
exit(1)
# Retrieve the list of all torrents
try:
torrents = qb.torrents_info()
except qbittorrentapi.APIError as e:
print(f"Error retrieving torrents: {e}")
qb.auth_log_out()
exit(1)
# Initialize counters
total_count = len(torrents)
changed_count = 0
ignored_count = 0
# Iterate over each torrent and replace all trackers with the new URL
for torrent in torrents:
try:
print(f"Torrent: {torrent.name}")
# Get the list of trackers for the current torrent
trackers = qb.torrents_trackers(torrent.hash)
for tracker in trackers:
# Skip special trackers like DHT, PeX, and LSD
if tracker.url not in ['** [DHT] **', '** [PeX] **', '** [LSD] **']:
if tracker.url != new_tracker_url:
# Replace the old tracker URL with the new tracker URL
qb.torrents_edit_tracker(torrent_hash=torrent.hash, original_url=tracker.url, new_url=new_tracker_url)
print(f" Old Tracker: {tracker.url} replaced with {new_tracker_url}")
changed_count += 1
else:
print(f" Torrent ignored because it already has the same tracker: {tracker.url}")
ignored_count += 1
print("-")
except qbittorrentapi.APIError as e:
print(f"Error processing torrent {torrent.name}: {e}")
# Display summary
print(f"Total number of torrents: {total_count}")
print(f"Number of trackers replaced: {changed_count}")
print(f"Number of torrents ignored: {ignored_count}")
# Log out from qBittorrent
qb.auth_log_out()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment