#!/usr/bin/env bash
# Another one, maybe one day, flash hell will end thanks to <video>
#
#     vimeo-dl
#     version 0.1.0
#
#
#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#                    Version 2, December 2004
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
#  0. You just DO WHAT THE FUCK YOU WANT TO.
#
# ------------------------------------------------------------


if [ "$1" = "" ]
then
       echo "\$1 : vimeo ID"
       exit 1
fi

tmp=`mktemp`

echo "[<] Downloading XML description"
if wget "http://www.vimeo.com/moogaloop/load/clip:$1/embed?param_show_title=0" -q -O $tmp
then
       echo "[.] Description downloaded"
else
       echo "[!] Something went wrong, dying"
       rm $tmp
       exit 2
fi

echo "[>] Parsing..."
signature="`grep 'request_signature>' $tmp | cut -d'>' -f2 | cut -d'<' -f1`"
expiration="`grep 'request_signature_expires>' $tmp | cut -d'>' -f2 | cut -d'<' -f1`"
rm $tmp

if [ "$signature" = "" -o "$expiration" = "" ]
then
       echo "[!] XML parsing error, dying"
       exit 3
else
       echo "[.] sign=$signature expires=$expiration"
fi


echo "[<] Downloading video $1 in $1.flv"
if wget "http://www.vimeo.com/moogaloop/play/clip:$1/$signature/$expiration/?q=sd&type=embed" -O "$1.mp4" -q
then
       echo "[.] Video saved in $1.flv"
else
       echo "[!] Something went wrong, dying"
       exit 4
fi