40 lines
1.2 KiB
Bash
Executable File
40 lines
1.2 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
API_KEY="99631af2d6db903d1f689c7d2cb13764"
|
|
CITY_ID="5809844"
|
|
UNITS="metric"
|
|
WEATHER_URL="http://api.openweathermap.org/data/2.5/weather?id=$CITY_ID&units=$UNITS&appid=$API_KEY"
|
|
|
|
celsius_to_fahrenheit() {
|
|
awk "BEGIN { printf \"%.0f\", ($1 * 9 / 5) + 32 }"
|
|
}
|
|
|
|
get_weather_icon() {
|
|
desc="$1"
|
|
case "$desc" in
|
|
"clear sky") echo "☀️" ;;
|
|
"few clouds") echo "🌤️" ;;
|
|
"scattered clouds") echo "🌥️" ;;
|
|
"broken clouds" | "overcast clouds") echo "☁️" ;;
|
|
"shower rain" | "light rain" | "light intensity drizzle" | "moderate rain" | "rain") echo "🌧️" ;;
|
|
"thunderstorm") echo "⛈️" ;;
|
|
"snow") echo "❄️" ;;
|
|
"mist" | "haze" | "smoke" | "fog") echo "🌫️" ;;
|
|
*) echo "❓" ;;
|
|
esac
|
|
}
|
|
|
|
response=$(curl -s "$WEATHER_URL")
|
|
if [ -z "$response" ]; then
|
|
echo "Error: Empty response from weather API"
|
|
exit 1
|
|
fi
|
|
|
|
# Extract fields using jq
|
|
desc=$(echo "$response" | jq -r '.weather[0].description')
|
|
temp_c=$(echo "$response" | jq -r '.main.temp' | awk '{ printf "%.0f", $1 }')
|
|
temp_f=$(celsius_to_fahrenheit "$temp_c")
|
|
icon=$(get_weather_icon "$desc")
|
|
|
|
printf "%s %s°C (%s°F)\n" "$icon" "$temp_c" "$temp_f"
|