Created
February 21, 2010 18:55
-
-
Save Daenyth/310461 to your computer and use it in GitHub Desktop.
torrpen - automated torrent management by size
This file contains 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 | |
rtorrent_watch_dir=~/torrent | |
torrent_holding_pen=~/torrent/pending | |
buffer=$((600 * 1024)) # Size in KB to keep unused (600MB) | |
space_left=$(($(df $rtorrent_watch_dir | awk '/dev/ {print $4}') - $buffer)) | |
tmpdir=$(mktemp -d /tmp/torrpen.XXXX) | |
get_torrent_size () { | |
size=$(echo "$(transmissioncli --info "$1" |& awk '/size/ {print $2}') / 1024" | bc) | |
echo $size | |
} | |
cleanup () { | |
rm -rf "$tmpdir" | |
exit | |
} | |
cd "$torrent_holding_pen" | |
declare -A torrent_sizes | |
for torrent in *.torrent; do | |
[[ -f "$torrent" ]] || exit # Dir is empty | |
torrsize=$(get_torrent_size "$torrent") | |
torrent_sizes["$torrent"]=$torrsize | |
done | |
for key in "${!torrent_sizes[@]}"; do | |
echo "${torrent_sizes[$key]} $key" | |
done | sort -n > "$tmpdir/sizes" | |
projected_space=$space_left | |
while read size torrent < "$tmpdir/sizes"; do | |
(( projected_space > 0 )) || break | |
if (( size < projected_space )); then | |
mv "$torrent" "$rtorrent_watch_dir" | |
let "projected_space -= size" | |
else | |
break | |
fi | |
done | |
cleanup |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment