Skip to content

Instantly share code, notes, and snippets.

@codyopel
Created August 25, 2014 00:16
Show Gist options
  • Save codyopel/4b16b737c8ed4048ff30 to your computer and use it in GitHub Desktop.
Save codyopel/4b16b737c8ed4048ff30 to your computer and use it in GitHub Desktop.
Television Episode Downloader
#!/bin/bash
# Filename: ezrss
# Version 091110
# Author: robz
# Download trackers from "EZrss" & monitor the "Torrents/Incomplete" folder.
# I have "unmetered" bandwidth in the small hours but dislike leaving my PC
# on all the time, this script suspends & resumes the PC within that timeslot.
# You will need "rtcwake" from the repositories and "shutdown" may need
# adding to the sudoers list.
# In a terminal (omitting all quote marks) do "sudo visudo", at the bottom of
# the listed items click Enter for a new line and paste this entry:
# "%user ALL=NOPASSWD: /sbin/shutdown" replacing "user" with your user name,
# click Escape and do ":wq" Enter to save and exit.
# Be careful editing sudoers as mistakes can cause problems.
# Ref: man visudo - read this if you're at all unsure ---- read it anyway.
# Use this line for your menu or panel launcher, alter to suit ---
# gnome-terminal --geometry 110x50+20+50 -x /home/"user?"/".scripts?"/ezrss
## Vars
DIRI=/home/$USER/Torrents/Incomplete/ # Incomplete downloads.
DIRC=/home/$USER/Torrents/Complete/ # Complete downloads.
FILE=/tmp/ezrss.file # Episodes list from EZrss.
TRKRS=/tmp/trackers.file # Episodes selected.
LOG=/home/$USER/Torrents/torrents.log # Logfile.
TFDLD=/home/$USER/Downloads/ # default .torrent dl dir.
IN=7200 # Default halt pc, in seconds.
SEED=600 # Default seed, in seconds.
EVERY=10 # Test dir every, in seconds.
SHOW_NAME_EXACT="" # Search leeway, leave blank.
QUALITY="hdtv" # Look for HDTV quality.
QUALITY_EXACT="true" # Enforce only hdtv not 720p.
UMDL=02:15 # Unmetered download start.
AT=$(date -d $UMDL +%s) # Date/time re-start - epoch.
ENDS=$(date -d 0645 +%s) # End time for unmetered.
PROG="transmission-gtk" # Bittorrent Client.
get_shows() ## Function search and select shows ---------------------------##
{
echo -e "\033[36;1mDownload television shows torrents from EZrss--\n"
tput sc; tput setaf 6
SHOW_NAME=""
while [ "$SHOW_NAME" = "" ]; do
read -p "Enter name of show: " SHOW_NAME; tput cuu1; done
SHOW_NAME=`echo $SHOW_NAME | sed -e 's/ \+/+/g'`
# Form "EZrss" URL for search page, get results.
SITE="http://ezrss.it/search/index.php?\
show_name="$SHOW_NAME"&\
show_name_exact=$SHOW_NAME_EXACT&\
date=&\
quality=$QUALITY&\
quality_exact=$QUALITY_EXACT&\
release_group=&\
mode=simple"
wget -q -O - $SITE | egrep -o "http:.*.torrent" | sort -u -r > $FILE
tput rc; tput el; tput setaf 7
nl $FILE # Number for easy selection.
echo; tput sc
ANS="0"
until [[ "$ANS" = "n" || "$ANS" = "q" ]]; do # Not n or q? get more shows.
tput setaf 6; tput el
read -p "Select an index number (q)uit (n)ext - press ENTER " ANS
if [[ "$ANS" != "n" && "$ANS" != "q" ]]; then
sed "$ANS"!d $FILE >> $TRKRS
tput setaf 7; tput rc; tput el
cat < $TRKRS
fi
done
if [ "$ANS" = "q" ]; then clean; fi # If quit clean temps & exit.
}
deferred_start() ## Function suspend and re-start later -------------------##
{
sudo /bin/true # Get root privileges.
if [ "$AT" -lt $(date +%s) ]; then # Is time set already gone?
AT=$((AT + 86400)) # If true then add a day.
fi
echo "Torrent log for "$(date +%a" "%x) >> $LOG
echo "Suspend PC start option chosen -------" >> $LOG
tput setaf 3
echo -e "To cancel - Ctrl+c within the next 10 seconds.\n"
tput setaf 7; sleep 10
AT=$(($AT - `date +%s`)) # Calc. wake in $AT seconds.
sudo killall rtcwake; clear # Kill any residual process.
poff -a # Stop ADSL.
sync && sudo rtcwake -u -s $AT -m on & # Set wake-up time.
sleep 2; sudo /usr/sbin/pm-suspend # Workaround rtcwake bug.
##### PC suspended here. Restart time is set with the "UMDL" in Vars above ##
xset dpms force off # Awake with VDU off.
CHECK=0; sleep 20; pon adsl; sleep 20 # Start & test for ADSL.
while ! ping -q -c 1 www.google.com > /dev/null && let "CHECK += 1"; do
if [ "$CHECK" -gt "18" ]; then # Ping for 180 seconds.
echo "Torrent log for "$(date +%a" "%x) >> $LOG
echo "ADSL failed to reconnect - bollocks!!" >> $LOG
haltpc # No internet? call shutdown.
fi
sleep 10
done
}
stats() ## Function download/upload stats ---------------------------------##
{
for n in {2..5}; do
RAW=`vnstat --dumpdb -i ppp0 | grep "h;$n;"`
UP=$(echo $RAW | cut -d ";" -f5)
DOWN=$(echo $RAW | cut -d ";" -f4)
let "DOWN_TOTAL += DOWN"
let "UP_TOTAL += UP" # Loop vnstat for traffic.
done
UP_TOTAL=$(echo "scale=2; $UP_TOTAL / 1024" | bc)
DOWN_TOTAL=$(echo "scale=2; $DOWN_TOTAL / 1024" | bc)
echo "Traffic for this session:" >> $LOG
echo -n "Up: "$UP_TOTAL"MiB | Down: "$DOWN_TOTAL"MiB | " >> $LOG
echo -n "Total: "$(echo "scale=2; $UP_TOTAL + $DOWN_TOTAL" | bc) >> $LOG
echo -e "MiB\nThese figures might also include the auto update cron job\n"\
"check the Update Manager for new downloads.\n" >> $LOG
}
haltpc() ## Function shutdown nicely -------------------------------------##
{
killall -w -q "$PROG" && stats
echo "Run completed, PC shutdown at: "$(date +%H:%M:%S) | tee -a $LOG
echo -e "\n==============================================================\n"\
>> $LOG
# "shutdown" needs to be added to sudoers list for next bit to work.
sync && sudo shutdown -h now
exit
}
clean() ## Function remove temporary files & quit script ------------------##
{
rm -f $TRKRS; rm -f $FILE; rm -f $TFDLD/*.torrent*; exit
}
## Start here ######
get_shows # Function get_shows.
ANS="0"
until [[ "$ANS" = "n" || "$ANS" = "q" ]]; do # Not n or q? then more shows.
tput sc; tput cuu1; tput el
echo -ne "\nAnother show? (y)es (q)uit (n)ext "
read ANS
if [ "$ANS" = "y" ]; then
clear
get_shows; ANS="0"
elif [[ "$ANS" != "n" && "$ANS" != "q" ]]; then
tput cuu1; tput el; echo "I don't understand your input"
sleep 2; tput cuu1
fi
done
if [ "$ANS" = "q" ]; then clean; fi # If quit clean temps, exit.
# Get tracker files
wget -q -nc -P $TFDLD -i $TRKRS
tput setaf 6; tput cuu1; tput el
echo -e "Tracker files have downloaded -----------------------------------\n"
rm -f $TRKRS; rm -f $FILE # Tidy up tmp files.
## Start now or suspend and resume at $UMDL.
tput bold; tput setaf 6;
ANS=" "
until [[ "$ANS" = "y" || "$ANS" = "q" || "$ANS" = "n" ]]; do
read -p "Suspend now and restart at $UMDL? (y)es (q)uit (n)ext " ANS
if [[ "$ANS" = "y" && `date +%s` -gt "$AT" && `date +%s` -lt "$ENDS" ]];
then
read -p "You're ADSL is unmetered! start now (q)uit (n)ext " ANS
fi
done
if [ "$ANS" = "q" ]; then
clean # If quit clean temps, exit.
elif [ "$ANS" = "y" ]; then
deferred_start # func deferred_start
else
echo "Download now option chosen -----" tee -a $LOG
fi
## Start Bittorrent client Transmission GUI.
tput bold; tput setaf 7; clear
echo "Torrent log for "$(date +%a" "%x) >> $LOG
# TODO
"$PROG" &
# Required parameters already set for GUI, but maybe the daemon or remote
# option would be a better choice to avoid potential config. conflicts.
## Wait for files to appear in /Incomplete, loop here until true.
while $(ls -A $DIRI); do
tput cup 0 0; tput smso
printf "\r%s"">>>>> Waiting for files in >>>>>\n$DIRI"
sleep 5
done
tput rmso; clear
## Write details of download to log.
tput bold; tput setaf 6
echo -e "Monitor \"$PROG's\" working directory for \n\
completion of currently downloading torrents.\n"
echo -n "Directory: " | tee -a $LOG
tput setaf 7
echo -e "\"$DIRI\"" | tee -a $LOG
tput setaf 6; tput sc
echo "Waiting for compliant peers"; sleep 180
tput rc
echo "Files found: " | tee -a $LOG
tput setaf 7
echo -e "$(ls -1 $DIRI)\n" | tee -a $LOG # List and log.
tput setaf 6
echo -e "PC will be shut down when seeding is complete"
echo -e "Run started at: "$(date +%H:%M:%S) | tee -a $LOG
DSET=$((`date +%s`+IN)) # Default shutdown from epoch.
echo -e "and will finish no later than: "$(date -d @$DSET +%H:%M:%S)"\n"
tput sc
echo -e "The directory is monitored at $EVERY""s intervals."
## Start timer
# Check /Incomplete dir. for content, if [time up or empty] then next.
CHECK=1
while [[ $CHECK*$EVERY -lt $IN && "$(ls -A $DIRI)" ]]; do
printf "\r%s""Working..............checks made so far: ""$CHECK "
sleep $EVERY
CHECK=$((CHECK+1))
done
echo
## If /Incomplete dir. is true then shutdown function.
if [[ "$(ls -A $DIRI)" ]]; then
tput setaf 1; tput rc
echo -e "--------------------------------------------------------------\n\
Default time limit reached torrents incomplete!\n\
$(ls -1 $DIRI)\n\
--------------------------------------------------------------" | tee -a $LOG
haltpc # shutdown function.
fi
## Torrents completed before default? then seed.
tput rc
echo -e "--------------------------------------------------------------\n\
Completed files transferred out at $(date +%T)" | tee -a $LOG
echo -n "to directory " | tee -a $LOG
tput setaf 7
echo -e "\"$DIRC\"\n\
--------------------------------------------------------------" | tee -a $LOG
echo -ne "\"$DIRI\""
tput setaf 6
echo -e " is now empty."
DIFF=$((DSET-`date +%s`)) # default time remaining.
if [ "$DIFF" -le "$SEED" ]; then
SEED=$DIFF
fi
echo -e "Torrent seeding time is set for $SEED"
## Seed time allocation.
if [ "$SEED" -gt "0" ]; then
echo -e "Torrent residual seed $SEED seconds" >> -a $LOG
tput setaf 1
for i in $(eval echo {$SEED..0}); do # count down from seed value.
printf "\r%s""This computer will shut down in: "$i
tput el
sleep 1
done
fi
echo
haltpc # shutdown function.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment