hardbrugg.ch/index.sh
2025-07-17 21:35:31 +02:00

210 lines
7.1 KiB
Bash
Executable File

#!/bin/bash
#
if ping -c 1 google.com; then
data="/var/www/hardbrugg.ch/data"
query="$(shuf -i 10000-99999 -n 1)"
## Read old data
atemp_old="$(cat $data/atemp.data)"
depth_old="$(cat $data/depth.data)"
disch_old="$(cat $data/disch.data)"
wtemp_old="$(cat $data/wtemp.data)"
# Get new data
## Air Temp
curl -A "Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Firefox/81.0" -s https://www.wetteronline.ch/wettertrend/zuerich | grep -o -P '.{0,1}current-temperature.{0,11}' | grep -Eo '\-?[0-9]*' | tail -n 1 > "$data"/atemp.data &
## Depth
curl -s https://www.ag.ch/app/hydrometrie/station/?id=11626 | grep -m 9 td | tail -n 1 | grep -Eo '[0-9][0-9][0-9].[0-9][0-9]' | awk '{print $1-398}' > "$data"/depth.data &
## Discharge
curl -s --user-agent "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/538.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36" https://hydroproweb.zh.ch/Listen/AktuelleWerte/aktuelle_werte.html | grep -A 3 Unterhard | grep nbsp | grep -Eo '[0-9]*[.][0-9]*' | sed 's/\.//' > "$data"/disch.data &
## Water Temp
curl -s https://hydroproweb.zh.ch/Listen/AktuelleWerte/AktWassertemp.html | grep -A 3 Letten | grep '<FONT COLOR=#000000>' | grep -Eo '[0-9]*\.[0-9]' > "$data"/wtemp.data &
## Weather
curl -s -N wttr.in/zuerich > $data/wttr.dataraw && grep -m 2 -Eo "[A-Z][a-z][a-z]* *[A-Za-z]* *[A-Za-z]* *[A-Za-z]*" "$data"/wttr.dataraw | tail -n 1 > "$data"/wttr.data &
## Wait for data to be fetched
wait
## Generate Visitor Number
#visit_num="$(shuf -i 111111-420069 -n 1)"
# Read new data
atemp_new="$(cat $data/atemp.data)"
depth_new="$(cat $data/depth.data)"
disch_new="$(cat $data/disch.data)"
wtemp_new="$(cat $data/wtemp.data)"
# Write plot data
echo "$(date +%Y%m%d%H%M) $atemp_new" >> "$data"/atemp.plot
echo "$(date +%Y%m%d%H%M) $depth_new" >> "$data"/depth.plot
echo "$(date +%Y%m%d%H%M) ${disch_new}0" >> "$data"/disch.plot
echo "$(date +%Y%m%d%H%M) $wtemp_new" >> "$data"/wtemp.plot
# Clean plot data and create plot images
current_date=$(date +%Y%m%d%H%M)
cutoff_date=$(date -d "1 week ago" +%Y%m%d%H%M)
plots="atemp depth disch wtemp"
for plot in $plots
do
awk -v cutoff="$cutoff_date" '$1 >= cutoff' "$data"/"${plot}".plot > "$data"/tempfile.txt && mv "$data"/tempfile.txt "$data"/"${plot}".plot
gnuplot -c "$data"/plot.gp "$data"/"${plot}".plot "$data"/"${plot}".png
done
find "$data" -type f -name "*.png" -exec cp {} /var/www/hardbrugg.ch/html/img/ \;
# Compare old and new data
## Air Temp
if [ $atemp_old -lt $atemp_new ]
then
echo "${atemp_new}° C ↗" > "$data"/atemp.datatrend
elif [ $atemp_old -gt $atemp_new ]
then
echo "${atemp_new}° C ↘" > "$data"/atemp.datatrend
else
echo "${atemp_new}° C ➡" > "$data"/atemp.datatrend
fi
## Depth
if (( $(echo "$disch_old < $disch_new" | bc -l) ))
then
echo "${depth_new} m ↗" > "$data"/depth.datatrend
elif (( $(echo "$disch_old > $disch_new" | bc -l) ))
then
echo "${depth_new} m ↘" > "$data"/depth.datatrend
else
echo "${depth_new} m ➡" > "$data"/depth.datatrend
fi
## Discharge
if (( $(echo "$disch_old < $disch_new" | bc -l) ))
then
echo "${disch_new}0 L ↗" > "$data"/disch.datatrend
elif (( $(echo "$disch_old > $disch_new" | bc -l) ))
then
echo "${disch_new}0 L ↘" > "$data"/disch.datatrend
else
echo "${disch_new}0 L ➡" > "$data"/disch.datatrend
fi
## Water Temp
if (( $(echo "$wtemp_old < $wtemp_new" | bc -l) ))
then
echo "${wtemp_new}° C ↗" > "$data"/wtemp.datatrend
elif (( $(echo "$wtemp_old > $wtemp_new" | bc -l) ))
then
echo "${wtemp_new}° C ↘" > "$data"/wtemp.datatrend
else
echo "${wtemp_new}° C ➡" > "$data"/wtemp.datatrend
fi
# Determine Danger Level
disch_dng="$(cat $data/disch.data)"
if [ $disch_dng -le 35000 ]
then
dangr="color:#00FF00\">1"
elif [ $disch_dng -gt 35000 ] && [ $disch_dng -le 45000 ]
then
dangr="color:#FFFF00\">2"
elif [ $disch_dng -gt 45000 ] && [ $disch_dng -le 53000 ]
then
dangr="color:#FFA500\">3"
elif [ $disch_dng -gt 53000 ] && [ $disch_dng -le 60000 ]
then
dangr="color:#FF0000\">4"
else
dangr="color:#9A0000\">5"
fi
# Read current data
atemp="$(cat $data/atemp.datatrend)"
depth="$(cat $data/depth.datatrend)"
disch="$(cat $data/disch.datatrend)"
wtemp="$(cat $data/wtemp.datatrend)"
utime="$(ls -la $data/atemp.data | awk '{print $8 }')"
wttr="$(cat $data/wttr.data)"
# Write index.html
echo -e "<!DOCTYPE html>
<html>
<head>
<title>
hardbrugg.ch
</title>
<meta name=viewport content=\"width=device-width, initial-scale=1.0\" charset=utf-8>
<link rel=\"shortcut icon\" type=image/png href=img/favicon.png>
<link rel=stylesheet href=style.css>
<head>
<body>
<script>
// Popup Function
function popFunction() {
var popup = document.getElementById(\"infopop\");
popup.classList.toggle(\"show\");
}
</script>
<div class=main>
<div class=conditions>
<p class=header><img class=logo src=img/hb.png alt=Hb.ch></p>
<div class=\"popup\" onclick=\"popFunction()\">
<div class=header>
<h5>&#8505</h5>
</div>
<span class=\"popuptext\" id=\"infopop\">
<p class=emo><a href=https://www.openstreetmap.org/#map=17/47.39157/8.52349 target=_blank>&#128506</a> <a href=mailto:cam@6bit.ch>&#128231</a></p>
<div class=text><p>Made with bash. No cookies, no bullshit.</p></div>
<div class=text><p>Updated every 20 minutes, curves show past week.</p></div>
<div class=sources><p>Using open data from:</p></div>
<div class=sources><a href=https://ag.ch target=_blank>ag.ch</a> <a href=https://wetteronline.ch target=_blank>wetteronline.ch</a> <a href=https://wttr.in target=_blank>wttr.in</a> <a href=https://zh.ch target=_blank>zh.ch</a></div>
<div class=text><p>Thank you!</ul></div>
</span>
</div>
<div class=current>
<p class=update>Updated at ${utime}</p>
<p class=wttr>$wttr</p>
<p class=danger>Danger Level</p>
<p class=level style=\"${dangr} / 5</p>
</div>
<div class=cat>
<p class=datapoint>Air Temperature</p>
<p class=\"data plot\" style=\"background-image: linear-gradient(rgba(255,255,255,0), rgba(0,0,0,0.39)), url(img/atemp.png?with=${query})\">${atemp}</p>
</div>
<div class=cat>
<p class=datapoint>Water Temperature</p>
<p class=\"data plot\" style=\"background-image: linear-gradient(rgba(255,255,255,0), rgba(0,0,0,0.39)), url(img/wtemp.png?with=${query})\">${wtemp}</p>
</div>
<div class=cat>
<p class=datapoint>Discharge per Second</p>
<p class=\"data plot\" style=\"background-image: linear-gradient(rgba(255,255,255,0), rgba(0,0,0,0.39)), url(img/disch.png?with=${query})\">${disch}</p>
</div>
<div class=cat>
<p class=datapoint>Depth</p>
<p class=\"data plot\" style=\"background-image: linear-gradient(rgba(255,255,255,0), rgba(0,0,0,0.39)), url(img/depth.png?with=${query})\">&asymp;${depth}</p>
</div>
</div>
<div class=video>
<img src=https://live.hardbrugg.ch alt=\"Looks like the network is down. Please tell your network admin that the network is down.\" onerror=this.src=\"img/down.png\">
</div>
</div>
<div class=footer>
<p>Gmacht mit 🔮 z'Züri. | <a href=https://6bit.ch/humans.txt>humans.txt</a></p>
</div>
</body>
</html>" > /var/www/hardbrugg.ch/html/index.html
else
logger -i "ping failed, skipping data aggregation."
exit 1
fi
# EOF