#!/usr/bin/env bash
#
#     dailymotion-dl
#     version 0.3.0
#
#
#  Copyright (C) 2009 Trecourt Nicolas
#
#  dailymotion-dl is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

# ------------------------------------------------------------

if [ "$1" = "" ]
then
	echo "Please specify an URL"
	exit 1
fi

# this will be used to store cookies
cookfile=`mktemp`

if [ "`echo $1 | grep "^http://www.dailymotion.com/swf/"`" ]
then
	echo "[.] looks like an embed/object source, flasm to the rescue"
	tmpfile=`mktemp`

	echo "[.] trying to download $1 to $tmpfile"
	wget -q "$1" -O "$tmpfile" --user-agent="Mozilla/5.0" \
             --keep-session-cookies \
             --save-cookies "$cookfile"
	ret=$?
	if [ $ret -gt 0 ]
	then
		echo "[!] something went bad, wrong URL?"
		rm "$cookfile" "$tmpfile"
		exit 2
	fi

	echo "[v] $1 downloaded, uncompressing"
	flasm -x "$tmpfile" &> /dev/null
	if [ $ret -gt 0 ]
	then
		echo "[!] something went bad, is flasm installed?"
		rm "$cookfile" "$tmpfile"
		exit 3
	fi

	# this is to help parsing...
	sed -i -e 's/||/\t/g' "$tmpfile"

	echo "[.] scanning swf for streams"
	for k in `cat $tmpfile`
	do
		if [ "`echo "$k" | grep H264-848x480`" ]
		then
			echo "[v] found H264-848x480 data"
			H264848=$k
		elif [ "`echo "$k" | grep H264-512x384`" ]
		then
			echo "[v] found H264-512x384 data"
			H264512=`echo "$k" | cut -d{ -f1 | sed 's/}//g'`
		elif [ "`echo "$k" | grep FLV-320x240`" ]
		then
			echo "[v] found FLV-320x240 data"
			FLV320=`echo "$k" | gawk '{ print substr($1, index($1, "{{video}}")+11)}'`
		fi
	done
	if [ "$H264848" != "" ]
	then
		localpart="$H264848"
		echo "[.] using H264-848x480"
	else
		if [ "$H264512" != "" ]
		then
			localpart="$H264512"
			echo "[.] using H264-512x384"
		else
			localpart="$FLV320"
			echo "[.] using FLV-320x240"
		fi
	fi

	rm "$tmpfile"
else
	echo "[.] doesn't looks like an embed/object source, processing"
fi

# First we get the damn "dummy" flv URL, this time we should be more carefull
if [ "$localpart" = "" ]
then
	localpart=`wget "$1" -O /dev/stdout -q --user-agent="Mozilla/5.0" \
        	                               --keep-session-cookies \
                	                       --save-cookies "$cookfile" \
	           | grep '.addVariable("video"' \
        	   | sed -e "s/%3A/:/g" \
	               -e "s/%2F/\//g" \
        	       -e "s/%3F/?/g" \
	               -e "s/%3D/=/g" \
        	       -e "s/%40/@/g" \
	               -e "s/%7C/|/g" \
        	       -e "s/%2C/,/g" \
	               -e "s/%25/%/g" \
        	       -e "s/%26/\&/g" \
	           | gawk '// { print substr($0, index($0, "video=")+6, index($0, "@@spark") - index($0, "video=")-6) }' \
        	   | cut -d " " -f 2 | sed -e "s/\"//g" -e "s/);//g"`
fi

if [ "$localpart" = "" ]
then
	echo "[!] cannot get FLV filename, maybe some HTML code change, or video don't exist"
	rm "$cookfile"
	exit 4
else
	tmpfile=`mktemp`
	echo "[v] got FLV initial filename: $localpart"
	echo "[.] will download in `pwd`/.$$, logging in $tmpfile"
fi

# ok now, the "tricky" (sort of) part, must got the "real" FLV
echo "[.] trying to download to .$$"
wget "$localpart" \
     --user-agent="Mozilla/5.0" \
     --keep-session-cookies \
     --save-cookies "$cookfile" \
     -o $tmpfile \
     -O .$$

ret=$?
if [ $ret -gt 0 ]
then
	echo "[!] something went bad, maybe some HTML code change, or video don't exist"
	exit 5
fi

rm "$cookfile"
echo "[v] file downloaded"
echo "[.] trying to detect real filename"

finalfile=`grep Location: "$tmpfile" | cut -d/ -f7 | cut -d? -f1`
if [ "$finalfile" = "" ]
then
	echo "[!] Unable to determine real filename from Location: header"
        echo "[.] renaming to $$.flv"
        mv .$$ $$.flv
else
	echo "[v] real filemame detected as: $finalfile, renaming"
	mv .$$ "$finalfile"
fi

rm "$tmpfile"