#!/bin/bash

#########################################
#	a3Downloader-sinDatos.sh			#
#	aabilio@gmail.com					#
#	http://en.wikipedia.org/wiki/WTFPL 	#
#########################################

# Válido mientras mantengan la mismas estructura de vídeos y URLs en el servidor.

#Poner a 1 si se prefiere usar axel y ni wget:
#Axel es un acelerador de descargas, tiene que estar instalado.
AXEL=1

#No modificar, servidores de a3 para web y videos:
servidorDescarga="http://desprogresiva.antena3.com/"
servidorAntena3="http://www.antena3.com"
HTML=`echo $1`

function fun_validar()
{
	#Comprobar "http://"
	echo $HTML | grep ^"http://www." > /dev/null
	if [ $? -ne 0 ]; then fun_ErrorAyuda; fi
	
	#Comprobar ".html"
	echo $HTML | grep ".html"$ > /dev/null
	if [ $? -ne 0 ]; then fun_ErrorAyuda; fi
}

function fun_final()
{
	#Borra antiguos .html, .xml que molesten:
	echo '[+] Borrando archivos temporales'
	fun_borrar

	echo -e '[FIN] \033[38;5;148mDescargas Finalizadas\033[39m'
	exit
}

function fun_ErrorAyuda()
{
	echo 'ERROR, URL mal formateada'
	echo 'Lanzamiento:'
	echo '	$ '$0' [URL]'
	echo ''
	echo 'URL: Tiene que tener "http://www."'
	echo 'a3Downloader - aabilio@gmail.com'
	
	exit
}

function fun_borrar()
{
	sleep 1.5
	find . -maxdepth 1 -name "*.html*" -exec rm {} \;
	find . -maxdepth 1 -name "*.xml*" -exec rm {} \;
	#find . -maxdepth 1 -name "*.mp4*" -exec rm {} \;
	echo ''
}

########## FORZAR DESCARGA ##########
function fun_a_cascoporro
{

	echo '[+] Descargando '$HTML'...'
	sleep 1.5
	wget -nv $HTML
	ERR=$?
	echo ''
	if [ $ERR -ne 0 ]
	then
		echo 'ERROR: al descargar, ¿Has introducido la URL con http://www??'
		exit
	fi
	
	#Hacer sus propias llamadas
	nombreHTML=`echo $HTML | tr "/" "\n" | grep -F .html`
	XML=`cat $nombreHTML | grep ".addVariable(\"xml\"" | cut -d\" -f4`
	nombreXML=`echo $XML | tr "/" "\n" | grep -F .xml`

	#Descargar xml:
	echo '[+] Descargando ' $nombreXML'...'
	sleep 1.5
	wget -nv $servidorAntena3$XML
	ERR=$?
	echo ''
	if [ $ERR -ne 0 ]
	then
		echo 'ERROR al descargar'
		exit
	fi

	programa=`echo $nombreHTML | cut -d. -f1`

	echo -e "\033[38;5;148m[+] Se descargará: "$programa"\033[39m"
	echo ''
	sleep 2

	#Sacar caps de .xml y descargar los caps.:
	for i in `cat $nombreXML | grep .mp4 | cut -d[ -f3 | cut -d] -f1`
	do
		PARTE=`echo $i | cut -d. -f1 | cut -d/ -f6`
		if [ -z $PARTE ]
		then
			PARTE="001"
		fi
		nombreMp4=$programa'_parte'$PARTE'.mp4'
		echo -e "[+] \033[38;5;148mDescargando: "$nombreMp4"\033[39m ($i)"
		
		#wget $servidorDescarga$i -O $nombreMp4
		if [ "$AXEL" -eq "1" ]
		then
			axel -a $servidorDescarga$i -o $nombreMp4
			ERR=$?
		else
			wget $servidorDescarga$i -O $nombreMp4
			ERR=$?
		fi
		
		echo ''
		if [ $ERR -ne 0 ]
		then
			echo 'ERROR al descargar. Inténtalo a mano'
			exit
		fi
	done
	fun_final
}

####### MAIN #######

fun_validar

clear

#Presentacion:
echo '==================================='
echo '| Descargar vídeos de Antena3.com |'
echo '==================================='
echo ''

#Borra antiguos .html, .xml y .mp4 (videos) que molesten:
echo '[+] Borrando archivos antiguos'
fun_borrar

fun_a_cascoporro


