Last active
November 8, 2018 15:26
-
-
Save jfeilbach/480bb303b70ae0f7a826b92cde9babb2 to your computer and use it in GitHub Desktop.
Update torrents with current tracker URL via transmission-remote (uses a gsed alias)
This file contains hidden or 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
#!/bin/bash | |
SECONDS=0 | |
CMD=/usr/local/bin/transmission-remote | |
TR=$(/usr/local/bin/transmission-remote) | |
# Your announce key for the new tracker (keep secret) | |
KEY="key" | |
# Transmission daemon host | |
HOST="localhost:9091" | |
# Authentication to transmission-daemon; 1 = True, 0 = False | |
AUTH=1 | |
TDUSER="user" | |
TDPASS="passwd" | |
# Old tracker URL to be replaced | |
OLD="old" | |
# New tracker URL | |
NEW="new" | |
displaytime () { | |
local T=$SECONDS | |
local D=$((T/60/60/24)) | |
local H=$((T/60/60%24)) | |
local M=$((T/60%60)) | |
local S=$((T%60)) | |
[[ $D > 0 ]] && printf '%d days ' $D | |
[[ $H > 0 ]] && printf '%d hours ' $H | |
[[ $M > 0 ]] && printf '%d minutes ' $M | |
[[ $D > 0 || $H > 0 || $M > 0 ]] && printf 'and ' | |
printf '%d seconds\n' $S | |
} | |
# Check $PATH for command | |
if ! [ -x "$(command -v transmission-remote)" ]; then | |
echo -e 'Error: transmission-remote is not on your $PATH or is not installed.\n' >&2 | |
exit 1 | |
fi | |
# Check if command exists. | |
if [[ -f $CMD ]] ; then | |
echo -e "The command transmission-remote was found, Proceeding...\n" | |
else | |
echo -e "The command transmission-remote was not found at $TR. Please edit \$TR or install transmission-remote. Exiting.\n" | |
exit 2 | |
fi | |
if [ ${AUTH} -eq 1 ]; then | |
OPTS="${HOST} -n ${TDUSER}:${TDPASS}" | |
else | |
OPTS="${HOST}" | |
fi | |
# Get all torrent IDs | |
LISTID=$(${TR} ${OPTS} -l | awk '{print $1}' | grep -E '^[0-9]+' | gsed -r -e 's/^([0-9]*).*$/\1/') | |
COUNT=$(echo ${LISTID} | wc -l) | |
echo -e "Found $COUNT torrents for $HOST\n" | |
for id in ${LISTID}; do | |
echo "================================" | |
# Get all trackers | |
TRACKS=$(${TR} ${OPTS} -t ${id} -it | grep -E 'Tracker [0-9]+') | |
# Check for old tracker | |
TRTEST=$(echo "${TRACKS}" | grep '${OLD}') | |
if [ $? -eq 0 ]; then | |
echo "Found old tracker - ${OLD} on torrent ID ${id} ..." | |
# Get tracker ID | |
TRKID=$(echo "${TRACKS}" | grep "${OLD}" | gsed -r -e 's/Tracker ([0-9]+).*/\1/') | |
echo $TRKID | |
echo "Removing ${OLD} tracker from transmission client..." | |
${TR} ${OPTS} -t ${id} -tr ${TRKID} | |
echo "Adding new tracker ${NEW} to torrent ${id} ..." | |
${TR} ${OPTS} -t ${id} -td "${NEW}/${KEY}/announce" | |
fi | |
done | |
TIME=$(displaytime) | |
echo -e "Completed in $TIME.\n" | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment