#!/bin/bash

cd /var/www/vhosts/velascom.es/htdocs/ranking

limitdate=20211226
currentdate=$(date +"%Y%m%d")

if [ $currentdate -ge $limitdate ]; then
   exit 
fi

cat pagina01.html | sed 's/a href=\"/|/g' | sed 's/idCategoria=2/idCategoria=2|/g' | tr '|' '\n' | grep "busca-tu-dibujo" | grep -v santanderpinta | sed 's/^/https:\/\/www.santanderpinta.com/g' > a1.tmp

rm resultado1.tmp 
cat a1.tmp | head -2 | while read url; do
    curl -o kk.tmp "$url"
    nombre=$(cat kk.tmp | grep -A 1 "class=.autor" | grep -v "autor" | sed 's/<\/p>//g' | xargs)
    votos=$(cat kk.tmp | grep "dibujoVotos" | sed 's/^.*dibujoVotos..\(.*\)..span.*/\1/g') 
    votos=$(printf "%04d" $votos)
    echo "$votos|$nombre|$url" >> resultado1.tmp
done

echo "<html>" > resultado.html
echo "<head>" >> resultado.html
echo '  <meta charset="UTF-8">' >> resultado.html
echo "</head>" >> resultado.html
echo "<body>" >> resultado.html

date >> resultado.html
echo "<br/>";

cont=1
echo "<table>" >> resultado.html
cat resultado1.tmp | sort -r | while read line;do
    echo $line
    votos=$(echo "$line" | cut -f1 -d"|")
    votos=$(expr $votos - 0)
    nombre=$(echo "$line" | cut -f2 -d"|")
    url=$(echo "$line" | cut -f3 -d"|")
    echo "<tr>" >> resultado.html
    echo "<td>" >> resultado.html
    echo '<a href='${url}'>VER</a>' >> resultado.html
    echo "</td><td>" >> resultado.html
    echo $cont >> resultado.html
    echo "</td><td>" >> resultado.html
    echo $nombre >> resultado.html
    echo "</td><td>" >> resultado.html
    echo $votos >> resultado.html
    echo "</td>"  >> resultado.html
    echo "</tr>" >> resultado.html
    cont=$(expr $cont + 1)
done
echo "</table></body></html>" >> resultado.html
