You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.1 KiB
Bash
82 lines
3.1 KiB
Bash
#!/bin/bash
|
|
# fanta <fanta@56k.es> - https://git.56k.es/fanta/santiago
|
|
# dependencies: toot, bc
|
|
|
|
scriptDir="/home/fanta/santiago" # The bot path. changeme
|
|
cd $scriptDir
|
|
user=$(whoami)
|
|
opt="$1"
|
|
version="0.1"
|
|
fileNotifications="/tmp/notifications.txt"
|
|
|
|
function who { if [ "$(whoami)" = "root" ]; then exit; fi }
|
|
|
|
function checkDeps {
|
|
tootValue="$(toot | head -1)"
|
|
if [[ $tootValue != "toot - a Mastodon CLI client" ]]; then echo "Please install toot"; exit; fi
|
|
}
|
|
|
|
function login {
|
|
toot login
|
|
}
|
|
|
|
function run {
|
|
toot notifications -m --no-color | grep -i "ID " | cut -d " " -f 2 | head -20 > $fileNotifications
|
|
while read not; do
|
|
toot=$(toot thread "$not" | grep -i "@caminosantiago añade " | head -1)
|
|
if [ -z "$toot" ]
|
|
then echo "Toot no valido"
|
|
else
|
|
walkerName=$(toot thread "$not" --no-color | head -2 | tail -1 | cut -d "@" -f 2,3 | cut -d " " -f 1)
|
|
walkerDate=$(toot thread "$not" --no-color | head -2 | tail -1 | cut -d "@" -f 2,3 | rev | cut -d " " -f 1,2,3 | rev | sed "s/ //g")
|
|
walkerFile=$(echo "$scriptDir/walkers/$walkerName")
|
|
walkerKm=$(toot thread "$not" --no-color | grep -i "@caminosantiago añade " | grep -i "km" | head -1 | tr " " "_" | rev | cut -d "_" -f 1 | rev | tr '[:upper:]' '[:lower:]' | sed 's/km//g')
|
|
walkerKM=$(echo $walkerKM | sed 's/[^0-9]*//g') # Remove all non-numeric characters
|
|
|
|
if ! [[ $walkerKm =~ '^[0-9]+$' ]]; then
|
|
if [ -f "$walkerFile" ]; then
|
|
walkerDate2=$(cat $walkerFile | grep "$walkerDate" | wc -l)
|
|
if [[ $walkerDate2 == 0 ]]; then
|
|
echo "Nuevo kilometraje de $walkerName"
|
|
echo "$walkerDate;$walkerKm" >> $walkerFile
|
|
walkerTotal=$(cat $walkerFile | cut -d ";" -f 2 | tr "\n" "+" | rev | cut -d "+" -f 2-9999 | bc)
|
|
toot post -r "$not" -m mapa.jpg -d "mapa desde Irun a Santiago. Ruta conocida como el camino del norte" "@$walkerName Llevas caminados $walkerTotal kilometros en total. Son 824Km el Camino del Norte #fedicaminodesantiago"
|
|
else
|
|
echo "Toot ya añadido previamente"
|
|
fi
|
|
else
|
|
touch $walkerFile
|
|
echo "Primer kilometraje de $walkerName"
|
|
echo "$walkerDate;$walkerKm" > $walkerFile
|
|
walkerTotal=$(cat $walkerFile | cut -d ";" -f 2 | tr "\n" "+" | rev | cut -d "+" -f 2-9999 | bc)
|
|
toot post -r "$not" -m mapa.jpg -d "mapa desde Irun a Santiago. Ruta conocida como el camino del norte" "Bienvenido/a/e @$walkerName al #fedicaminodesantiago. Llevas caminados $walkerTotal kilometros. Son 824Km el Camino del Norte #newWalkerCaminoSantiago"
|
|
fi
|
|
else
|
|
echo "Toot no valido: Km value Not a number"
|
|
fi
|
|
fi
|
|
done < $fileNotifications
|
|
}
|
|
|
|
function help {
|
|
echo -e "santiago $version - fanta <fanta@56k.es>\n"
|
|
echo "-l Login the mastodon bot account\n"
|
|
echo "-r Run the bot\n"
|
|
echo "-h Show this help\n"
|
|
}
|
|
|
|
function checkOpt {
|
|
if [ -z "$opt" ]; then help; fi
|
|
if [ "$opt" = "-h" ]; then help; fi
|
|
if [ "$opt" = "-l" ]; then login; fi
|
|
if [ "$opt" = "-r" ]; then run; fi
|
|
}
|
|
|
|
function main() {
|
|
who
|
|
checkDeps
|
|
checkOpt
|
|
}
|
|
|
|
main
|