Skip to content

Instantly share code, notes, and snippets.

@lionelyoung
Created February 6, 2014 10:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lionelyoung/8841724 to your computer and use it in GitHub Desktop.
Save lionelyoung/8841724 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Moves torrents from the queue directory into the watch directory.
#
# Example usage:
# * manage_rtorrent_queue.sh movies 3
# * manage_rtorrent_queue.sh software 1
# Check for proper number of command line args.
EXPECTED_ARGS=2
E_BADARGS=65
if [ $# -ne $EXPECTED_ARGS ]
then
echo "Usage: `basename $0` <folder_name> <max_downloads>"
echo
echo " Example: `basename $0` movies 3"
exit $E_BADARGS
fi
MAX_DOWNLOADS=$2
QUEUE_DIR=/srv/multimedia/watch/$1/queue
WATCH_DIR=/srv/multimedia/watch/$1/
LOG=/tmp/manage_rtorrent_queue.log
TIMESTAMP=`date +"%x %H:%M"`
NUM_TOTAL=`find $WATCH_DIR/. -type f | wc -l | awk '{print $1}'`;
NUM_QUEUE=`find $QUEUE_DIR/. -type f | wc -l | awk '{print $1}'`;
# Uncomment below if the queue directory is NOT a subdirectory of watch
# NUM_DOWNLOADING=$NUM_TOTAL
# The queue directory is a sudirectory of the watch directory
NUM_DOWNLOADING=$((NUM_TOTAL-NUM_QUEUE))
# RENAME ALL BLANKS TO UNDERSCORES
cd $QUEUE_DIR || exit 1
FOUND=0 # Successful return value.
for filename in * #Traverse all files in directory.
do
echo "$filename" | grep -q " " # Check whether filename
if [ $? -eq $FOUND ] #+ contains space(s).
then
fname=$filename # Strip off path.
n=`echo $fname | sed -e "s/ /_/g"` # Substitute underscore for blank.
mv "$fname" "$n" # Do the actual renaming.
fi
done
# MOVE THE FILES
echo $TIMESTAMP $1 QUEUED:$NUM_QUEUE DOWNLOADING:$NUM_DOWNLOADING | tee --append $LOG
if [ $NUM_DOWNLOADING -lt $MAX_DOWNLOADS ] && [ $NUM_QUEUE -gt 0 ]; then
num_to_move=$((MAX_DOWNLOADS-NUM_DOWNLOADING))
echo $TIMESTAMP Moving $num_to_move files | tee --append $LOG
i=0;
until [ $i -eq $num_to_move ]; do
queued_tor=`find $QUEUE_DIR/. -type f |sort | head -1 | sed 's/\ /\\ /g'`;
echo " $queued_tor" | tee --append $LOG
mv -v $queued_tor $WATCH_DIR
i=$((i+1))
done
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment