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


沒有留言:

張貼留言

來推薦一下VPS Racknerd

網路上有蠻多這間VPS的推薦,不過大多是賺傭金的 推薦的點不外乎是便宜,然後中國那邊很多拿來當VPN跳板主機 我當初也是無聊租了一台來玩 後來實際上用起來覺得還可以 但是有幾個小地方當初租的時候沒注意到 就來分享一下,順便分享我的推薦連結 XDDD Racknerd有很多優惠方案...