#!/bin/sh

# Descarga el vídeo flv de rtve.es a partir de la ID del mismo

echo '
# Script de dominio público
# kresp0 - principios de abril 2010
# Versión 0.3
'

if [ -z "$1" ]; then 
	echo uso: $0 ID_del_video
	echo Por ejemplo, si la URL es http://www.rtve.es/mediateca/videos/20100406/telediario-060410/737950.shtml, el ID es 737950
	exit
fi

#if [ ! -f /usr/bin/flvstreamer ]
#then
#	echo Tienes que instalar el programa flvstreamer.
#	echo Si estás en debian o ubuntu, sólo tienes que poner:
#	echo sudo aptitude install flvstreamer
#	exit
#fi

echo $1 | awk '{ print "wget http://www.rtve.es/swf/data/es/videos/alacarta/" substr($0, 6,1)"/" substr($0, 5,1) "/" substr($0, 4,1) "/" substr($0, 3,1) "/"$0".xml -O /tmp/xmlrtve" }' | sh

iconv --from-code=ISO-8859-1 --to-code=UTF-8 /tmp/xmlrtve > /tmp/xml

#urlrtmp=`sed -n 's|<file>\(.*\)</file>|\1|p' /tmp/xml  | sed -e 's/^[ \t]*//'`
urlhttp=`sed -n 's|<file>\(.*\)</file>|\1|p' /tmp/xml  | sed -e 's/^[ \t]*//' | sed -e 's/rtmp:\/\/stream/http:\/\/www/g'`

titulo=`sed -n 's|<name>\(.*\)</name>|\1|p' /tmp/xml  | sed -e 's/^[ \t]*//' | sed 's/ /_/g' | sed 's/\//-/g'`

#flvstreamer -r $urlrtmp -o $titulo.flv
wget $urlhttp -O $titulo.flv

