refactored weather.sh

- refactored code for better readability
- added URL about the creator from this script
This commit is contained in:
0n1cOn3 2024-04-17 01:03:27 +02:00 committed by GitHub
parent f8ed1b8cd0
commit 1e54769d99
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,31 +1,63 @@
#!/bin/bash #!/bin/bash
#verify if user has jq installed # # # # # # # # # # # # # # # # # # # # # # # # # #
# Written by: https://github.com/HornetMaidan/ #
# https://github.com/HornetMaidan/ #
# #
# Refactored by: #
# https://github.com/0n1cOn3 #
# # # # # # # # # # # # # # # # # # # # # # # # # #
# Variables for weather.sh
api_key="1dfeef54f6e423266e0f09920919f297"
ipinfo_key="adc827a697c024"
user_ip=$(curl -s https://ifconfig.me/ip)
location_info=$(curl -s "https://ipinfo.io/$user_ip?token=$ipinfo_key")
api_url="https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&units=metric&appid=$api_key"
forecast_url="https://api.openweathermap.org/data/2.5/forecast?lat=$lat&lon=$lon&units=metric&appid=$api_key"
lat=$(echo "$location_info" | jq -r '.loc' | cut -d ',' -f 1)
lon=$(echo "$location_info" | jq -r '.loc' | cut -d ',' -f 2)
weather_data=$(curl -s "$api_url")
forecast_data=$(curl -s "$forecast_url")
weather_desc=$(echo "$weather_data" | jq -r '.weather[].description')
wind_speed=$(echo "$weather_data" | jq -r '.wind.speed')
# Main application #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Check if the user has jq installed
if ! command -v jq &> /dev/null; then if ! command -v jq &> /dev/null; then
echo "oww, it seems that you don't have jq JSON pawsew installed... pwease do :3" if [ "$(id -u)" -ne 0 ]; then
echo "sewect youw package managew..." echo "You need root privileges to install jq."
echo "[1] apt" exit 1
echo "[2] pacman" fi
echo "[3] xbps" echo "It seems that jq is not installed. Please select your package manager:"
echo "[4] yum" echo "[1] apt"
echo "[5] dnf" echo "[2] pacman"
echo "[6] brew" echo "[3] xbps"
echo "[7] zypper" echo "[4] yum"
read -rp "youw choice: " choice echo "[5] dnf"
case $choice in echo "[6] brew"
1) sudo apt-get update && sudo apt-get install jq awk;; echo "[7] zypper"
2) sudo pacman --sync jq awk;; read -rp "Your choice: " choice
3) sudo xbps-install -S jq awk;; case $choice in
4) sudo yum install epel-release && sudo yum install jq awk ;; 1) sudo apt-get update && sudo apt-get install jq awk;;
5) sudo dnf install jq awk;; 2) sudo pacman --sync jq awk;;
6) brew install jq awk;; 3) sudo xbps-install -S jq awk;;
7) sudo zypper install jq awk ;; 4) sudo yum install epel-release && sudo yum install jq awk ;;
*) echo "oww, you've done a fucky wucky! twy again ow install it manually~"; exit 1 ;; 5) sudo dnf install jq awk;;
6) brew install jq awk;;
7) sudo zypper install jq awk ;;
*) echo "Invalid input! Please try again or install jq manually."; exit 1 ;;
esac esac
fi fi
#formatting the output # Functions for text formatting
strip_ansi_escape_codes() { strip_ansi_escape_codes() {
echo -ne "$1" | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g" echo -ne "$1" | sed "s,\x1B\[[0-9;]*[a-zA-Z],,g"
} }
@ -38,80 +70,76 @@ pad_string() {
printf "%s%*s" "$string" "$spaces" "" printf "%s%*s" "$string" "$spaces" ""
} }
#api requests # Adjusting city name
api_key="1dfeef54f6e423266e0f09920919f297"
ipinfo_key="adc827a697c024"
user_ip=$(curl -s https://ifconfig.me/ip)
location_info=$(curl -s "https://ipinfo.io/$user_ip?token=$ipinfo_key")
lat=$(echo "$location_info" | jq -r '.loc' | cut -d ',' -f 1)
lon=$(echo "$location_info" | jq -r '.loc' | cut -d ',' -f 2)
api_url="https://api.openweathermap.org/data/2.5/weather?lat=$lat&lon=$lon&units=metric&appid=$api_key"
forecast_url="https://api.openweathermap.org/data/2.5/forecast?lat=$lat&lon=$lon&units=metric&appid=$api_key"
weather_data=$(curl -s "$api_url")
forecast_data=$(curl -s "$forecast_url")
weather_desc=$(echo "$weather_data" | jq -r '.weather[].description')
wind_speed=$(echo "$weather_data" | jq -r '.wind.speed')
city=$(echo "$weather_data" | jq -r '.name') city=$(echo "$weather_data" | jq -r '.name')
if [[ "$city" == "Nur-Sultan" ]] ; then if [[ "$city" == "Nur-Sultan" ]]; then
city='Astana'; city='Astana';
fi fi
#cli output # CLI output
echo "" echo ""
echo -e "\t\e[37mhewwo \e[35m$USER!\e[37m i hope u awe doing gweat today!\e[0m" echo -e "\tHello $USER! I hope you are doing well!"
echo -e "\t\e[37mhewe is the cuwwent weathew wepowt fow \e[32m$city\e[37m uwu~\e[0m" echo -e "\tHere is the current weather report for $city."
echo echo
echo -e "\e[32m$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' =)\e[0m" echo -e "$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' =)"
echo -e "\e[32m\t$(pad_string "cuwwent" 40) fowecast" echo -e "\t$(pad_string "Current" 40) Forecast"
echo -e "\e[32m$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' -)\e[0m" echo -e "$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' -)"
echo echo
echo -e "\t$(pad_string "\e[37mtime: $(date +"%H:%M")" 40)\e[0m $(echo "$forecast_data" | jq -r '.list[2].dt_txt' | sed 's#-#/#g;s#...$##;') ----- \e[33m$(echo "$forecast_data" | jq -r '[.list[0].weather[].description, .list[0].main.temp] | join(", ")')°C\e[0m" echo -e "\t$(pad_string "Time: $(date +"%H:%M")" 40) $(echo "$forecast_data" | jq -r '.list[2].dt_txt' | sed 's#-#/#g;s#...$##;') ----- $(echo "$forecast_data" | jq -r '[.list[0].weather[].description, .list[0].main.temp] | join(", ")')°C"
echo -e "\t$(pad_string "\e[37mdate: $(date +"%d/%m/%Y")" 40)\e[0m $(echo "$forecast_data" | jq -r '.list[3].dt_txt' | sed 's#-#/#g;s#...$##') ----- \e[33m$(echo "$forecast_data" | jq -r '[.list[1].weather[].description, .list[1].main.temp] | join(", ")')°C\e[0m" echo -e "\t$(pad_string "Date: $(date +"%d/%m/%Y")" 40) $(echo "$forecast_data" | jq -r '.list[3].dt_txt' | sed 's#-#/#g;s#...$##') ----- $(echo "$forecast_data" | jq -r '[.list[1].weather[].description, .list[1].main.temp] | join(", ")')°C"
echo -e "\t$(pad_string "weathew: \e[33m\e[5m$(echo "$weather_data" | jq -r '.weather[].description')\e[0m" 40) $(echo "$forecast_data" | jq -r '.list[4].dt_txt' | sed 's#-#/#g;s#...$##') ----- \e[33m$(echo "$forecast_data" | jq -r '[.list[2].weather[].description, .list[2].main.temp] | join(", ")')°C\e[0m" echo -e "\t$(pad_string "Weather: $(echo "$weather_data" | jq -r '.weather[].description')" 40) $(echo "$forecast_data" | jq -r '.list[4].dt_txt' | sed 's#-#/#g;s#...$##') ----- $(echo "$forecast_data" | jq -r '[.list[2].weather[].description, .list[2].main.temp] | join(", ")')°C"
echo -e "\t$(pad_string "\e[37mtempewatuwe: \e[35m$(echo "$weather_data" | jq -r '.main.temp')°C\e[0m" 40) $(echo "$forecast_data" | jq -r '.list[5].dt_txt' | sed 's#-#/#g;s#...$##') ----- \e[33m$(echo "$forecast_data" | jq -r '[.list[3].weather[].description, .list[3].main.temp] | join(", ")')°C\e[0m" echo -e "\t$(pad_string "Temperature: $(echo "$weather_data" | jq -r '.main.temp')°C" 40) $(echo "$forecast_data" | jq -r '.list[5].dt_txt' | sed 's#-#/#g;s#...$##') ----- $(echo "$forecast_data" | jq -r '[.list[3].weather[].description, .list[3].main.temp] | join(", ")')°C"
echo -e "\t$(pad_string "\e[37mwind: \e[37m\e[36m$(echo "$weather_data" | jq -r '.wind.speed')m/s, azimuth: $(echo "$weather_data" | jq -r '.wind.deg')\e[0m" 40) $(echo "$forecast_data" | jq -r '.list[6].dt_txt' | sed 's#-#/#g;s#...$##') ----- \e[33m$(echo "$forecast_data" | jq -r '[.list[4].weather[].description, .list[4].main.temp] | join(", ")')°C\e[0m" echo -e "\t$(pad_string "Wind: $(echo "$weather_data" | jq -r '.wind.speed')m/s, Azimuth: $(echo "$weather_data" | jq -r '.wind.deg')" 40) $(echo "$forecast_data" | jq -r '.list[6].dt_txt' | sed 's#-#/#g;s#...$##') ----- $(echo "$forecast_data" | jq -r '[.list[4].weather[].description, .list[4].main.temp] | join(", ")')°C"
echo -e "\t$(pad_string "\e[37mcwouds: \e[37m\e[34m$(echo "$weather_data" | jq -r '.clouds.all')%\e[0m" 40) $(echo "$forecast_data" | jq -r '.list[7].dt_txt' | sed 's#-#/#g;s#...$##') ----- \e[33m$(echo "$forecast_data" | jq -r '[.list[5].weather[].description, .list[5].main.temp] | join(", ")')°C\e[0m" echo -e "\t$(pad_string "Clouds: $(echo "$weather_data" | jq -r '.clouds.all')%" 40) $(echo "$forecast_data" | jq -r '.list[7].dt_txt' | sed 's#-#/#g;s#...$##') ----- $(echo "$forecast_data" | jq -r '[.list[5].weather[].description, .list[5].main.temp] | join(", ")')°C"
echo echo
echo -e "\e[32m$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' -)\e[0m" echo -e "$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' -)"
if [[ "$weather_desc" == "clear sky" ]]; then # Weather description
echo -e "\t\e[33mseems to be a pwetty cleaw sky today!\e[0m" case "$weather_desc" in
elif [[ "$weather_desc" == *"clouds"* ]]; then "clear sky")
echo -e "\t\e[34msome cwouds awe pwesent but it's ok :3 i like cwouds!!!!\e[0m" echo -e "\tSeems to be a pretty clear sky today!"
elif [[ "$weather_desc" == *"rain"* || "$weather_desc" == *"drizzle"* ]]; then ;;
echo -e "\t\e[36mlooks like it's wainin today, make suwe u bwing an umbwella with u :3\\e[0m" *"clouds"*)
elif [[ "$weather_desc" == *"thunderstorm"* ]]; then echo -e "\tSome clouds are present but it's okay. I like clouds!"
echo -e "\t\e[31ma thundewsowm is coming! pwepawe youwself!\e[0m" ;;
elif [[ "$weather_desc" == *"snow"* ]]; then *"rain"* | *"drizzle"*)
echo -e "\t\e[37mthewe is going to be snow today! be caweful outside~\e[0m" echo -e "\tLooks like it's raining today. Make sure you bring an umbrella with you."
elif [[ "$weather_desc" == "fog" || "$weather_desc" == "mist" ]]; then ;;
echo -e "\t\e[31mthe fog is coming owo~\e[0m" *"thunderstorm"*)
elif [[ "$weather_desc" == "smoke" || "$weather_desc" == "haze" ]]; then echo -e "\tA thunderstorm is coming! Prepare yourself!"
echo -e "\t\e[31msomebody is gwilling weally hawd today!! expect some smoke~\e[0m" ;;
fi *"snow"*)
echo -e "\tThere is going to be snow today! Be careful outside."
;;
"fog" | "mist")
echo -e "\tThe fog is coming."
;;
"smoke" | "haze")
echo -e "\tSomebody is grilling really hard today! Expect some smoke."
;;
esac
# Check for strong wind
wind_speed_rounded=$(echo "$wind_speed" | awk '{ print int($1) }') wind_speed_rounded=$(echo "$wind_speed" | awk '{ print int($1) }')
if [ "$wind_speed_rounded" -gt 8 ] ; then if [ "$wind_speed_rounded" -gt 8 ]; then
echo -e "\t\e[36mwind is stwong, be caweful!\e[0m" echo -e "\tWind is strong. Be careful!"
fi fi
# Special message for New Year's Eve
if [ "$(date +"%d/%m")" == "31/12" ]; then if [ "$(date +"%d/%m")" == "31/12" ]; then
echo -e "\t\e[35m\e[5mhappy new yeaw!!! >w<\e[0m" echo -e "\tHappy New Year!!!"
fi fi
temperaure_rounded=$(echo "$weather_data" | jq -r '.main.temp' | awk '{ print int($1) }') # Check for extreme temperatures
if [ "$temperaure_rounded" -gt 30 ] ; then temperature_rounded=$(echo "$weather_data" | jq -r '.main.temp' | awk '{ print int($1) }')
echo -e "\t\e[31mthe weathew is weally hot today, make suwe u dwink enough watew!\e[0m" if [ "$temperature_rounded" -gt 30 ]; then
elif [ "$temperaure_rounded" -lt 15 ] ; then echo -e "\tThe weather is really hot today. Make sure you drink enough water!"
echo -e "\t\e[34mthe weathew is weally cowd today, make suwe u dwess pwopewwy!\e[0m" elif [ "$temperature_rounded" -lt 15 ]; then
elif [ "$temperaure_rounded" -lt -20 ] ; then echo -e "\tThe weather is really cold today. Make sure you dress properly!"
echo -e "\t\e[37mit's fucking fweezing outside!! take cawe and dwess pwopewly!!! TwT\e[0m" elif [ "$temperature_rounded" -lt -20 ]; then
echo -e "\tIt's freezing outside!! Take care and dress properly!"
fi fi
echo -e "\e[32m$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' =)\e[0m" echo -e "$(printf '%*s\n' "${COLUMNS:-$(stty size 2>/dev/null | cut -d' ' -f2)}" '' | tr ' ' =)"
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# End #