bio-dynamie/scrapers/org.bio-dynamie.sh

152 lines
5.5 KiB
Bash
Executable File
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
## eFile is older than "nn min ago"
if [[ $(date +%s -r /tmp/index.html) -lt $(date +%s --date="1440 min ago") ]]; then
cd /tmp; rm -f index.html; rm -f /tmp/buffer
wget https://www.bio-dynamie.org/jardinage/demarrer/
fi
[[ ! -f /tmp/index.html ]] && exit 1
## C'est quoi demain
DAY=$(date +%A | awk '{ print toupper( substr( $0, 1, 1 ) ) substr( $0, 2 ); }')
DAYE=$(date +%A --date="1 day" | awk '{ print toupper( substr( $0, 1, 1 ) ) substr( $0, 2 ); }')
DOM=$(date +%d)
TOM=$(date +%d --date="1 day")
DEND=$(date +%d/%m --date="2 day")
T0=$(echo $DOM | sed 's/^0*//')
T1=$(echo $TOM | sed 's/^0*//')
MONTH=$(date +%m)
MONTHE=$(date +%m --date="1 day")
YEAR=$(date +%Y)
YEARE=$(date +%Y --date="1 day")
## TRANSLATION
[[ $DAY == "Monday" ]] && DAY="Lundi" && DAYE="Mardi"
[[ $DAY == "Tuesday" ]] && DAY="Mardi" && DAYE="Mercredi"
[[ $DAY == "Wednesday" ]] && DAY="Mercredi" && DAYE="Jeudi"
[[ $DAY == "Thursday" ]] && DAY="Jeudi" && DAYE="Vendredi"
[[ $DAY == "Friday" ]] && DAY="Vendredi" && DAYE="Samedi"
[[ $DAY == "Saturday" ]] && DAY="Samedi" && DAYE="Dimanche"
[[ $DAY == "Sunday" ]] && DAY="Dimanche" && DAYE="Mardi"
## EXTRACT SEMAINES
count=$(cat /tmp/index.html | awk '/Semaine/{s=x}{s=s$0"\n"}/Semaine/{print s}' | grep -v ^$ | wc -l | cut -d ' ' -f 1)
x=1
while [ $x -le $count ]
do
SEM="$(cat /tmp/index.html | awk '/Semaine/{s=x}{s=s$0"\n"}/Semaine/{print s}' | grep -v ^$ | head -n $x | tail -n 1)"
echo "$x : $SEM"
A=$(echo "$SEM" | awk -F 'du ' '{print $2}' | cut -d ' ' -f 1 | sed 's|[^0-9]||g')
A0=$(echo "$A" | sed 's/^0*//')
B=$(echo "$SEM" | awk -F 'au ' '{print $2}' | cut -d ' ' -f 1 | sed 's|[^0-9]||g')
B0=$(echo "$B" | sed 's/^0*//')
echo "($x) : SEMAINE: T0=$T0 T1=$T1 A0=$A0 B0=$B0"
# CHECK WEEK INTERVAL
if [[ $T0 -le $T1 ]]; then
echo "($x) : SEMAINE: $A <= $T1 <= $B"
[[ $T1 -ge $A0 && $T1 -le $B0 ]] && echo "OK: ($x) $A - $B" && S=$x && break
else
echo "($x) : SEMAINE: $B <= $T1 <= $A"
[[ $T1 -le $A0 && $T1 -ge $B0 ]] && echo "REVERSE: ($x) $A - $B" && S=$x && break
fi
x=$(( $x + 1 ))
done
echo "##################################################################"
echo "Demain nous serons $DAYE $TOM/$MONTHE/$YEARE (SEMAINE $S)"
echo "##################################################################"
[[ "$S" == "" ]] && echo "IMPOSSIBLE DE TROUVER LA BONNE SEMAINE" && exit 1
# BROKEN cat /tmp/index.html | awk "/$DOM\/$MONTH\/$YEAR/{s=x}{s=s$0\"\n\"}/$TOM\/$MONTH\/$YEAR/{print s}" | html2text -utf8
function splitweek()
{
str="$(tr '\n' ' ' < /tmp/buffer)"
delimiter="$1"
s=$str$delimiter
array=();
while [[ $s ]]; do
array+=( "${s%%"$delimiter"*}" );
s=${s#*"$delimiter"};
done;
# declare -p array
echo "$1${array[$2]}" | awk -F "$DEND" '{print $1}' | sed s/\/\'/g | sed s/\…/./g | sed s/\/./g
}
#read
if [[ "$DAYE" == "Lundi" ]]; then
echo "LUNDI $TOM/$MONTHE $S"
LUNDI=$(cat /tmp/index.html | awk '/Lundi/{s=x}{s=s$0"\n"}/Mardi/{print s}' | html2text -utf8 | awk '/Lundi/{s=x}{s=s$0"\n"}/Mardi/{print s}')
echo "$LUNDI" > /tmp/buffer
splitweek "$DAYE" "$S" > /tmp/sms_BIODYN
fi
#read
if [[ "$DAYE" == "Mardi" ]]; then
echo "MARDI $TOM/$MONTHE $S"
MARDI=$(cat /tmp/index.html | awk '/Mardi/{s=x}{s=s$0"\n"}/Mercredi/{print s}' | html2text -utf8 | awk '/Mardi/{s=x}{s=s$0"\n"}/Mercredi/{print s}')
echo "$MARDI" > /tmp/buffer
splitweek "$DAYE" "$S" > /tmp/sms_BIODYN
fi
#read
if [[ "$DAYE" == "Mercredi" ]]; then
echo "MERCREDI $TOM/$MONTHE $S"
MERCREDI=$(cat /tmp/index.html | awk '/Mercredi/{s=x}{s=s$0"\n"}/Jeudi/{print s}' | html2text -utf8 | awk '/Mercredi/{s=x}{s=s$0"\n"}/Jeudi/{print s}')
echo "$MERCREDI" > /tmp/buffer
splitweek "$DAYE" "$S" > /tmp/sms_BIODYN
fi
#read
if [[ "$DAYE" == "Jeudi" ]]; then
echo "JEUDI $TOM/$MONTHE $S"
JEUDI=$(cat /tmp/index.html | awk '/Jeudi/{s=x}{s=s$0"\n"}/Vendredi/{print s}' | html2text -utf8 | awk '/Jeudi/{s=x}{s=s$0"\n"}/Vendredi/{print s}')
echo "$JEUDI" > /tmp/buffer
splitweek "$DAYE" "$S" > /tmp/sms_BIODYN
fi
#read
if [[ "$DAYE" == "Vendredi" ]]; then
echo "VENDREDI $TOM/$MONTHE $S"
VENDREDI=$(cat /tmp/index.html | awk '/ Vendredi/{s=x}{s=s$0"\n"}/ Samedi/{print s}' | html2text -utf8 | awk '/ Vendredi/{s=x}{s=s$0"\n"}/ Samedi/{print s}')
echo "$VENDREDI" > /tmp/buffer
splitweek "$DAYE" "$S" > /tmp/sms_BIODYN
fi
#read
if [[ "$DAYE" == "Samedi" ]]; then
echo "SAMEDI $TOM/$MONTHE $S"
SAMEDI=$(cat /tmp/index.html | awk '/Samedi/{s=x}{s=s$0"\n"}/Dimanche/{print s}' | html2text -utf8 | awk '/Samedi/{s=x}{s=s$0"\n"}/Dimanche/{print s}')
echo "$SAMEDI" > /tmp/buffer
splitweek "$DAYE" "$S" > /tmp/sms_BIODYN
fi
#read
if [[ "$DAYE" == "Dimanche" ]]; then
echo "DIMANCHE $TOM/$MONTHE $S"
DIMANCHE=$(cat /tmp/index.html | awk '/Dimanche/{s=x}{s=s$0"\n"}/#Lune/{print s}' | html2text -utf8 | awk '/Dimanche/{s=x}{s=s$0"\n"}/#Lune/{print s}' | awk -F "#Lune" '{print $1}')
echo "$DIMANCHE" > /tmp/buffer
splitweek "$DAYE" "$S" > /tmp/sms_BIODYN
fi
if [[ "$DAYE" == "Lundi" ]]; then
echo "__________LUNE__________"
LUNE=$(cat /tmp/index.html | awk '/#Lune/{s=x}{s=s$0"\n"}/\.#/{print s}' | html2text -utf8 | awk -F 'Lundi' '{print $1}')
echo "$LUNE" > /tmp/buffer
splitweek "#Lune" "$S" >> /tmp/sms_BIODYN
fi
echo "##################################################################"
echo "SMS"
echo "##################################################################"
cat /tmp/sms_BIODYN
echo "##################################################################"
exit 0