#!/bin/bash
# snarf vids from a youtube user's channel
# uses youtube-dl
# can be used repeatedly, to snarf newly added vids
# based on yt-chanrip, but all code has been replaced!

. usage_auto "channel [max_videos=1000000] [start_at_number=1] [youtube-dl-option e.g. --format 18 ...]" "$@"

yt_user="${1%/}" ; shift
max_videos=1000000
start_at_number=1
if [[ "$1" == [0-9]* ]]; then
	max_videos=$1 ; shift
fi
if [[ "$1" == [0-9]* ]]; then
	start_at_number=$1 ; shift
fi

type=user
case "$yt_user" in
show/*) type=show ;;
esac
yt_user=${yt_user##*/}

if [ "`dirname "$PWD"`" != "$yt_user" -a ! -e .links.txt ]; then
	mkdir -p "$yt_user" ; cd "$yt_user"
fi

opts=(--title --no-overwrites "$@")
# opts=(--title --no-overwrites --format 18 "$@")   # mp4 480p

P=1
while true; do
	_P=`printf %06d $P`
	save_html=".$_P.html"
	if [ ! -s "$save_html" ]; then
		wget "http://www.youtube.com/$type/$yt_user/videos?sort=da&flow=list&page=$P" -O $save_html.part &&
			mv $save_html.part $save_html
	fi
	< $save_html htmllinks |
	grep '^http://www\.youtube\.com/watch?'
	if [ $? != 0 ]; then
		rm $save_html     # remove the page with no vids
		for D in 1 2; do  # remove the previous 2 pages to ensure we catch all new vids next time (unless they deleted >30!)
			__P=`printf %06d $[$P-$D]`
			__save_html=".$__P.html"
			if [ -e "$__save_html" ]; then
				mv "$__save_html" "$__save_html.old"
			fi
		done
		break
	fi
	P=$[P+1]
done |
sed 's/\&.*//' | uniqo |
tee .links.txt |
tail -n +$start_at_number |
head -n $max_videos |
while read U; do
	id=${U#http://www.youtube.com/watch\?v=}
#echo $id
	if [ -z "`qe ls -- *"$id".*`" -o -n "`qe ls -- *"$id".*part`" ]; then
		youtube-dl "${opts[@]}" "$U"
	fi
done


#  tee links.txt | tac | xa youtube-dl "${opts[@]}"

#curl -s "http://gdata.youtube.com/feeds/api/users/$channel/uploads" |
#  htmlsplit | htmltagattr media:player url | sed 's/\&.*//' |
#  tee links.txt | tac | xa youtube-dl "${opts[@]}"

