WolfPack/wolfpack

250 lines
9.2 KiB
Bash
Executable File

#!/bin/bash
source /usr/local/src/SharedLibraries/Bash/header
if [ -z "$3" ]; then
configuretorrent /usr/local/etc/WolfPack/torrentengine
else
configuretorrent "$3"
fi
export IFS="
"
export logfile='/var/log/wolfpack.log'
function helptext {
echo '
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/\____ xxxxxxxxxxxxx AniNIX::WolfPack
// ~ / _\_____ Let loose the wolves to hunt, run,
/ \ .. \/ and be free. They shall repay you
// /~_____/ with their prey.
/// \\/ / ~dev@aninix.net
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wolfpack --alpha [enginefile] ~~ Tell the alpha to send the
pack members hunting.
wolfpack --member <pup> [enginefile] ~~ Send a member to train a pup
and bring back results.
wolfpack --help ~~ Show this helptext
Available pup types (all pups are line-delimited with the type and
result location as the first two lines):
torrent ~~ Torrent the result of the
search term.
Will remove search line on
success.
shows ~~ Download the episode in a
show and increment. Shows
are listed in format
S01E01#Show terms
download ~~ Takes a list of commands to
run to arrive at a URL to
grab and downloads the last
URL.
search ~~ Uses the Google search
engine to return the top
five results for the search
terms on each line.
'
}
function privacycheck {
if [ "$(whatismyip)" == "$(nslookup aninix.net | grep Address | cut -f 2 -d ' ' | tail -n 1)" ]; then
echo "ERROR: Should not torrent directly from the AniNIX main IP for privacy reasons" 2>&1 | tee -a "$logfile"
exit 1
fi
}
function nextairdate {
if [ -z "$1" ]; then
echo "Need a show to search for"
exit 1;
fi
lynx -read_timeout=5 -connect_timeout=5 --dump "https://www.episodate.com/tv-show/$(echo $1 | tr '[:upper:]' '[:lower:]' | tr ' ' '-')?season=99" 2>&1 | grep 'and airs on' | sed 's# Season [0-9]\+ / Episode [0-9]\+ and airs on ##'
return
}
function transmissioncheck {
if [ "$(pgrep -afc transmission-cli)" -ne 0 ]; then
echo "Transmission is still running. Aborting." | tee -a "$logfile"
exit 0;
fi
}
function torrent {
if [ "$1" == "" ] || [ $(wc -l "$1" | cut -f 1 -d ' ') -lt 3 ]; then
echo "Need a valid argument."
return;
fi
transmissioncheck;
privacycheck;
export downloaddir=$(head -n 2 "$1" | tail -n 1)
if [ "$(pgrep -afc transmission-cli)" -ne 0 ]; then
echo "Transmission is still running. Aborting." | tee -a "$logfile"
return;
fi
for i in $(tail -n +3 "$1"); do
getmagnetlink "$i"
if [ "$magnetlink" != "" ]; then
/usr/bin/transmission-cli -d 600 -u 1 -er -w "$downloaddir" -b -f "/usr/local/bin/wolfpack-stoptorrent" "$magnetlink"
sed -i "/$i/d" "$1"
echo Downloaded and removed $i | tee -a "$logfile"
fi
done
if [ -x `which clamscan` ]; then clamscan -r -i "$downloaddir" | tee -a "$logfile"; fi
}
function shows {
if [ "$1" == "" ] || [ $(wc -l "$1" | cut -f 1 -d ' ') -lt 3 ]; then
echo "Need a valid argument."
return;
fi
transmissioncheck;
privacycheck;
export downloaddir=$(head -n 2 "$1" | tail -n 1)
echo "Would download to "$downloaddir
for i in $(tail -n +3 "$1"); do
sepisode=$(echo $i | cut -f 1 -d '#' | xargs)
showName="$(echo $i | cut -f 2 -d '#' | xargs)"
modifier="$(echo $i | cut -f 3 -d '#' | xargs)"
season=$(echo $sepisode | cut -f 1 -d 'E' | cut -f 2 -d 'S')
episode=$(echo $sepisode | cut -f 2 -d 'E')
newep=$(printf "%02d\n" $((${episode#0} + 1)))
newseason="$(printf "%02d\n" $((${season#0} + 1)))"
if [ -z "$modifier" ]; then
getmagnetlink "$showName $sepisode"
else
getmagnetlink "$showName $sepisode $modifier"
fi
if [ "$magnetlink" != "" ]; then
# We found an episode.
echo $i | tee -a "$logfile"
/usr/bin/transmission-cli -d 300 -u 1 -er -w "$downloaddir" -b -f "/usr/local/bin/wolfpack-stoptorrent" "$magnetlink"
sed -i "s/$i/S${season}E${newep}#${showName}#${modifier}/" "$1"
echo Downloaded and pup updated. | tee -a "$logfile"
rm -Rf ~/.config/transmission/resume/*
rm -Rf ~/.config/transmission/torrents/*
rm -f "$downloaddir"/*/*.txt # Including this to avoid breaking Yggdrasil
rm -f "$downloaddir"/*/*.nfo
echo | tee -a "$logfile"
else
# We didn't -- is there a new season?
if [ -z "$modifier" ]; then
getmagnetlink "$showName S${newseason}E01"
else
getmagnetlink "$showName S${newseason}E01 $modifier"
fi
if [ "$magnetlink" != "" ]; then
echo "S${newseason}E01" $showName | tee -a "$logfile"
/usr/bin/transmission-cli -d 600 -u 1 -er -w "$downloaddir" -b -f "/usr/local/bin/wolfpack-stoptorrent" "$magnetlink"
sed -i "s/$i/S${newseason}E01#$showName#$modifier/" "$1"
else
nextAirDate="$(nextairdate "$showName")"
if [ ! -z "$nextAirDate" ]; then
echo "No new season found for $showName -- next airs $(nextairdate "$showName")" | tee -a "$logfile"
if [ "$(date -s "$nextAirDate" -u +%s)" -lt "$(date -u +%s)" ]; then
echo ERROR: Should have found a new episode for $searchterms and did not -- check your provider\! | tee -a "$logfile"
fi
else
echo "No new season found for $showName" | tee -a "$logfile"
fi
fi
fi
done
if [ -x `which clamscan` ]; then clamscan -r -i "$downloaddir" | tee -a "$logfile"; fi
}
function download {
if [ "$1" == "" ] || [ $(wc -l "$1" | cut -f 1 -d ' ') -lt 3 ]; then
echo "Need a valid argument."
return;
fi
# Second line in the file is the directory to download to.
export downloaddir=$(head -n 2 "$1" | tail -n 1)
# The third line and any following lines should be
for i in $(tail -n +3 "$1"); do
if [ -z "$i" ]; then continue; fi
export comm=$(echo $i | sed "s#PREVRESULT#$nexturl#g" -)
export nexturl=$(bash -c "$comm")
done
export filename="$downloaddir"/"$(echo "$nexturl" | rev | cut -f 1 -d \/ | rev)"
#if file doesn't already exist....
if [ ! -f "$filename" ]; then
echo Downloading $nexturl to $filename ... | tee -a "$logfile"
export dedupterm="$(echo $1 | rev | cut -f 1 -d '/' | rev | cut -f 1 -d '.')"
for i in $(find -type f "$downloaddir" | grep -i "$dedupterm"); do
echo Deduped! Removed $i from $downloaddir | tee -a "$logfile"
rm -f "$downloaddir"/"$i" 2>&1 | tee -a "$logfile"
done
wget --timeout=5 -q -O "$filename" "$nexturl"
echo Downloaded | tee -a "$logfile"
fi
if [ -x `which clamscan` ]; then clamscan -r -i "$downloaddir" | tee -a "$logfile"; fi
}
function search {
if [ "$1" == "" ] || [ $(wc -l "$1" | cut -f 1 -d ' ') -lt 3 ]; then
echo "Need a valid argument."
return;
fi
export downloaddir=$(head -n 2 "$1" | tail -n 1)
for i in $(tail -n +3 "$1"); do
export searchstring=$(echo $i | sed 's/ /+/g')
export file="$downloaddir"/$searchstring'.txt'
echo | googler --count=5 -C -x "$searchstring" | grep -v 'Enter n, p, result' > "$file"
echo "Ran search for $searchstring to $file" | tee -a "$logfile"
done
}
if [ "$1" == "" ]; then
helptext;
exit 1;
fi
echo '---' $(date) '---' | tee -a "$logfile"
case "$1" in
"--alpha")
echo Started alpha on `uname -n` | tee -a "$logfile"
if [ "$2" == "" ]; then
export basedir="/usr/local/etc/WolfPack"
else
export basedir="$2";
fi
echo "Starting members from pup in "$basedir | tee -a "$logfile"
for i in $(find "$basedir" -type f | grep '.pup'); do
/usr/local/bin/wolfpack --member "$i" "$3";
done
;;
"--member")
echo "Spawning a $(head -n 1 "$2") member for $2 on `uname -n`" | tee -a "$logfile"
(printf "IP: "; whatismyip) | tee -a /var/log/wolfpack.log
case "$(head -n 1 "$2")" in
"shows")
shows "$2"
;;
"torrent")
torrent "$2"
;;
"search")
search "$2"
;;
"download")
download "$2"
;;
*)
echo "Bad pup type."
;;
esac
;;
"--reset-log")
cat /dev/null > "$logfile"
exit 0;
;;
*)
helptext
;;
esac
echo "Exited "$1 | tee -a "$logfile"
echo '---' "$(date)" '---' | tee -a "$logfile"
echo | tee -a "$logfile"