Saturday, December 24, 2011

Script rtve



#!/bin/sh
#usage: ./rtve urlVideoToDownload OR ./rtve -f fileWithOneURLPerLine
#License: Public Domain

option="-f"

download(){

#Phase 1: get xml Id
xml=/tmp/$id.xml
urlpart=`sed 's/^.*\(.\)\(.\)\(.\)\(.\)$/\4\/\3\/\2\/\1/' <<< $id`
wget http://www.rtve.es/swf/data/es/videos/video/$urlpart/$id.xml --output-document=$xml


#Phase 2: get video Id
dataId=`sed 's/.*assetDataId::\([^"]*\).*/\1/;tx;d;:x' $xml`
urlpart2=`sed 's/^.*\(.\)\(.\)\(.\)\(.\)$/\4\/\3\/\2\/\1/' <<< $dataId`
xml2=/tmp/$dataId.xml
wget http://www.rtve.es/scd/CONTENTS/ASSET_DATA_VIDEO/$urlpart2/ASSET_DATA_VIDEO-$dataId.xml --output-document=$xml2


#Phase 3 get video
videoUrl=`sed 's/.*defaultLocation="\([^"]*\).*/\1/;tx;d;:x' $xml2`
title=`tr -d "\n" < $xml2 | sed 's/.*AS_TITLE_URL<\/key>[" "]*\([^<]*\).*/\1/;tx;d;:x'`
wget www.rtve.es/resources/te_ngva/flv$videoUrl --output-document="$title.flv"

}



if [ "$1" = "$option" ] ; then

while read line; do
id=`sed 's/.*\/\(.*\)\/$/\1/' <<<$line`
download
done < $2

else
id=`sed 's/.*\/\(.*\)\/$/\1/' <<<$1`
download
fi