Archived

This topic is now archived and is closed to further replies.

NorthernBear

Updated Shell Script to Remove Completed Torrents

Recommended Posts

Here is a shell script to remove completed torrents....

This works with uTorrent 3.2.

Enjoy!


#!/bin/bash

# VARIABLES
user="USER"
password="PASS"
server="SERVER"
port="PORT"


# Don't change anything below this line (unless you know what you're doing)
WGET_PARAMS_INITIAL="--save-cookies cookies.txt --keep-session-cookies"
WGET_INITIAL="wget $WGET_PARAMS_INITIAL"
WGET="wget --load-cookies cookies.txt"

# Get the auth token.
resp1=`$WGET_INITIAL --http-user=$user --http-password=$password -q -O - http://$server:$port/gui/token.html`

token=`echo -e "$resp1" | awk -F'[<>]' '/<div[^>]*id=[\"'"'"']*token[\"'"'"'][^>]*>([^<]*)<\/div>/ { print $5 }'`

# Get the list of finished torrents
resp2=`$WGET --http-user=$user --http-password=$password -q -O - http://$server:$port/gui/?list=1\&token=$token`

# Substring it to the start of the torrents list.
ndx=`echo -e "$resp2" | tr "\n" " " | awk '{ndx=match($0, /,"torrents": \[/); print ndx }'`
sstr=${resp2:ndx}

# Look at each line and identify the torrent listing.
rx='^\[\"[^"]*\",[0-9]'
IFS="
"

#echo Processing Line: $sstr
for line in $sstr
do
# If this line matches a torrent item in the torrents property...
if [[ "$line" =~ $rx ]]; then
# Tokenize the fields.
lineTokens=(`echo -e "$line" | sed -e 's/\[//g;s/\]//g;s/"//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' `)

torrent=${lineTokens[2]}
size=${lineTokens[3]}
percent=${lineTokens[4]}
downloaded=${lineTokens[5]}


echo TORRENT: $torrent
echo " SIZE: '$size'"
echo " PERCENT: '$percent'"
echo " DOWNLOADED: $downloaded"

# Double check that this is a torrent listing and that it's finished.
if [[ "$percent" == "1000" ]]; then
# Make the call to remove the torrent.
TORRENT=${lineTokens[2]}
resp3=`wget --load-cookies cookies.txt --http-user=$user --http-password=$password -q -O - http://$server:$port/gui/?action=$removeAction\&token=$token\&hash=${lineTokens[0]}`
if [[ "$resp3" == "" ]]; then
echo "Error removing torrent: ${lineTokens[2]} (id: ${lineTokens[0]})"
fi
fi
fi
done

Share this post


Link to post
Share on other sites

and how does one actually use this? I made a .bat and had set it to run after download complete but it didnt seem to work. Sorry for the noob question but Im just fumbling my way through at the moment.

Share this post


Link to post
Share on other sites

Script works with one change. Nowhere in the script does it set removeAction. So the actual wget near the end just errors because there is no action defined. Right before that wget just add this line:

removeAction="remove"

or use "removeData" if you want the files deleted.

Maybe the author had this set in his environment or something so that he could alternate between remove and removeData?

thanks

steve

Share this post


Link to post
Share on other sites

I know this is an old post and for uTorrent 3.2, but does anyone know how to make this work with 3.3?

 

I run the script, but all I get back is an "Error removing torrent:" message on the torrent(s) it should be removing.

 

Thanks, Eddie

Share this post


Link to post
Share on other sites