-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathforecast.sh
73 lines (59 loc) · 2.91 KB
/
forecast.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/bin/bash
source printer.sh
get_weather_forecast() {
local location=$1
local apikey=$2
# Set OpenWeatherMap API key, write your own API key here
apikey=$(cat apikeyfile.txt)
API_KEY=$apikey
# Call OpenWeatherMap API and retrieve weather forecast data
forecast_data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=${location}&appid=${API_KEY}")
# Extract relevant weather information from JSON response using grep and cut
dates=$(echo "$forecast_data" | grep -o '"dt":[0-9]*' | cut -d ":" -f 2 | tr '\n' '|')
temperature=$(echo "$forecast_data" | grep -o '"temp":[^,]*' | cut -d ":" -f 2 | tr '\n' '|')
description=$(echo "$forecast_data" | grep -o '"description":"[^"]*"' | cut -d ":" -f 2 | tr -d '"' | tr '\n' '|')
humidity=$(echo "$forecast_data" | grep -o '"humidity":[^,]*' | cut -d ":" -f 2 | tr -d '}' | tr '\n' '|')
wind_speed=$(echo "$forecast_data" | grep -o '"speed":[^,]*' | cut -d ":" -f 2 | tr -d '}' | tr '\n' '|')
clouds=$(echo "$forecast_data" | grep -o '"all":[^,]*' | cut -d ":" -f 2 | tr -d '}' | tr '\n' '|')
# Function to convert Unix timestamp to readable date
if [[ "$OSTYPE" == "darwin"* ]]; then
convert_unix_to_date() {
local timestamp=$1
local formatted_date=$(date -r "$timestamp" +"%Y-%m-%d %H:%M:%S")
echo "$formatted_date"
}
else
convert_unix_to_date() {
local formatted_date=$(awk -v ts="$timestamp" 'BEGIN { print strftime("%Y-%m-%d %H:%M:%S", ts) }')
echo "$formatted_date"
}
fi
IFS='|' read -r -a dates_arr <<< "$dates"
IFS='|' read -r -a temperature_arr <<< "$temperature"
IFS='|' read -r -a description_arr <<< "$description"
IFS='|' read -r -a humidity_arr <<< "$humidity"
IFS='|' read -r -a wind_speed_arr <<< "$wind_speed"
IFS='|' read -r -a clouds_arr <<< "$clouds"
# Convert temperature from Kelvin to Celsius
for ((i = 0; i < ${#temperature_arr[@]}; i++)); do
temperature_arr[i]=$(echo "scale=2; ${temperature_arr[i]} - 273.15" | bc)
done
# Convert Unix timestamps to readable date format and associate weather information with each date
for ((i = 0; i < ${#dates_arr[@]}; i++)); do
curr_date="${dates_arr[i]}"
curr_temp="${temperature_arr[i]}"
curr_description="${description_arr[i]}"
curr_humidity="${humidity_arr[i]}"
curr_wind_speed="${wind_speed_arr[i]}"
curr_clouds="${clouds_arr[i]}"
if [[ $curr_date =~ ^[0-9]+$ ]]; then
if [[ $curr_date -gt 9999999999 ]]; then
curr_date=$(expr $curr_date / 1000)
fi
formatted_date=$(convert_unix_to_date "$curr_date")
print_information "$curr_description" $curr_temp $curr_humidity, $curr_wind_speed $curr_clouds "$formatted_date"
else
echo "Invalid timestamp: $curr_date"
fi
done
}