Last active
December 23, 2015 23:59
-
-
Save agassiyzh/6713607 to your computer and use it in GitHub Desktop.
Put transmission torrent to current place
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 | |
HOST=localhost | |
PORT=9091 | |
RPC_AUTH=0 | |
USER=username | |
PASS=password | |
############################################################################ | |
# CONSTANTS | |
############################################################################ | |
BASE_NAME=checkTargetDirTransmission | |
ALL_FILE="/tmp/$BASE_NAME.all" | |
TMP_FILE="/tmp/$BASE_NAME.tmp" | |
TMP_FILE2="/tmp/$BASE_NAME.tmp2" | |
LIST_FILE="/tmp/$BASE_NAME.list" | |
LOCK_FILE="/tmp/$BASE_NAME" | |
MOVIE_DIR="/shares/Public/Movies" | |
SERIES_DIR="/shares/Public/Series" | |
TARGET_DIR="" | |
############################################################################ | |
# FUNCTIONS | |
############################################################################ | |
callTransmission () { | |
if [ "$RPC_AUTH" -eq 0 ] | |
then | |
transmission-remote "$HOST":"$PORT" "$@" | |
else | |
transmission-remote "$HOST":"$PORT" -N "$TMP_FILE" "$@" | |
fi | |
# Since transmission 1.70 exit code can be used to detect errors | |
if [ $? -ne 0 ] | |
then | |
echo "Error while accessing Transmission daemon" >&2 | |
exitAndClean 3 | |
fi | |
} | |
# Remove lock and temporary files, exit with code $1 | |
exitAndClean () { | |
[ -z "$LOCK_PID" ] || kill "$LOCK_PID" | |
lockfile-remove "$LOCK_FILE" | |
rm -f "$TMP_FILE" "$ALL_FILE" "$LIST_FILE" "$TMP_FILE2" "$ERROR_FILE.new" | |
exit "$1" | |
} | |
# Remove torrents that deleted from downloads list from specified list file | |
# $1 -- list name | |
removeDeletedTorrents() { | |
# remove deleted torrents from sent notifications list | |
sort "$1" > "$TMP_FILE" | |
mv "$TMP_FILE" "$TMP_FILE2" | |
sort "$ALL_FILE" > "$TMP_FILE" | |
mv "$TMP_FILE" "$ALL_FILE" | |
comm -1 -2 "$TMP_FILE2" "$ALL_FILE" > "$TMP_FILE" | |
mv "$TMP_FILE" "$1" | |
} | |
rename () { | |
name=$1 | |
if [[ -n `echo $name| grep -Ei "s[0-9]+e[0-9]+"` ]]; then | |
SE=`echo $name | sed -r 's/.*(s[0-9]+e[0-9]+).*/\1/I'` | |
NAME=`echo $name | sed -r 's/[^a-z]*([a-z\. 0-9]*)\.(s[0-9]+e[0-9]+).*/\1/I'` | |
season=`echo $SE | sed -r 's/s([0-9]+)e.*/\1/I'` | |
TARGET_DIR="${SERIES_DIR}/${NAME}/Season $season" | |
else | |
TARGET_DIR="$MOVIE_DIR" | |
fi | |
} | |
############################################################################ | |
# MAIN | |
############################################################################ | |
CONFIGPATH="$HOME/.$BASE_NAME" | |
RENAME_FILE="$CONFIGPATH/renamed" | |
ERROR_FILE="$CONFIGPATH/error" | |
umask 077 | |
lockfile-create "$LOCK_FILE" || (echo "Unable to lock lockfile!" >&2; exitAndClean 2) | |
lockfile-touch "$LOCK_FILE" & | |
LOCK_PID="$!" | |
MAIN_PID="$$" | |
trap "exitAndClean 1" HUP INT QUIT KILL | |
touch "$RENAME_FILE" "$ERROR_FILE" "$ERROR_FILE.new" "$TMP_FILE" | |
printf "" > "$ALL_FILE" | |
# generate netrc file for RPC authorisation | |
if [ "$RPC_AUTH" -eq 0 ]; then | |
printf "machine %s\nlogin %s\npassword %s\n" "$HOST" "$USER" "$PASS" > "$TMP_FILE" | |
fi | |
#main | |
callTransmission -l > "$TMP_FILE2" || exitAndClean 1 | |
gawk '{ | |
if ($1 != "Sum:" && $1 != "ID") { | |
print $1,$2 | |
} | |
}' "$TMP_FILE2" > "$LIST_FILE" | |
while read id percent; do | |
if [ ! $percent = "n/a" ]; then | |
id="`echo "$id" | sed 's/\*//'`" | |
reply="`callTransmission -t "$id" -i | grep -E '^ [A-Z]'`" | |
[ $? -eq 0 ] || exitAndClean 1 | |
name="`echo "$reply" | grep '^ Name' | cut -c 9-`" | |
hash="`echo "$reply" | grep '^ Hash' | cut -c 9-`" | |
error="`echo "$reply" | grep '^ Error' | cut -c 10-`" | |
trackerError="`echo "$reply" | grep '^ Tracker gave an error' | cut -c 26-`" | |
grep -q "$hash" "$RENAME_FILE" | |
if [ $? = 1 -a ! "$percent" = "100%" ]; then | |
rename "$name" | |
callTransmission -t "$id" --move "$TARGET_DIR" | |
echo $hash >> "$RENAME_FILE" | |
fi | |
echo $hash >> "$ALL_FILE" | |
fi | |
done < "$LIST_FILE" | |
removeDeletedTorrents "$RENAME_FILE" | |
exitAndClean 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment