2018年8月31日 星期五

Remove utorrent torrent from command line

refer:
skyy99 post on utorrent forum
https://forum.utorrent.com/topic/66811-script-to-remove-finished-torrents/

The original post does not work, cuz it utorrent would check cookies, so I've fix it first
After fix the script, the script could fetch correct list and remove torrent and data correctly.

Second, I need find the oldest torrent, but the list structure is hard to sorting
So I try the dummy way ( But it save some coffee time for me :p )
Just find the oldest torrent and remove it.

Cuz I need the script for free the space automation,
So if the space still occupy, just need call it again. :p

It's dirty, but worked!


# Get the auth token.
user="********"
password="********"
server="127.0.0.1"
port="8080"
#removeAction="remove" # Change to removedata to remove torrent+data
removeAction="removedata"
Label="LabelText"
# Don't change anything below this line (unless you know what you're doing)

resp1=`wget --save-cookies cookies.txt --keep-session-cookies --http-user=$user --http-password=$password -q -O - \
http://$server:$port/gui/token.html`
token=`echo -e "$resp1" | awk -F'[<>]' '/

]*id=[\"'"'"']*token[\"'"'"'][^>]*>([^<]*)<\/div>/ { print $5 }'`

# Get the list of finished torrents
resp2=`wget --load-cookies cookies.txt --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}
oldest=`date +"%s"`
# Look at each line and identify the torrent listing.
rx='^\[\"[^"]*\",[0-9]'
IFS="
"
for line in $sstr
do
if [[ "$line" =~ $rx ]]; then
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]}' `)
TokenLabel=$[${#lineTokens[*]}-15]
if [[ "${#lineTokens[*]}" -ge "26" && "${lineTokens[1]}" == "136" && "${lineTokens[$TokenLabel]}" == "$Label" ]]; then
if [[ $oldest -gt ${lineTokens[22]} ]]; then
oldest=${lineTokens[22]}
fi
fi
fi
done
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]}' `)
dlNdx=$[${#lineTokens[*]}-15]
TokenCount=${#lineTokens[*]}
TokenLabel=$[${#lineTokens[*]}-15]
TokenStatus=$[${#lineTokens[*]}-7]
TokenCompleted=$[${#lineTokens[*]}-4]
# Double check that this is a torrent listing and that it's finished.
#echo $line
#echo "${#lineTokens[*]} ${lineTokens[$TokenLabel]} ${lineTokens[$TokenStatus]} ${lineTokens[$TokenCompleted]}"
#if [[ "${#lineTokens[*]}" -ge "18" && "${lineTokens[1]}" == "136" && "${lineTokens[$dlNdx]}" == "1000" ]]; then
if [[ "$TokenCount" -ge "26" && "${lineTokens[1]}" == "136" && "${lineTokens[$TokenLabel]}" == "$Label" ]]; then
# Make the call to remove the torrent.
if [ "${lineTokens[$TokenCompleted]}" == "$oldest" ]; then
echo "Torrent $removeAction: ${lineTokens[0]}"
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
fi
done

rm cookies.txt


沒有留言:

張貼留言

Racknerd可不可以升級?搬VPS?搬遷的Step by Step (Racknerd 2024 Black Friday 新方案)

 先附上Racknerd 2024 Black Friday 新方案 RAM CPU core 空間 流量 頻寬 價格 1   G 1核 20G 1.5T/月 1Gbps $10.99/年 2.5G 2核 40G 3.0T/月 1Gbps $18.93/年 3   G 2核 60...