Skip to content

Instantly share code, notes, and snippets.

@Fooftilly
Last active February 3, 2024 16:09
Show Gist options
  • Save Fooftilly/33dcfcc84bcfb479d973df54912850ea to your computer and use it in GitHub Desktop.
Save Fooftilly/33dcfcc84bcfb479d973df54912850ea to your computer and use it in GitHub Desktop.
Limit upload speed in qBittorrent based on the upload ratio of individual torrents
#!/usr/bin/python
import qbittorrentapi #Download here: https://pypi.org/project/qbittorrent-api/
import time
# SETTINGS:
# Upload speed of an individual torrent won't be limited until it reached this ratio
UNLIMITED_RATIO = 5
UNLIMITED_SPEED = -1 # Default upload speed for torrents below UNLIMITED_RATIO (set to -1 for infinite)
# Proportionally lower upload speeds from MAX_SPEED to MIN_SPEED until the ratio of a individual torrent reaches MAX_RATIO.
MIN_RATIO = 5
MIN_SPEED = 100 # Minimum upload speed in KiB/s
MAX_RATIO = 20
MAX_SPEED = 400 # Maximum upload speed in KiB/s
CHECK_INTERVAL_SECONDS = 300 # Check torrent ratios every 300 seconds (5 minutes)
# ANSI escape codes for text formatting
RED = '\033[91m'
GREEN = '\033[92m'
BLUE = '\033[94m'
ENDC = '\033[0m'
def colorize_output(message, color):
return f"{color}{message}{ENDC}"
def adjust_upload_limit(qb_client, torrent, current_ratio):
torrent_hash = torrent['hash']
if current_ratio < UNLIMITED_RATIO:
qb_client.torrents_set_upload_limit(torrent_hashes=[torrent_hash], limit=UNLIMITED_SPEED)
print(colorize_output(f"Torrent {torrent['name']} - Ratio: {current_ratio:.2f} - Upload Limit: Unlimited", GREEN))
elif MIN_RATIO <= current_ratio <= MAX_RATIO:
upload_limit = max(MAX_SPEED - int((current_ratio - MIN_RATIO) * 20), MIN_SPEED)
qb_client.torrents_set_upload_limit(torrent_hashes=[torrent_hash], limit=upload_limit * 1024)
print(colorize_output(f"Torrent {torrent['name']} - Ratio: {current_ratio:.2f} - Upload Limit: {upload_limit} KiB/s", BLUE))
else:
qb_client.torrents_set_upload_limit(torrent_hashes=[torrent_hash], limit=MIN_SPEED * 1024)
print(colorize_output(f"Torrent {torrent['name']} - Ratio: {current_ratio:.2f} - Upload Limit: {MIN_SPEED} KiB/s", RED))
def main():
qbt_client = qbittorrentapi.Client(host='localhost:8080', username='admin', password='adminadmin')
while True:
torrents = qbt_client.torrents.info.completed()
for torrent in torrents:
if 'public' in torrent['tags'] and torrent['size'] > 300 * 1024 * 1024:
current_ratio = torrent['ratio']
adjust_upload_limit(qbt_client, torrent, current_ratio)
time.sleep(CHECK_INTERVAL_SECONDS)
if __name__ == "__main__":
main()
@Fooftilly
Copy link
Author

Fooftilly commented Feb 3, 2024

Torrents under 300 MiB are excluded from the rules in the settings. They are always seeding with your qBittorrent settings. Also, you must tag torrents you want this script to apply to with tag "public" or remove part of 52nd line 'public' in torrent['tags'] and in the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment