Skip to content

Instantly share code, notes, and snippets.

@jfeilbach
Last active June 10, 2019 21:10
Show Gist options
  • Save jfeilbach/ac2cded08be4b50aaf7717c538c1df28 to your computer and use it in GitHub Desktop.
Save jfeilbach/ac2cded08be4b50aaf7717c538c1df28 to your computer and use it in GitHub Desktop.
move torrents and torrent metadata to new host and client
#!/bin/bash
# Move torrent data and metadata from local transmission client to new host running transmission.
# This was made for MacOS
# requires transmission, install via brew. 'brew install transmission'
SECONDS=0
id=${1}
tracker='t.myanonamouse.net' # search string for tracker name, results will be transferred
cmd='/usr/local/bin/transmission-remote'
show='/usr/local/bin/transmission-show'
user='jason'
new_host='192.168.1.40'
port='9091'
torrent_files=/Volumes/Promise\ Pegasus/jason/Library/Application\ Support/Transmission/Torrents
options="-avhz --stats --progress --size-only"
list=$HOME/file_list
tmp_dir=$HOME
remote_dir=/Users/jason/Desktop/torrents_to_be_added
# set some colors for output (033 is MacOS nonsense)
NC='\033[0m'
WHITE='\033[1;37m'
YELLOW='\033[1;33m'
LIGHT_BLUE='\033[1;34m'
CYAN='\033[0;36m'
RED='\033[0;31m'
GREEN='\033[0:32m'
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
}
#get_id_list () {
# echo -e "Finding torrents based on tracker name. This will take a minute.\n"
# list_id=$($cmd -l | awk '{print $1}' | grep -E '^[0-9]+' | sed -r -e 's/^([0-9]*).*$/\1/') #
# for t_id in ${list_id}; do
# trackers=$($cmd -t ${t_id} -it | grep -E 'Tracker [0-9]+')
# treval=$(echo "${trackers}" | grep ${tracker})
# if [[ $? -eq 0 ]]; then
# get_name=$($cmd -t ${t_id} -i |grep "Name")
# echo -e "Transferring ${t_id} ${get_name}"
# t_id_array=${t_id}
# fi
#done
dl_dir=$($cmd -t ${id} -i |grep "Location" | cut -d":" -f2- | cut -d" " -f2-)
name=$($cmd -t ${id} -i |grep "Name" | cut -d":" -f2- | cut -d" " -f2-)
#magnet=$($cmd -t ${id} -i |grep "Magnet" | cut -d":" -f2- | cut -d" " -f2-)
hash1=$($cmd -t ${id} -i |grep "Hash" | cut -d":" -f2- | cut -d" " -f2-)
#echo -e "Download dir is ${WHITE}${dl_dir}${NC}\n"
#echo -e "Torrent name is ${WHITE}${name}${NC}\n"
#echo Magnet link is ${magnet}
echo $(/bin/date)
echo -e "Starting tranfer: ${WHITE}${name}${NC}."
echo -e "Torrent hash is ${WHITE}${hash1}${NC}."
#if [[ -f "${list}" ]] ; then
# echo -e "The file list was found. Proceeding...\n"
# else
# echo -e "${RED}File ${list} not found. Exiting.${NC}\n"
# exit 1
#fi
#echo -e "Searching for correct .torrent file... This might take a minute.\n"
# Read through a pregenerated list of torrents to move to get the .torrent file; export vars to tmp file to get out of subshell
cat "${list}" | while read line; do
# file_name=$( echo ${line##/*/} )
# echo -e "${file_name}"
hash2=$($show "${line}" | grep 'Hash' | awk '{ print $2 }')
if [[ "$hash1" = "$hash2" ]]; then
echo -e "${GREEN}Matching hash found.${NC} ${WHITE}${hash2}${NC} ${GREEN}Proceeding...${NC}\n"
# echo Torrent file is $line
torrent="${line}"
# echo Torrent is ${torrent}
# echo -e "Setting torrent variable...\n"
echo "export torrent=\"${torrent}\"" > "${tmp_dir}/_torrent.tmp"
torrent_base=$( echo ${line##/*/} )
# echo Torrent base is ${torrent_base}
# echo -e "Setting torrent_base variable...\n"
echo "export torrent_base=\"${torrent_base}\"" > "${tmp_dir}/_torrent_base.tmp"
break
fi
done
# set the vars for the .torrent file from the var export
# set the full path var
if [[ -f "${tmp_dir}/_torrent.tmp" ]] ; then
# echo -e "${CYAN}$(<"${tmp_dir}/_torrent.tmp")${NC}"
source "${tmp_dir}/_torrent.tmp"
if [ -z "${torrent}" ] ; then
echo -e "${RED}Error: \${torrent} is NULL${NC}\n"
exit 4
fi
else
echo -e "${RED}File _torrent.tmp not found, exiting.\n${NC}"
exit 2
fi
# set the basename var
if [[ -f "${tmp_dir}/_torrent_base.tmp" ]] ; then
# echo -e "${CYAN}$(<"${tmp_dir}/_torrent_base.tmp")${NC}"
source "${tmp_dir}/_torrent_base.tmp"
if [ -z "${torrent_base}" ] ; then
echo -e "${RED}Error: \${torrent} is NULL${NC}\n"
exit 5
fi
else
echo -e "${RED}File _torrent_base.tmp not found, exiting.\n${NC}"
exit 3
fi
# just to verify
#echo -e "Torrent is ${WHITE}${torrent}${NC}\n"
#echo -e "Torrent base is ${WHITE}${torrent_base}${NC}\n"
#torrent=$(ls -1 "${torrent_files}"/*.torrent | grep "${name}")
#echo -e "Torrent is ${WHITE}${torrent}${NC}"
fullpath=${dl_dir}/${name}
#echo -e "The path to send to rsync is ${WHITE}${fullpath}${NC}"
#torrent_base=$(/usr/bin/basename "${torrent}")
#echo -e "Torrent filename is ${WHITE}${torrent_base}${NC}"
#echo ${torrent}
/usr/local/bin/rsync ${options} "${fullpath}" "/Volumes/Raid 0/Torrent Downloads"
/usr/bin/scp "${torrent}" ${user}@${new_host}:${remote_dir}
#echo -e "Sending .torrent file ${WHITE}(${torrent})${NC} to ${WHITE}${new_host}${NC}\n"
echo -e "Sending .torrent file to ${WHITE}${new_host}${NC}\n"
/usr/bin/ssh ${user}@${new_host} "$cmd ${new_host}:${port} -a '/Users/${user}/Desktop/torrents_to_be_added/${torrent_base}' --start-paused > /dev/null"
#$cmd ${new_host}:${port} --add ${magnet} --start-paused --verify
# stop the local torrent
echo -e "${YELLOW}Stopping${NC} torrent ${WHITE}${id}${NC} - ${name} on ${WHITE}localhost${NC}.\n"
$cmd localhost:9091 -t ${id} --stop > /dev/null
new_id=$($cmd ${new_host}:${port} -l | grep "${name}" | awk '{ print $1 }')
echo -e "The new torrent id on remote host is ${WHITE}${new_id}${NC}.\n"
echo -e "Force starting verify on new torrent.\n"
$cmd ${new_host}:${port} -t ${new_id} --verify > /dev/null
sleep 1
#$cmd ${new_host}:${port} -t ${new_id} -i | grep 'State:'
#status=$($cmd ${new_host}:${port} -t ${new_id} -i | grep 'State:')
status=$($cmd ${new_host}:${port} -t ${new_id} -i | grep 'State:' | awk '{ print $2 }')
if [[ ${status} = "Idle" ]]; then
echo -e "Status is ${WHITE}${status}${NC}\n"
elif [[ ${status} = "Stopped" ]]; then
echo -e "Status is ${YELLOW}${status}${NC}. ${GREEN}Starting verify...${NC}\n"
$cmd ${new_host}:${port} -t ${new_id} --verify > /dev/null
elif [[ ${status} = "Seeding" ]]; then
echo -e "Status is ${WHITE}${status}${NC}\n"
elif [[ ${status} = "Verifying" ]]; then
echo -e "Status is already ${YELLOW}${status}${NC}. Please check again once verify is done.\n"
fi
# clean up tmp files
echo -e "Cleaning up temporary files."
rm "${tmp_dir}/_torrent.tmp"
rm "${tmp_dir}/_torrent_base.tmp"
time=$(displaytime)
echo ""
echo -e "Completed at ${WHITE}$(/bin/date)${NC}, took ${YELLOW}${time}${NC}."
echo -e "===========================================================================\n"
echo -e "\n\n\n"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment