Skip to content

Instantly share code, notes, and snippets.

@danwilldev
Last active April 14, 2025 22:24
Show Gist options
  • Save danwilldev/0d02b1a4097d772df4e5a010986ca90f to your computer and use it in GitHub Desktop.
Save danwilldev/0d02b1a4097d772df4e5a010986ca90f to your computer and use it in GitHub Desktop.
A script to pause active torrents when new torrent added. To stop overwhelming of HDDs. Script runs continuously until the initial torrents have ended. Pausing any torrents that start uploading that are not being raced. If new torrents are added afterwards the script exits without resuming torrents as it assumes a new instance will be running an…

For use with AutoBrr. Dependancy qbittorrentapi

In filter settings > Actions > Add New > Exec

Command is path to file location eg /home/user/scripts/pythonScripts/pauseActiveTorrents.py

Argument {{.TorrentHash}} but can also be null. This is used to check if torrent actually exists in client as script is run even for torrents that may nto be added by AutoBrr due to client settings eg maxActiveDownloads.

GL HF and BRRRRR

#!/usr/bin/env python3
from qbittorrentapi import Client
import sys, time
#----------------CONFIG----------------
catagoriesToExclude = ['autodl-1', 'autodl-2', 'autodl-3']
tagsToExclude = ['tackerName']
uploadSpeedCutOff = 15000000
uploadSpeedCutOffToggle = True
tag = 'Temp Paused'
#--------------------------------------
client = Client(host='http://127.0.0.1:PORTNUMBER')
averageUploadSpeed = 0
def logoutOfClient():
client.auth_log_out()
def logoutOfClient():
client.auth_log_out()
def getTorrentProperties(torrentHash):
transferInfo = client.torrents_properties(torrentHash)
return transferInfo
#Checking if cli param exists and is valid
newTorrentHash = []
if (len(sys.argv)) > 1:
newTorrentHash.append(sys.argv[1])
try:
getTorrentProperties(newTorrentHash)
print('Torrent Added: ' + str(newTorrentHash))
except:
print('Torrent Not Found, quitting.')
logoutOfClient()
sys.exit()
def getActiveTorrents():
listOfTorrentHashes = []
for torrent in client.torrents_info(status_filter = 'active'):
if torrent.category not in catagoriesToExclude and torrent.tags not in tagsToExclude and torrent.ratio > 1.1 and torrent.seeding_time > 0:
listOfTorrentHashes.append(torrent.hash)
return listOfTorrentHashes
def getDownloadingTorrents():
listOfTorrentHashes = []
for torrent in client.torrents_info(status_filter = 'downloading'):
listOfTorrentHashes.append(torrent.hash)
return listOfTorrentHashes
def getTorrentsByTag(tag):
listOfTorrentHashes = []
for torrent in client.torrents_info():
if tag in torrent.tags:
listOfTorrentHashes.append(torrent.hash)
return listOfTorrentHashes
def getNewTorrents():
listOfTorrentHashes = []
for torrent in client.torrents_info():
if torrent.seeding_time == 0:
listOfTorrentHashes.append(torrent.hash)
return listOfTorrentHashes
def tagTorrents(tag, listOfTorrentHashes):
client.torrents_add_tags(tag, listOfTorrentHashes)
def removeTags(tag, listOfTorrentHashes):
client.torrents_remove_tags(tag, listOfTorrentHashes)
def pauseTorrents(listOfTorrentHashes):
client.torrents_pause(listOfTorrentHashes)
return listOfTorrentHashes
def resumeTorrents(listOfTorrentHashes):
client.torrents_resume(listOfTorrentHashes)
def getClientUploadSpeed():
transferInfo = client.transfer_info()
return transferInfo['up_info_speed']
def getTorrentUploadSpeed(torrentHash):
transferInfo = client.torrents_properties(torrentHash)
return transferInfo['up_speed_avg']
def getClientDownloadSpeed():
transferInfo = client.transfer_info()
return transferInfo['dl_info_speed']
def checkIfTorrentsStarted(listOfTorrentHashes):
numberOfTorrents = 0
numberOfStarted = 0
for torrent in listOfTorrentHashes:
numberOfTorrents += 1
props = getTorrentProperties(torrent)
upSpeedAvg = int(props['up_speed_avg'])
seeds = int(props['seeds'])
seedingTime = int(props['seeding_time'])
if upSpeedAvg > 0 or seeds > 0 or seedingTime > 0:
numberOfStarted += 1
if numberOfTorrents == numberOfStarted:
return True
else:
return False
def checkIfTorrentsDone(listOfTorrentHashes):
numberOfTorrents = 0
numberOfDone = 0
for torrent in listOfTorrentHashes:
numberOfTorrents += 1
props = getTorrentProperties(torrent)
seedingTime = int(props['seeding_time'])
if seedingTime > 0:
numberOfDone += 1
if numberOfTorrents == numberOfDone:
return True
else:
return False
def quit(logout):
time.sleep(2)
newTorrents = getNewTorrents()
if len(newTorrents) != 0:
x = 0
for torrent in newTorrents:
torrentUploadSpeed = getTorrentUploadSpeed(torrent)
if (torrentUploadSpeed > 0 and torrentUploadSpeed < uploadSpeedCutOff):
x += 1
if x == len(newTorrents):
torrentsToUnpause = getTorrentsByTag(tag)
time.sleep(5)
resumeTorrents(torrentsToUnpause)
time.sleep(5)
removeTags(tag, torrentsToUnpause)
time.sleep(5)
if logout == True:
print('Ended.')
logoutOfClient()
sys.exit()
print('New Torrent Detected, quitting without unpausing.')
print(newTorrents)
if logout == True:
print('Ended.')
logoutOfClient()
sys.exit()
else:
return
torrentsToUnpause = getTorrentsByTag(tag)
time.sleep(5)
resumeTorrents(torrentsToUnpause)
time.sleep(5)
removeTags(tag, torrentsToUnpause)
time.sleep(5)
print('Ended.')
logoutOfClient()
sys.exit()
#---------------------------------------------------------------------------------
pausedTorrents = []
priorityTorrents = getNewTorrents()
pausedTorrents += (pauseTorrents(getActiveTorrents()))
print('Priority Torrent: ' + str(priorityTorrents))
time.sleep(2)
tagTorrents(tag, pausedTorrents)
def run(newTorrentHash, priorityTorrents, pausedTorrents, uploadSpeedCutOffToggle):
try:
if (len(newTorrentHash)) >= 1:
if len(priorityTorrents) != 0:
counter = 0
averageUploadSpeed = 0
pausedTorrents += (pauseTorrents(getActiveTorrents()))
time.sleep(5)
time.sleep(2)
tagTorrents(tag, pausedTorrents)
while not checkIfTorrentsStarted(newTorrentHash):
pausedTorrents += (pauseTorrents(getActiveTorrents()))
time.sleep(2)
tagTorrents(tag, pausedTorrents)
print('Torrent not started, waiting.')
time.sleep(30)
while not checkIfTorrentsDone(newTorrentHash):
print(f'Torrent: {newTorrentHash} still downloading.')
time.sleep(5)
recentTorrentsToPause = [item for item in getActiveTorrents() if item not in newTorrentHash]
pausedTorrents += (pauseTorrents(recentTorrentsToPause))
print(f'Paused torrents: {pausedTorrents}')
print('Waiting for torrents to finish...')
time.sleep(5)
tagTorrents(tag, pausedTorrents)
time.sleep(30)
if uploadSpeedCutOffToggle:
time.sleep(10)
torrentUpSpeed = getTorrentUploadSpeed(newTorrentHash)
if torrentUpSpeed < uploadSpeedCutOff:
averageUploadSpeed += torrentUpSpeed
counter += 1
print(f'Upload too slow, warn {counter}!')
time.sleep(120)
if counter == 3 and averageUploadSpeed / 3 < uploadSpeedCutOff:
time.sleep(10)
print('Average upload slow, stopping wait.')
quit(logout = False)
while not checkIfTorrentsDone(newTorrentHash):
time.sleep(60)
if getTorrentUploadSpeed(newTorrentHash) > uploadSpeedCutOff:
run(newTorrentHash, priorityTorrents, pausedTorrents, uploadSpeedCutOffToggle)
else:
quit(logout = True)
elif counter > 0 and counter < 3:
print(f'Slow {counter} times, need more itterations for average.')
time.sleep(45)
else:
print(f'Average Upload speed threshold not hit, would unpause at {uploadSpeedCutOff} but Client has averaged {averageUploadSpeed / 3}')
else:
counter = 0
else:
time.sleep(30)
quit(logout = True)
except:
quit(logout = True)
run(newTorrentHash, priorityTorrents, pausedTorrents, uploadSpeedCutOffToggle)
for x in range (12):
time.sleep(30)
pausedTorrents += (pauseTorrents(getActiveTorrents()))
time.sleep(5)
tagTorrents(tag, pausedTorrents)
time.sleep(30)
torrentsToUnpause = getTorrentsByTag(tag)
time.sleep(5)
resumeTorrents(torrentsToUnpause)
time.sleep(5)
removeTags(tag, torrentsToUnpause)
time.sleep(5)
logoutOfClient()
sys.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment