99 lines
3.6 KiB
Bash
Executable File

#!/bin/bash
source .env
while true; do
date=$(date "+%Y%m%d_%H%M%S")
image=data/image_$date.png
speedtest_result=data/speedtest_$date.txt
weather_file=data/weather_$date.json
edited_image=data/edited_image_$date.png
#################################################################################
echo Grabbing Image
# Capture a frame from RTSP stream
mpv $RTSP_URL --frames=1 --vo=image --ovc=png --o=$image >> /dev/null
echo Image grabbed
#################################################################################
#################################################################################
echo Doing speedtest
# Run speedtest and output to a file
speedtest --json > $speedtest_result
# Extract information from the speedtest output
# Parsing JSON data
download=$(jq '.download' "$speedtest_result")
upload=$(jq '.upload' "$speedtest_result")
ping=$(jq '.ping' "$speedtest_result")
bytes_received=$(jq '.bytes_received' "$speedtest_result")
bytes_sent=$(jq '.bytes_sent' "$speedtest_result")
isp=$(jq -r '.client.isp' "$speedtest_result")
country=$(jq -r '.client.country' "$speedtest_result")
cc=$(jq -r '.client.cc' "$speedtest_result")
# Converting download and upload speeds from bytes per second to Mbit/s
download_mbps=$(echo "scale=3; $download / 125000" | bc)
upload_mbps=$(echo "scale=3; $upload / 125000" | bc)
# Converting bytes to MB
bytes_received_mb=$(echo "scale=2; $bytes_received / 1048576" | bc)
bytes_sent_mb=$(echo "scale=2; $bytes_sent / 1048576" | bc)
echo Speedtest complete
#################################################################################
#################################################################################
echo Checking weather
# Make the API request
curl -s "https://api.open-meteo.com/v1/forecast?latitude=${LATITUDE}&longitude=${LONGITUDE}&current_weather=true" > $weather_file
# Extract Weather Condition
weather_condition=$(cat $weather_file | jq '.current_weather.weathercode')
case $weather_condition in
0) current_weather="☀️ Clear sky" ;;
1|2|3) current_weather="☁️ Cloudy" ;;
45|48) current_weather="🌫️ Fog" ;;
51|53|55|56|57) current_weather="🌧️ Drizzle" ;;
61|63|65|66|67) current_weather="🌦️ Rain" ;;
80|81|82) current_weather="🌧️ Rain showers" ;;
85|86) current_weather="🌨️ Snow showers" ;;
95|96|99) current_weather="⛈️ Thunderstorm" ;;
*) current_weather="🌐 Weather condition not recognized" ;;
esac
echo Weather checked
#################################################################################
#################################################################################
caption="
$current_weather
🌐 *ISP*: $isp, $country, $cc
🏓 *Latency*: $ping ms
⬇️ *Download*: $download_mbps Mbit/s, Used: $bytes_received_mb MB
⬆️ *Upload*: Upload: $upload_mbps Mbit/s, Used: $bytes_sent_mb MB
"
#################################################################################
#################################################################################
#convert "$image" \
# -gravity South \
# -font $PWD/JoyPixels.ttf \
# -pointsize 20 \
# -annotate +0+10 "$caption" \
# $edited_image
#################################################################################
#################################################################################
echo Uploading to Telegram
curl -X POST https://api.telegram.org/bot$TG_BOT_API_KEY/sendPhoto \
-F chat_id=$TG_CHANNEL_ID \
-F photo=@$image \
-F caption="$caption" \
-F parse_mode=Markdown >> /dev/null
#################################################################################
echo Ok done, Sleeping for $DELAY
sleep $DELAY
done