228 lines
9.1 KiB
Bash
Executable File

#!/bin/bash
source .env
download_meter=resources/download_meter.png
upload_meter=resources/upload_meter.png
needle=resources/needle.png
font=resources/CODE_Bold.otf
while true; do
date=$(date "+%Y%m%d_%H%M%S")
camera_image=data/image_$date.png
satellite_image=data/satellite_image_$date.png
speedtest_ookla=data/speedtest_ookla_$date.json
speedtest_libre=data/speedtest_libre_$date.json
speedtest_fast=data/speedtest_fast_$date.json
speedtest_download_meter=data/speedtest_download_meter_$date.png
speedtest_upload_meter=data/speedtest_upload_meter_$date.png
speedtest_meter_image=data/speedtest_meter_image_$date.png
weather_file=data/weather_$date.json
edited_image=data/edited_image_$date.png
#################################################################################
latency(){
echo $(ping -c 1 $1) | \
grep 'time=' | \
awk -F'time=' '{print $2}' | \
awk '{print $1}'
}
#################################################################################
#################################################################################
echo Grabbing Camara Image
# Capture a frame from RTSP stream
mpv $RTSP_URL --frames=1 --vo=image --ovc=png --o=$camera_image >> /dev/null
echo Camera Image grabbed
#################################################################################
#################################################################################
echo Grabbing Satellite Image
# scrap html, get image link and download it.
curl -s $(curl -s "https://www.meteorology.gov.mv/" | \
grep -o 'https://mobile.codeworks.mv/uploads/Satellite/[^"]*' | \
tail -n 1) -o $satellite_image
echo Satellite Image grabbed
#################################################################################
#################################################################################
echo Doing speedtest by Ookla
# Run speedtest and output to a file
speedtest --json > $speedtest_ookla
# Extract information from the speedtest output
# Parsing JSON data
ookla_server=$(jq -r '.server.sponsor' "$speedtest_ookla")
ookla_city=$(jq -r '.server.name' "$speedtest_ookla")
ookla_country=$(jq -r '.server.country' "$speedtest_ookla")
ookla_ping=$(jq '.ping' "$speedtest_ookla")
ookla_download=$(jq '.download' "$speedtest_ookla")
ookla_upload=$(jq '.upload' "$speedtest_ookla")
ookla_bytes_received=$(jq '.bytes_received' "$speedtest_ookla")
ookla_bytes_sent=$(jq '.bytes_sent' "$speedtest_ookla")
# Converting to human readable units
ookla_download_mbps=$(echo "scale=2; $ookla_download / 1000000" | bc)
ookla_upload_mbps=$(echo "scale=2; $ookla_upload / 1000000" | bc)
ookla_bytes_received_mb=$(echo "scale=2; $ookla_bytes_received / 1048576" | bc)
ookla_bytes_sent_mb=$(echo "scale=2; $ookla_bytes_sent / 1048576" | bc)
ookla_total=$(echo $ookla_bytes_sent_mb+$ookla_bytes_received_mb | bc)
#echo Starting LibreSpeed
#librespeed-cli --json > $speedtest_libre
# Parsing JSON data
#libre_bytes_sent=$(jq '.[0].bytes_sent' "$speedtest_libre")
#libre_bytes_received=$(jq '.[0].bytes_received' "$speedtest_libre")
#libre_ping=$(jq '.[0].ping' "$speedtest_libre")
#libre_upload=$(jq '.[0].upload' "$speedtest_libre")
#libre_download=$(jq '.[0].download' "$speedtest_libre")
#libre_server_name=$(jq -r '.[0].server.name' "$speedtest_libre")
# Converting to human readable units
#libre_bytes_sent_mb=$(echo "scale=2; $libre_bytes_sent / 1048576" | bc)
#libre_bytes_received_mb=$(echo "scale=2; $libre_bytes_received / 1048576" | bc)
#libre_total=$(echo $libre_bytes_sent_mb+$libre_bytes_received_mb | bc)
#echo Starting Fast
#fast --upload --json > $speedtest_fast
#fast_download_speed=$(jq '.downloadSpeed' "$speedtest_fast")
#fast_upload_speed=$(jq '.uploadSpeed' "$speedtest_fast")
#fast_latency=$(jq '.latency' "$speedtest_fast")
#fast_downloaded=$(jq '.downloaded' "$speedtest_fast")
#fast_uploaded=$(jq '.uploaded' "$speedtest_fast")
#fast_total=$(echo $fast_downloaded+$fast_uploaded | bc)
total_wasted=$ookla_total
echo Speedtests complete
#################################################################################
#################################################################################
valorant_ping=$(latency dynamodb.ap-southeast-1.amazonaws.com)
cfdns_ping=$(latency 1.1.1.1)
googledns_ping=$(latency 8.8.8.8)
#################################################################################
#################################################################################
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 and day or not
weather_condition=$(jq '.current_weather.weathercode' "$weather_file")
is_day=$(jq '.current_weather.is_day' "$weather_file")
case $weather_condition in
0) current_weather="$([ "$is_day" = "1" ] && echo "☀️" || echo "🌙") Clear Sky";;
1) current_weather="$([ "$is_day" = "1" ] && echo "☀️" || echo "🌙") Mainly Clear";;
2) current_weather="⛅ Partly Cloudy" ;;
3) current_weather="☁️ Overcast" ;;
45) current_weather="🌫️ Fog" ;;
48) current_weather="🌫️🌬️ Depositing Rime Fog" ;;
51) current_weather="🌦️💧 Light Drizzle" ;;
53) current_weather="🌦️💧 Moderate Drizzle" ;;
55) current_weather="🌦️💧 Dense Drizzle" ;;
56) current_weather="🌦️❄️ Light Freezing Drizzle" ;;
57) current_weather="🌦️❄️ Dense Freezing Drizzle" ;;
61) current_weather="🌧️ Slight Rain" ;;
63) current_weather="🌧️ Moderate Rain" ;;
65) current_weather="🌧️ Heavy Rain" ;;
66) current_weather="🌧️❄️ Light Freezing Rain" ;;
67) current_weather="🌧️❄️ Heavy Freezing Rain" ;;
71) current_weather="🌨️ Slight Snowfall" ;;
73) current_weather="🌨️ Moderate Snowfall" ;;
75) current_weather="🌨️ Heavy Snowfall" ;;
77) current_weather="❄️ Snow Grains" ;;
80) current_weather="🌧️🚿 Slight Rain Showers" ;;
81) current_weather="🌧️🚿 Moderate Rain Showers" ;;
82) current_weather="🌧️🚿 Violent Rain Showers" ;;
85) current_weather="🌨️❄️ Slight Snow Showers" ;;
86) current_weather="🌨️❄️ Heavy Snow Showers" ;;
95) current_weather="⛈️ Slight Thunderstorm" ;;
96) current_weather="⛈️🌨️ Thunderstorm with Slight Hail" ;;
99) current_weather="⛈️🌨️ Thunderstorm with Heavy Hail" ;;
*) current_weather="🌐 Weather Condition Not Recognized" ;;
esac
echo Weather checked
#################################################################################
#################################################################################
ookla_download_angle=$(echo "scale=2; -118 + ($ookla_download_mbps - 0) / ($MAX_DOWNLOAD_SPEED - 0) * (118 - -118)" | bc)
ookla_upload_angle=$(echo "scale=2; -118 + ($ookla_upload_mbps - 0) / ($MAX_UPLOAD_SPEED - 0) * (118 - -118)" | bc)
convert $download_meter \
\( $needle -background none \
-rotate $ookla_download_angle \) \
-gravity center \
-compose over \
-composite \
-pointsize 41 \
-font $font \
-fill white \
-annotate +0+-15 $ookla_download_mbps \
-annotate +120+85 $MAX_DOWNLOAD_SPEED \
$speedtest_download_meter
convert $upload_meter \
\( $needle -background none \
-rotate $ookla_upload_angle \) \
-gravity center \
-compose over \
-composite \
-pointsize 41 \
-font $font \
-fill white \
-annotate +0+-15 $ookla_upload_mbps \
-annotate +120+85 $MAX_UPLOAD_SPEED \
$speedtest_upload_meter
convert $camera_image \
\( $speedtest_download_meter \
-resize $METER_SCALE% \
-gravity southwest \
-geometry +10+0 \) \
-composite \
\( $speedtest_upload_meter \
-resize $METER_SCALE% \
-gravity southwest \
-geometry +$UPLOAD_METER_LOCATION+0 \) \
-composite \
$speedtest_meter_image
#################################################################################
#################################################################################
caption="
$current_weather
🔫 Valorant SEA ping: $valorant_ping ms
🌐 1.1.1.1 ping: $cfdns_ping ms
🌐 8.8.8.8 ping: $googledns_ping ms
*Speedtest by Ookla*:
🌐 *Server*: $ookla_server, $ookla_city, $ookla_country
🏓 *Latency*: $ookla_ping ms
⬇️ *Download*: $ookla_download_mbps Mbit/s, Used: $ookla_bytes_received_mb MB
⬆️ *Upload*: $ookla_upload_mbps Mbit/s, Used: $ookla_bytes_sent_mb MB
Total data wasted: $total_wasted MB
"
#################################################################################
#################################################################################
echo Uploading to Telegram
curl -X POST https://api.telegram.org/bot$TG_BOT_TOKEN/sendMediaGroup \
-F chat_id=$TG_CHATID \
-F media='[{"type": "photo", "media": "attach://photo1", "caption": "'"$caption"'", "parse_mode": "Markdown"}, {"type": "photo", "media": "attach://photo2"}]' \
-F photo1=@$speedtest_meter_image \
-F photo2=@$satellite_image >> /dev/null
#################################################################################
echo Ok done, Sleeping for $DELAY
sleep $DELAY
done