Skip to content

Instantly share code, notes, and snippets.

@darrentmorgan
Last active July 12, 2024 13:26
Show Gist options
  • Save darrentmorgan/f26f5bafb9aa700a0bc377407074a8ce to your computer and use it in GitHub Desktop.
Save darrentmorgan/f26f5bafb9aa700a0bc377407074a8ce to your computer and use it in GitHub Desktop.
A script to re-announce torrents in Deluge when they get stuck with the error "Error: unregistered torrent". It has built in limiting.
#!/bin/bash
torrentid="$1"
torrentname="$2"
torrentpath="$3"
max_attempts=100
ddport=$(grep '"daemon_port": [0-9]*' ~/.config/deluge/core.conf | awk -F ': ' '{print $2}' | awk -F ',' '{print $1}')
log() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> ~/deluge_reannounce.log
}
check_torrent_status() {
local info=$(deluge-console "connect 127.0.0.1:$ddport; info $torrentid")
local verbose_info=$(deluge-console "connect 127.0.0.1:$ddport; info -v $torrentid")
local state=$(echo "$info" | grep -oP '^\[\K[^\]]+')
local progress=$(echo "$info" | grep -oP '\d+(?=%)' | head -n1)
local tracker_status=$(echo "$verbose_info" | grep -i "Tracker status:" | cut -d':' -f2- | xargs)
log "State: $state, Progress: $progress%, Tracker status: $tracker_status"
if [[ "$state" == "D" && $(echo "$progress > 0" | bc -l) -eq 1 ]]; then
return 0 # Downloading and progress > 0%
elif [[ -n "$tracker_status" && ("$tracker_status" == *"Announce OK"* || "$tracker_status" == *"Scrape OK"* || "$tracker_status" == *"Announce Sent"*) ]]; then
return 0 # Tracker status OK or Sent
else
return 1 # Need to re-announce
fi
}
log "Starting reannounce script for torrent: $torrentname (ID: $torrentid)"
for ((attempt=1; attempt <= max_attempts; attempt++))
do
log "Attempt $attempt of $max_attempts"
if check_torrent_status; then
log "Torrent is downloading or tracker status is OK. No need to re-announce."
log "Found working torrent: $torrentname (ID: $torrentid)"
exit 0
else
log "Potential issue detected. Re-announcing..."
deluge-console "connect 127.0.0.1:$ddport; reannounce '$torrentid'" > /dev/null 2>&1
sleep 5 # Wait for 5 seconds after re-announcing
fi
done
log "Max attempts reached. Torrent may still have issues."
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment