Mint Serena 64 bit using the Mate desktop environment. Most of the following scripts will run on this machine, although some have only been tested with the Gnome desktop. Extra software requirements can be met by the Ubuntu and safe PPA repositories. Scripts may be re-written or added to from time to time, the date serves as the version number. Syntax highlighting on these pages is provided by GVim's "Convert to HTML" option.
Showing posts with label television. Show all posts
Showing posts with label television. Show all posts

Clean Scrape


#!/bin/bash
# Filename: clean-scrape.sh
# Version.: 05082018
# Author..: robz
# Clean list of magnet links from "showRSS.info" page source scrape.
# Add to your file browser scripts directory for right click usage.
# Usage: In the web browser, right click - "View Page Source" - "Select All" 
# left click - drag to desktop - right click - run script

cd '/home/robz/Desktop'
[ -e 'dropped text.txt' ] || exit
sed '/1080p\|720p/d;/href="magnet/!d;s/.*href="//;s/" .*//' 'dropped text.txt' \
| sort -t= -k3 -o 'dropped text.txt'
mv 'dropped text.txt' 'add-magnet.txt'
exit

Report


#!/bin/bash
# Filename: report.sh
# Version: 170816
# Author: robz
# Report for newPVR.sh activity, HTML page served by nginx.
# Root crontab
# Update index.html page.
# @reboot /home/pi/Downloads/scripts/report.sh > /dev/null 2>&1
# Kill server.
# 00 18 * * * sudo /etc/init.d/nginx stop

boot="$(date -d "@"$(awk '/btime/{print $2}' /proc/stat) +"%H:%M:%S")"
stor="$(df -h / | awk 'NR==2 {print $5}')"
lst1="$(cat "/home/pi/Downloads/log/motd.txt")"
lst2="/home/pi/Downloads/scripts/followed-shows.txt"
lst2="$(grep -v '^$\|^\s*\#' "$lst2" | nl -w3 -s ')  ')"

cat > /usr/share/nginx/www/index.html << EOF
<!DOCTYPE html>
<html>
<head>
  <title>robz Report</title>
  <style>
    html {
      background: url(pi.jpg) no-repeat center center fixed;
      background-size: cover;
      -webkit-background-size: cover;
      -o-background-size: cover;
    }
    h1 {
      color: #BD1143; text-shadow: 2px 2px 1px #FFFFFF;
      font-size: 40px; font-style: italic;
    }
    h3 {
      font-style: italic; font-weight: 600;
      margin: 0;
    }
    body {
      color: #FFFFFF;
      font-family: sans-serif; font-size: 12px; font-weight: 500;
      margin-left: 25px;
    }
    .footer {
      color: #BD1143;
      font-size: 8px; font-style: italic; font-weight: 700;
      position: fixed;
      bottom: 5px; right: 5px;
    }
  </style>
</head>
<body>
<h1>The <img src="pi5.png" align="top" width="34" height="42"> Pi Torrent Box...
</h1>
<pre>
<h3> Download report for $(date +%A" "%d" "%B)</h3>
 Machine last rebooted at $boot - SD card usage is currently at $stor
<br>$lst1
<br> Tracked shows.:
<br>$lst2
</pre>
<span class="footer">Raspberry powered page by robz</span>
</body>
</html>
EOF



New PVR


#!/bin/bash
# Filename: new-pvr.sh
# Version.: 03042018
# Author..: robz
# WARNING! this works on my system it may break yours, read, check, tweak #####
# A torrent box script for a headless Raspberry Pi model B, it auto downloads
# media files for show episodes followed, then renames and transfers to NAS.
# Required software: xmllint, transmission-daemon and transmission-cli.
# Enable the "Watch" and "Incomplete" directories in transmission-daemon's
# settings.json, this can also be done using the web interface.
# http://<your-pi's-net-address-192.168.0.etc>:9091/transmission/web/#all
# No login required no gui, just power up pi whenever or leave on all the time.
# Downloads will occur at random times 01:00-05:00 ie. during off peak hours.
# List show names in the "followed-shows.txt" file one entry per line.
# Manually added links are copy/pasted to the "add-magnet.txt", one per line.
# Make script executable and run it first in /home/<user> i.e. ./new-pvr.sh
#
# Add to "sudoers" with sudo visudo, this, "pi ALL=(ALL) NOPASSWD: ALL". ######
# Backup your "/etc/fstab" then add these lines, leave out "#" on second line
# Allow mount of network drive using the samba protocol, but without sudo.
#//192.168.<etc>/Media  /home/pi/Media  cifs  guest,uid=pi,noauto,user  0  0
# Alter the NAS address (first one) to suit your network.

# Create a "Downloads" directory in home before running the script.
cd "$HOME/Downloads" || exit                        # Go to working directory.
# If script fails kill processes allowing cron to do a reboot.
trap "killall -q transmission-daemon; exit" SIGINT SIGTERM SIGHUP EXIT

# Setup variables.
smb="$HOME/Media/Cloud TV"                          # Samba mount point to NAS.
trk="$HOME/.config/transmission-daemon"             # The daemon's config file.
add='scripts/add-magnet.txt'                        # Any extra magnet links.
don='scripts/done-shows.txt'                        # Done downloads list.
rss='scripts/rss.txt'                               # Feed file rss listing.
rlg='log/run.log'                                   # This session, statistics.
dlg='log/debug.log'                                 # Debug log.
rec='log/rec.txt'                                   # Recently downloaded list.
mod='log/motd.txt'                                  # Message for ssh login.
ret='3'                                             # File retention in days.
cdn='0'                                             # Count/countdown variable.
export trk                                          # Give access for subshell.
###############################################################################
# On first run, this part of the script creates the directory/file structure 
# in /pi/Downloads, it also installs the random time run-script crontab.
if [ ! -e scripts/"$(basename $0)" ]; then
    set -o noclobber
    mkdir "/home/pi/Media"
    mkdir -p {Complete,Incomplete,scripts,log,ted}
    &> /dev/null > scripts/followed-shows.txt
    &> /dev/null > scripts/clipboard.txt
    &> /dev/null > scripts/recent.txt
    &> /dev/null > "$add"
    &> /dev/null > "$don"
    &> /dev/null > "$rss"
    t0='# Randomly between 01:00 and 05:00 new-pvr.sh run it.'
    t1='00 00 * * * /bin/sleep $(((RANDOM\%10800)+3600)); '
    t2="/home/pi/Downloads/scripts/$(basename $0); sudo shutdown -r +1"
    (crontab -u "$USER" -l; echo -e "\n$t0\n$t1$t2\n" ) | crontab -u "$USER" -
    cp "$0" scripts; mv "$0" "$0".backup
    echo 'Now add some shows to the followed-shows.txt'
    exit
else
    echo '### Files exist, no action taken ###'
fi
# On subsequent runs it does nothing, you can delete it if you like.
# NB. I noticed after my OS ugrade this section throws an error message, but it
# still seems to do it's job, so check file structure is in place before use.
###############################################################################
# Set up log files. 
mv "$dlg" "log/$(date -d y +%a-%-d%b)-debug.log"    # Rename old debug log.
mv "$rlg" "log/$(date -d y +%a-%-d%b)-run.log"      # Rename old session log.
set -x                                              # Generate debug output.
exec > "$dlg" 2>&1                                  # Route debug to debug.log.
date > "$rlg"                                       # Start a new session log.
> "$mod"                                            # Blank motd.txt.
# Timer will wait for "Incomplete" to empty or for a file to appear dependant
# on whether the $swh variable is set or not, or it'll just time out.
run_timer () {
while [ "$cdn" -gt "1" ]; do
    [[ ! "$swh" && "$(ls Incomplete)" ]] && break
    [[ "$swh" && ! "$(ls Incomplete)" ]] && break
    (( cdn-=5 )); sleep 5
done 2> /dev/null
}
# Strip unwanted characters from filenames, magnet links and xml input.
# 's/s[0-9]*e[0-9]*/\U&/;'\
strip_title () {
sed -e \
's/\([S][0-9]*[E][0-9]*\).*\<.*/\1/;'\
's/.*>//;'\
's/[.+-]/ /g;'\
's/ \+/ /g;'\
's/.*dn=//;'\
's/New.*: //;'\
's/www Torrenting com //;'\
's/www scenetime com //'
}
# Remove links with no proper episode number or with episode number 00.
no_specials ()
{
strip_title <<< "$lnk" | grep 'E[0-9][0-9]' | grep -v 'E00'
}
# Housekeeping, clean up old downloads and logs.
[ "$(df --output=pcent / | sed '1d' | cut -d'%' -f1)" -gt 70 ] && ret="1"
find log -mtime +14 -type f -delete
find Complete -mindepth 1 -mtime +"$ret" -delete
find "$trk" -mtime +"$ret" -name *.torrent -type f -delete -execdir bash -c\
    'del="{}"; del="${del##*/}"; rm -f "$trk/resume/${del%.*}.resume"' \;
# Fire up dat daemon.
transmission-daemon -g "$trk"
echo "$(date +%T) - Transmission-daemon started." >> "$rlg"
sleep 60
# Download rss feeds, pretty print to seperate lines allowing parsing.
wget -q -O - 'https://showrss.info/other/all.rss' | \
xmllint --format - > "$rss" 2> /dev/null
cat scripts/clipboard.txt >> "$rss"                 # Append earlier download.
# Check followed-shows list against new rss.txt file, standard def. not HD. If
# you want HD versions you'll have to modify the "sed" statments, alternatively
# you could use the "add-magnet.txt" file for a one off.
## NB. GNU sed is being used in this script, BSD/POSIX versions won't do.  ####
if [ -s "$rss" ]; then
    while read -r shw; do
        if [[ "${shw:0:1}" != "#" && "$shw" != "" ]]; then
            epi=$(sed -n "/720p\|1080p/d;/descrip.*\b$shw/Ip" "$rss")
            [ -z "$epi" ] && continue
            while read -r lnk; do
                if ! grep -iq "$(strip_title <<< "$lnk")" "$don"; then
                    lnk="$(sed 's/.*href=\"//;s/\".*//' <<< "$lnk")"
                    if no_specials; then
                        echo "$lnk" >> "$add"
                        strip_title <<< "$lnk" >> "$don"
                    fi
                fi
            done <<< "$epi"                         # Add to add-magnet.txt
        fi
    done < 'scripts/followed-shows.txt'
    sed 's/.*E\([0-9][0-9]*\)/\1 &/' "$add" | sort -u -o "$add"
    sed -i 's/.* //' "$add"
    #sort -t "=" -k3 -u -o "$add" "$add"
    grep "raw.*title" "$rss" | sed "/720\|1080/d" | strip_title | sort -u \
    > log/rss-today.txt
else
    echo 1 > log/flashred.txt
    echo ' ## ERROR - rss feed failed to download. ##' | tee -a "$rlg" "$mod"
    > log/rss-today.txt
fi
# Load the bit torrent client with links from the "add-magnet.txt" file.
while read -r lnk; do
    [ "$cdn" -ge "3" ] && break
    (( cdn+=1 ))
    transmission-remote --add "$lnk"
    strip_title <<< "$lnk" >> "$don"
    sed -i '1,1d' "$add"
done < "$add"
sort -u "$don" -o "$don"                            # Tidy up done-shows list.
# Wait for first link to load, skip if incomplete files exist.
[ "$cdn" -ne "0" ] && \
(echo "$(date +%T) - Wait for links or resume incompletes." >> "$rlg";
cdn="420"; swh=""; run_timer)                       # Set to wait for link load.
# If torrents are present start session timer.
if [ "$(ls Incomplete)" ]; then
    transmission-remote --torrent all --start
    cdn="3600"; swh="1"
    echo "$(date +%T) - Proceeding with download $((cdn/60))m max." >> "$rlg"
    run_timer
    echo "$(date +%T) - Seed for another 10 minutes." >> "$rlg"
    sleep 600
    transmission-remote --torrent all --stop
    transmission-remote --session-stats | sed '3,6!d' >> "$rlg"
    (
    echo "$(date +%T) - End of $(date +%A)'s downloads."
    [ "$(ls Incomplete)" ] && echo 'Incomplete download(s):' && ls Incomplete
    ) >> "$rlg"
    echo " PVR session...: $(awk '/Downloaded/{print $1,$2$3}' "$rlg")" >>"$mod"
    echo 'Rename & transfer to network storage.' >> "$rlg"
fi
transmission-remote --exit
# Just the main video files are selected ignoring previews and promos.
# Create directories if needed on NAS and copy files there, renaming on the way.
cdn=0; new="$(find Complete -size +50M -daystart -mtime -1)"
if [ "$new" ]; then                                 # Do we have episodes.
    mount '/home/pi/Media'
    sleep 45
        while read -r vid; do
        ext="${vid##*.}"
        epi="$(strip_title <<< $(dirname "$vid" | xargs basename -a))"
        dir="${epi% [Ss][0-9]*}/Season $(tail -c 6 <<< $epi |\
        head -c 2 | sed 's/^0//')"                  # Determine directory tree.
        echo "$epi" | sed 's/^/  /' | tee -a "$rlg" "$mod" |\
        sed "s/^/$(date +%a-%d)/;s/$/  \:$(du --si "$vid" | cut -f1)/" >> "$rec"
        mkdir -p "$smb/$dir"                        # Create directory on NAS.
        cp "$vid" "$smb/$dir/$epi.$ext"             # Copy to NAS renamed.
        (( cdn+=1 ))                                # Increment LED indicator.
    done <<< "$new"
fi
# This works in conjunction with the "ted-talks.sh" script if it exists.
# The mount commands will need tailoring to your network setup. ###############
new="$(find ted -type f)"
if [ "$new" ]; then
    echo " TED session...: Downloaded "$(du --si ted | cut -f2)"B" |\
    tee -a "$rlg" "$mod"
    while read -r vid; do
        [ -e "$vid" ] && mv "$vid"\
        ted/"$(grep "${vid##*/}" scripts/ted-donelist.txt | cut -d"|" -f1-2 |\
        tr -cd [:alnum:][:blank:]).mp4"             # Make a sane filename.
        (( cdn+=1 ))
    done <<< "$new"
    ls ted | sed 's/^/  /;s/.mp4//' | cut -c1-77 | tee -a "$rlg" "$mod"
    if ! mount -l | grep $(dirname "$smb"); then
        mount '/home/pi/Media'
        sleep 45
    fi
    mv ted/* "$smb"/TED-talks
fi
# Finish up.
echo "$cdn" > 'log/flashblue.txt'                   # Flash LED $cdn times.
echo "$(tail -25 "$rec")" | tac > 'scripts/recent.txt'
if [ "$cdn" -lt "1" ]; then
    echo " This session..: $(date +%A) nothing new." | tee -a "$rlg" "$mod"
else
    echo "$(date +%T) - File(s) transfer completed." >> "$rlg"
    (
    echo -n " Script runtime: $(date -d@"$SECONDS" -u +%H:%M:%S) - "
    echo "Load average: $(awk '{print $1,$2,$3}' /proc/loadavg)"
    ) | tee -a "$rlg" "$mod"
    umount '/home/pi/Media'
fi
# exit - see trap.


LED flash


#!/bin/bash
# Filename: countLED.sh
# Version: 270716
# Author: robz
# Raspberry Pi script, flashes an LED number in "flash.txt" times. 
# GPIO pin 18 flash for new-pvr.sh, flashes number of shows downloaded.
# Requires an LED on pin 18 with a 470ohm resistor to a ground pin.
# An alternative is to use the green activity LED, comment code appropriately.
# Sudo crontab entries for this to work with new-pvr.sh:
# @reboot /home/pi/Downloads/scripts/countLED.sh > /dev/null 2>&1
# 00 18 * * * /usr/bin/pkill countLED.sh > /dev/null 2>&1

cdn="$(cat /home/pi/Downloads/scripts/flash.txt)"   # Current number of shows.
[ "$cdn" -lt "1" ] && exit

cleanup ()
{
echo "18" > /sys/class/gpio/unexport
# echo "mmc0" > /sys/class/leds/led0/trigger
}

trap 'cleanup > /dev/null 2>&1; exit' SIGTERM SIGHUP ERR EXIT

echo "18" > /sys/class/gpio/export                  # Activate GPIO pin 18.
echo "out" > /sys/class/gpio/gpio18/direction       # Designate as an output.

while true; do                                      # Continuous loop.
    for ((n=1; n<=cdn; n++)); do                    # Count shows loop.
        echo 1 > /sys/class/gpio/gpio18/value       # GPIO 18 high, LED on.
      # echo 1 > /sys/class/leds/led0/brightness    # Activity LED on.
        sleep .1
        echo 0 > /sys/class/gpio/gpio18/value       # GPIO 18 low, LED off.
      # echo 0 > /sys/class/leds/led0/brightness    # Activity LED off.
        sleep .2
    done
    sleep 4
done



TED Talks


#!/bin/bash
# Filename: ted-talks.sh
# Version: 040716
# Author: robz
# A raspi script for downloading TED talks video using off peak bandwidth.
# Used as part of the interactive motd.sh script or just run in a terminal.
# You may need to "apt-get install at" this will also install heirloom-mailx
# packages but these can be removed afterwards if you wish without affecting
# the operation of the "at" utility.
#
dwn="$HOME/Downloads"
ted="$dwn/ted"                                      # Downloaded media here.
rss="$dwn/scripts/ted-rss.txt"                      # Feed location.
don="$dwn/scripts/ted-donelist.txt"                 # Shows already downloaded.
num="29"                                            # Number of choices in list.
# Get rss feed, refresh weekly.
if [[ ! -e "$rss" || $(find "$rss" -mtime +7) ]]; then
    echo "please wait - downloading new feed."
    wget -q -O - "https://www.ted.com/talks/rss" > "$rss"
fi
# Display choices list. (suits an 80 column terminal) 
lst=$(grep "<title" "$rss" | sed -e 's/<[^>]*>//g;1,2d;s/^[ \t]*//' | \
grep -v "$(sed 's/|.*//' < "$don")")                # Make list, exclude done.
top=$(sed q <<< "$lst")                             # Extract newest not viewed.
lst=$(grep -v "$top" <<< "$lst")                    # Remove duplicate entry.
lst=$(shuf -n"$num" <<< "$lst")                     # Shuffle the rest of list.
lst=$(echo -e "$top\n$lst")                         # Put newest talk at top.
echo " Popular TED talks."
echo -e "\n$lst\n" | nl -w3 -s ') ' | cut -c -79    # Number and trim to width.
echo
# Choose your TED talk.
read -p " Type in number - Enter, or just enter to cancel: " ans
# Reject nonsense inputs.
if [[ "$ans" -gt "$(( num+1 ))" || "$ans" -lt "1" ]]; then
    echo " Incorrect input, Hit enter."
    exit
fi
# Parse information for donelist and download.
tlk=$(echo "$lst" | sed "${ans}q;d")
echo -n "$tlk| " >> "$don"                          # Add title to donelist.
echo -e "\n $tlk"
tlk=$(grep -A4 "$tlk" "$rss" | grep "<enclosure" | \
sed 's/<.*url=\"//;s/\".*>//;s/^[ \t]*//')          # Parse url to download.
# Set job content and time to download.
echo "wget --content-disposition -P "$ted" "$tlk"" | at 01:11 > /dev/null 2>&1
echo " $tlk" | tee -a "$don"                        # Echo url & add to done.
echo -e "$(atq)\n Pending @ 01:11"                  # Query "at" to check job.
exit


Basic PVR

#!/bin/bash
# Filename: mypvr.sh
# Version: 081211
# Author: robz
# This script uses http://ezrss.it to check for the lastest episodes of your
# favorite tv shows, which you'll have listed in the "followed-shows" file.
# Run mypvr.sh from your chosen directory. First, the above list file will be
# set up, once this is done the script can be controlled via a crontab or on
# PC startup, or both. Here's a crontab to check for new shows every 2 hours.
# 25 */2 * * * export DISPLAY=:0 && /$HOME/PVR/mypvr.sh >/dev/null 2>&1
# Selected .torrent trackers will be sent to your "Downloads" directory ready
# to be picked up by "Transmission" gui/daemon or any other client that can be
# set to auto-load from a monitored directory.

SHOW_NAME_EXACT=""                  # Allow some search leeway.
QUALITY="hdtv"                      # HDTV standard quality.
QUALITY_EXACT="true"                # Only hdtv not 720p - no huge files.
DWNLDS="$HOME/Downloads"            # Default Downloads, change if you like.
green="<span color=\"#008000\">"    # Green ok! :p

func_form_url ()                                    # Create web search url.
{
WEBSITE="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"
}

cd
"$(dirname "$0")"                                # Do cd to "mypvr.sh" dir.

trap "rm -f "newlist" "urllist"; exit" \
INT TERM EXIT                                       # Clean up cruft on close.

if [ ! -e "donelist" ]; then touch donelist; fi     # Check donelist exists.
if [ ! -e "followed-shows" ]; then                  # Check shows list exists.
cat > followed-shows << EOF
# Type the names of tv shows to be followed into this list.
# Hash commented, like this line, and blank lines will be ignored.
# To stop a show search, comment "#" the line or delete it :)

Spooks
#Terra Nova < examples, delete and replace with yours.
Dexter

# End of list - a blank line is required above this line.
EOF
gedit followed-shows                                # Edit new shows list.
exit; fi

while read SHOW_NAME; do                            # Read followed-shows list.
    if [[ "${SHOW_NAME:0:1}" != "#" && -n "$SHOW_NAME" ]]; then
        SHOW_NAME=$(echo "$SHOW_NAME" | sed -e 's/ \+/+/g')
        func_form_url                               # Create custom url.
        # Get web pages for shows, take episode at top of page - newest.
        wget -q --random-wait -O - $WEBSITE |\
        egrep -om1 "http:.*.torrent" >> urllist
    fi
done < followed-shows                               # Feed followed-shows.

sort -u urllist > newlist                           # De-dupe, make copy.

# Whittle away the URL bumf for a naked "newlist".
sed -i 's#http:/[^",]*/##' newlist
sed -i 's/\([Ee][0-9][0-9]\)\+.*/\1/' newlist       # S01E02 - s01e02.
sed -i 's/\([Xx][0-9][0-9]\)\+.*/\1/' newlist       # 1X02 - 1x02 - 01X02.
#sed -i 's/\([0-9][0-9][0-9]\)\+.*/\1/' newlist     # 102 ? (V2009)?! aah!
sed -i '/torrent/d' newlist                         # Remove strays.

sort newlist -o newlist                             # Sort stripped list.

NEW=$(comm --check-order -23 newlist donelist)      # Compare new/existing.
if [ -z "$NEW" ]; then exit; fi                     # $tring null? then exit.

for LINE in $NEW; do SEL+=" TRUE $LINE"; done       # Prepend entries "TRUE".

# Option for 10 mins to choose/cancel or it auto-downloads all new trackers.
CHOOSE=$(zenity --title "PVR" --timeout 600 --width 300 --height 225 \
--list --text "$green""There are new TV episodes available</span>" \
--checklist --column " #" --column "Download torrent trackers for:" $SEL)
EXITCODE="$?"

if [ "$EXITCCODE" = 1 ] || [[ "$EXITCODE" = 0 && -z "$CHOOSE" ]]; then
    exit                                            # Null entry or cancel.
elif [ -z "$CHOOSE" ]; then                         # $CHOOSE empty, use $NEW.
    CHOOSE=$(echo $NEW | sed 's/  */|/g')           # Add "|" egrep delimiters.
    zenity --notification --text "Auto download of latest TV trackers"\
    & disown
fi

timeout 60 \
wget --quiet --no-clobber --random-wait --directory-prefix=$DWNLDS \
--input-file=$(egrep $(echo $CHOOSE) urllist)       # Go "wget" the trackers.

CHOOSE=$(echo "$CHOOSE" | sed 's/|/ /g')            # Strip delimiters.

# If tracker has downloaded correctly, make entry to "donelist".
for LINE in $CHOOSE; do
    if [ $(ls "$DWNLDS" | grep $LINE) ]; then echo "$LINE" >> donelist
    elif [ -z $PID ]; then zenity --notification\
        --text="`echo -e "One or more trackers has failed to download.\
        \nNext try is scheduled for $(date --date='+2 hours' +%H:25) hours."`"\
        & disown
        PID=$!                                      # Just one notification.
    fi
done

sort donelist -o donelist                           # Sort new "donelist".

notify-send -i '/usr/share/icons/hicolor/scalable/apps/transmission.svg' \
"Tracker files downloaded" "$(ls "$DWNLDS" | grep "\.torrent")" &