#!/bin/bash
#Rip hollywoodtuna.com
#set -x
. /home/default

# Parameters
URL=http://www.hollywoodtuna.com
DIR=/home/houghi/Pictures/to_work_on/download/rip
. ~/.hollywoodtuna


if [ "$MAX" \< "60428" ]
then
	echo "Please watch what you download. Stopping here"
#	grep MAX /media/backup/*/home/houghi/.hollywoodtuna
	cat ~/.hollywoodtuna
	exit 1
fi

#Get the latest numbers
wget -q -O $TMP $URL
grep 'href="http://www.hollywoodtuna.com/?p=' $TMP | \
grep '" rel=' | \
grep -v '&folder=' |\
awk -Fp= '{print $2}' |\
awk -F\" '{print $1}' | \
sort -un > $TMP.1

#Get the page
for PAGE in `cat $TMP.1`
do
	# See if it is a newer page
	if [ "$PAGE" -gt "$MAX" ]
	then
		#get the Image page URL
		wget -q -O $TMP "${URL}/?p=${PAGE}"
		grep title $TMP|grep loc|sed 's/#038;//g'|awk -F\" '{print $2}'|sed 's/ /%20/g' >> $TMP.img
	fi
done

#Get the images URL
cat $TMP.img | \
sed 's#http://www.hollywoodtuna.com/photo.php?id=##g' | \
sed 's/&title=/ /g' | \
sed 's/&loc=/ /g' | \
awk '{print "http://www.hollywoodtuna.com/images" $NF "/bigimages" $NF "/" $1 ".jpg" }' > $TMP

#Get the images
wget -q -nc -nd -P $DIR `cat $TMP`

#Clean up and count
find $DIR -type f -size 0 -print|xargs rm
MAX=`tail -n 1 $TMP.1`
echo "MAX=$MAX" > .hollywoodtuna
echo "`wc -l $TMP|awk '{print $1}'` pics downloaded from hollywoodtuna with `basename $0`"

#read
exit

