#!/bin/bash

# by TeknoHog

# usage: flashvideoget.sh URL1 [URL2] [...]
# where URL is the web page containing the flash player with the video.

# seems to work for the domains listed below, do try others too

# doesn't work for iltalehti.fi

YT_CLIENTS="youtube-dl clive"

for i in "$@"; do
    # we may need the contents multiple times
    PAGE=`mktemp`
    wget -O $PAGE $i

    FILE_URL=`grep -m 1 "\.flv" $PAGE | sed -e "s|.*[\"']\(.*\.flv\)[\"'].*|\1|"`

    if [ -z "$FILE_URL" ]; then
	# look for an embedded youtube, guba link
	YT_URL=`grep -m 1 "http://\(www.youtube.com/v/\|www.guba.com/watch/\)" $PAGE | sed -e 's!.*\(http://\(www.youtube.com/v/\|www.guba.com/watch/\)[a-zA-Z0-9]\+\).*!\1!'`
	for CLIENT in $YT_CLIENTS; do
	    if [ -x "`which $CLIENT`" ]; then
		$CLIENT $YT_URL && break
	    fi
	done
    elif [ -z "`echo $FILE_URL | grep http://`" ]; then
# e. g. nettitelkku.fi, ylex.yle.fi, www.wimp.com
	HTDOMAIN=`echo $i | sed -e 's|\(http://[^/]*\)/.*|\1|'`
	FILE_URL=$HTDOMAIN$FILE_URL
	wget -c "$FILE_URL"
    else
# e. g. iltasanomat.fi, etv.ee, hs.fi/videot, irc-galleria.net 
	wget -c "$FILE_URL"
    fi
done


