Creating a simple wind forecast bot in Mastodon

terceranexus6

Paula

Posted on October 6, 2023

Creating a simple wind forecast bot in Mastodon

I'm keeping a manual log (manual like in pencil and paper) of weather forecast in the mountain I like the most (Sierra Nevada). At some point I got interested in building scripts to help me out and I started sharing that information in Mastodon using a bot.

illustration of Mastodon mascot walking on Sierra Nevada

First of all, a note on Mastodon bots. They are easy to set up! specially using toot command. I created an account, added the specification that says it's a bot, configured the authorization in toot and got it running. Once I got there I created a simple weather parser so to be able to toot automatically the weather forecast in Mastodon.

Then I started to learn about wind and I got interested in getting wind details easily so to note them down. I asked around my community and I got an answer: open-meteo.

While it has it's limitations it worked perfectly for my purpose, a script to help me gather wind details through the day.
gif of a puppy being hit by the wind

First of all I used the interactive API to get the info that I wanted (Sierra Nevada, one day, wind details) into a CSV:

curl "https://api.open-meteo.com/v1/forecast?latitude=37.095&longitude=-3.3969&hourly=temperature_2m,windspeed_180m&forecast_days=1&format=csv" > mywindtoday.csv
Enter fullscreen mode Exit fullscreen mode

Then I counted the total lines of the file:

lines=$(wc -l mywindtoday.csv | cut -d ' ' -f1|tr '\n' ' '
Enter fullscreen mode Exit fullscreen mode

Also I created a variable for the half of the values line, discarding the first description lines (which are 5):

mdline=$((($lines/2)+5))
Enter fullscreen mode Exit fullscreen mode

Between the noon (first line, 00:00) and the half of the file is morning, so I created a variable for morning:

mnline=$(( ($mdline/2)+5 ))
Enter fullscreen mode Exit fullscreen mode

And between the half and the end it's evening, so I created another variable for that:

eveline=$(( (($lines-$mdline)/2) + $mdline ))
Enter fullscreen mode Exit fullscreen mode

The first noon values That I want are hour and wind, so I parse the CSV looking for those values in the key daytimes I estimated before:

midnight=$(csvcut -c 1,3 mywindtoday.csv | sed '5!d')
midnightdate=${midnight%,*}
midnighthour=${midnightdate#*T}
midnightwind=${midnight#*,}

morning=$(csvcut -c 1,3 mywindtoday.csv | head -n $mnline | tail -1)
morningdate=${morning%,*}
morninghour=${morningdate#*T}
morningwind=${morning#*,}

afternoon=$(csvcut -c 1,3 mywindtoday.csv | head -n $mdline | tail -1)
afternoondate=${afternoon%,*}
afternoonhour=${afternoondate#*T}
afternoonwind=${afternoon#*,}

evening=$(csvcut -c 1,3 mywindtoday.csv | head -n $eveline | tail -1)
evedate=${evening%,*}
evehour=${evedate#*T}
evewind=${evening#*,}

night=$(csvcut -c 1,3 mywindtoday.csv | head -n $lines | tail -1)
ngtdate=${night%,*}
ngthour=${ngtdate#*T}
Enter fullscreen mode Exit fullscreen mode

Now all I had to do was save it all into a text document in a readable format:

touch mywindtoot

echo "Detalles del viento en Sierra Nevada" >> mywindtoot
echo "A las $midnighthour de hoy,  $midnightwind k/h" >> mywindtoot
echo "A las $morninghour de hoy,  $morningwind k/h" >> mywindtoot
echo "A las $afternoonhour de hoy,  $afternoonwind k/h">> mywindtoot
echo "A las $evehour de hoy, $evewind k/h">> mywindtoot
echo "A las $ngthour de hoy, $ngtwind k/h">> mywindtoot
Enter fullscreen mode Exit fullscreen mode

And, if I wanted to toot it:

toot post "$(cat mywindtoot)"
Enter fullscreen mode Exit fullscreen mode

Or if I just wanted to read it in the terminal, I just cat it.

Detalles del viento en Sierra Nevada
A las 00:00 de hoy,  15.8 k/h
A las 09:00 de hoy,  8.0 k/h
A las 14:00 de hoy,  19.5 k/h
A las 18:00 de hoy, 2.2 k/h
A las 23:00 de hoy, 14.1 k/h

Enter fullscreen mode Exit fullscreen mode

Finally a little clean up:

rm mywindtoot
rm mywindtoday
Enter fullscreen mode Exit fullscreen mode

And that's it! My first test using it in Mastodon worked just fine. You can check the whole code here. Why don't you try to build something similar for your city? Maybe share with your neighborhood.

💖 💪 🙅 🚩
terceranexus6
Paula

Posted on October 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related