#!/bin/bash
logger "CHECK_TORRENT: Start"
WATCH_FOLDER="/mnt/wd/torrent/source/serials"
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
MAIL_TO="mail@example.com"
MAIL_FROM="torrents_checker@example.com"
NOTIFY_BY_MAIL="true"
rm $DIR/cookies.txt
wget --keep-session-cookies --save-cookies=$DIR/cookies.txt "http://login.rutracker.org/forum/login.php" --post-data="login_username=you_name&login_password=your_password&login=Вход" -O - >/dev/null 2>/dev/null
while [ -h "$SOURCE" ] ; do SOURCE="$(readlink "$SOURCE")"; done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
for i in $(ls -1 $WATCH_FOLDER|grep -E '^\[rutracker.org\]\.t[0-9]*\.torrent$'|awk '{ gsub(/[[:alpha:]]|[[:punct:]]/," ")}1'); do
wget --keep-session-cookies --load-cookies=$DIR/cookies.txt --referer='http://rutracker.org/forum/viewtopic.php?t='$i --header='Content-Type:application/x-www-form-urlencoded' --header='t:'$i --post-data='t='$i -O /tmp/check_torrent_$i.torrent http://dl.rutracker.org/forum/dl.php?t=$i >/dev/null 2>/dev/null
torrentinfo -n /tmp/check_torrent_$i.torrent >/dev/null 2>/dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
if [ $(md5sum "/tmp/check_torrent_$i.torrent" | cut -d " " -f1) != $(md5sum "$WATCH_FOLDER/[rutracker.org].t$i.torrent" | cut -d " " -f1) ]; then
logger "CHECK_TORRENT: New version found for torrent [rutracker.org].t$i.torrent. Updating..."
mv -f /tmp/check_torrent_$i.torrent $WATCH_FOLDER/[rutracker.org].t$i.torrent
if [ $NOTIFY_BY_MAIL == "true" ]; then
torrentinfo -f -n $WATCH_FOLDER/[rutracker.org].t$i.torrent>/tmp/msg.txt
nail -r $MAIL_FROM -s "New series are availiable: `torrentinfo -n $WATCH_FOLDER/[rutracker.org].t$i.torrent |grep name|cut -c20-`" -S "your.smtp.relay" $MAIL_TO < /tmp/msg.txt
rm /tmp/msg.txt
fi
else
rm /tmp/check_torrent_$i.torrent
logger "CHECK_TORRENT: No new versions for torrent [rutracker.org].t$i.torrent. Skipping..."
fi
else
# rm /tmp/check_torrent_$i.torrent
logger "CHECK_TORRENT: Something is wrong with RUTRACKER.ORG. Exiting..."
fi
done
logger "CHECK_TORRENT: Stop"
schedule = watch_directory_1,5,5,"load_start=/mnt/wd/torrent/source/serials/*.torrent,d.set_directory=/path/where/to/save/files"