#! /bin/bash
########################################################################
## Grabs newest movie trailers off http://www.apple.com/trailers and ##
## saves them on the disk for viewing. ##
## ##
## by Kevin Welch, 2005 ##
########################################################################
# move to proper directory
PWD=`pwd`
cd /home/kevin/sbin/trailers
if [ -z "$1" ]; then
# grab fresh copy of the trailer list for screenscraping
echo "Grabbing fresh page..."
wget -q http://www.apple.com/trailers/ -O trailers.html
# scrape trailers.html for the "Newest Trailers" and "Exclusive Trailers"
# columns and strip HTML:
# resulting list is in format "DATE \t URL \t TITLE"
echo "Filtering links..."
sed -nr -e "
/Trailers Exclusive/,/class=\"clearer\"/ {
s/^\t*//
s/<\/?div[^<]*>//g
s/<\/?h[1-4]>//g
s//\t/
s/<\/a>.*//
p
}
" -e "
/Newest Trailers/,/class=\"clearer\"/ {
s/^\t*//
s/<\/?div[^<]*>//g
s/<\/?h[1-4]>//g
s//\t/
s/<\/a>.*//
p
}
" trailers.html | egrep ^[0-9]{2}[.][0-9]{2} | awk '{print $2}' > trailerlist.tmp
elif [ -e $PWD/$1 ]; then
# argument is filelist
cp "$PWD/$1" trailerlist.tmp
elif [[ "$1" == http* ]]; then
# argument is url
echo "$1" > trailerlist.tmp
else
# Dunno what it is
echo "Parameter not recognized."
exit 1
fi
# loop through trailer list
echo "Grabbing trailers..."
numtrailers=`wc -l trailerlist.tmp | awk '{print $1}'`
numhistory=0
for url in `cat trailerlist.tmp`
do
# set temp short name for file
name=`echo $url | sed -e 's/[.]html$//' | awk -F / '{print $6}'`
# check if url is in history
if [ -z "$1" ]; then
if grep -q $url trailerhistory; then
inhistory=TRUE
else
inhistory=FALSE
fi
else
# force no history check for command-line parameters
inhistory=FALSE
fi
if [[ $inhistory == TRUE ]]; then
# it is; move on
echo "History: $url"
let "numhistory += 1"
# w00! new trailer! grab it.
else
wget -q $url -O ./$name.html
if grep -iq embed $name.html; then
# we can look directly at the file
# scour html file for