Initial commit of weather.sh script

This script retrieves current weather information from OpenWeatherMap API. It includes:
- OpenWeatherMap API key for data retrieval.
- Location settings for specifying the city and country.
- API request using cURL to fetch weather data.
- Extraction of relevant information from the JSON response using jq.
- Conversion of temperature from Kelvin to Celsius.
- Formulation of the weather information in a formatted English text.
- Output of the weather information.

Author: Michael Haider
Date: 2024-02-08
This commit is contained in:
Ratatoskr 2024-02-08 16:03:47 +01:00
parent 0a77d93643
commit a61f9f2d2d
Signed by: Ratatoskr
GPG Key ID: 28B77439A6D78F4E

40
weather.sh Normal file
View File

@ -0,0 +1,40 @@
#!/bin/bash
################################################################################
# OpenWeatherMap Weather Script
# Author: Michael Haider
# Date: 2024-02-08
#
# This script retrieves current weather information from OpenWeatherMap API
# based on the specified city and country. It then extracts relevant data,
# converts temperature from Kelvin to Celsius, and outputs the information
# in a formatted English text.
################################################################################
# OpenWeatherMap API key
api_key="da7765a8c6d2975b7dfe68f2a1775631"
# Location settings
city="Pfarrkirchen"
country="de"
# Make API request and retrieve data
weather_data=$(curl -s "http://api.openweathermap.org/data/2.5/weather?q=${city},${country}&appid=${api_key}")
# Extract relevant information from the JSON response
description=$(echo "$weather_data" | jq -r '.weather[0].description')
temperature=$(echo "$weather_data" | jq -r '.main.temp')
humidity=$(echo "$weather_data" | jq -r '.main.humidity')
wind_speed=$(echo "$weather_data" | jq -r '.wind.speed')
# Convert temperature from Kelvin to Celsius
temperature_celsius=$(awk "BEGIN {print $temperature - 273.15}")
# Formulate the text in English
weather="Current weather in $city: $description.
The temperature is ${temperature_celsius}°C, humidity is ${humidity}%.
Wind speed is ${wind_speed} m/s."
# Output the text
echo "$weather"