-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.sh
executable file
·138 lines (130 loc) · 3.39 KB
/
weather.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/sh
# --------------------------------------------------------------------------
#
# Very simple script to grab local weather from Open-Meteo API, for provided
# latitude and longitute.
# Usage: ./weather.sh {latitude} {longitute}
#
# Open-Meteo: https://open-meteo.com/
#
# Author: Aggelos Stamatiou, Feb 2025
#
# This source code is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this source code. If not, see <http://www.gnu.org/licenses/>.
# --------------------------------------------------------------------------
# Auxilary function to map provided weather code to description.
weather_description() {
case $1 in
0)
echo "Clear sky"
;;
1)
echo "Mainly clear"
;;
2)
echo "Partly cloudy"
;;
3)
echo "Overcast"
;;
45)
echo "Fog"
;;
48)
echo "Depositing rime fog"
;;
51)
echo "Light drizzle"
;;
53)
echo "Moderate drizzle"
;;
55)
echo "Dense drizzle"
;;
56)
echo "Light freezing drizzle"
;;
57)
echo "Dense freezing drizzle"
;;
61)
echo "Slight rain"
;;
63)
echo "Moderate rain"
;;
65)
echo "Heavy rain"
;;
66)
echo "Light freezing rain"
;;
67)
echo "Heavy freezing rain"
;;
71)
echo "Slight snow fall"
;;
73)
echo "Moderate snow fall"
;;
75)
echo "Heavy snow fall"
;;
77)
echo "Snow grains"
;;
80)
echo "Slight rain showers"
;;
81)
echo "Moderate rain showers"
;;
82)
echo "Violent rain showers"
;;
85)
echo "Slight snow showers"
;;
86)
echo "Heavy snow showers"
;;
95)
echo "Thunderstorm"
;;
96)
echo "Thunderstorm with slight hail"
;;
99)
echo "Thunderstrom with heavy hail"
;;
*)
echo "Unknown weather code"
;;
esac
}
# Execute request
req="https://api.open-meteo.com/v1/forecast?latitude=$1&longitude=$2¤t_weather=true"
result="$(curl -s $req)"
# Extract weather code description
prefix="\"weathercode\"\:"
pattern="$prefix\d+"
string=$(echo "$result" | grep -oP "$pattern")
desc=$(weather_description ${string/#$prefix})
# Extract temperature
prefix="\"temperature\"\:"
pattern="$prefix[\-]?\d+\.\d+"
string=$(echo "$result" | grep -oP "$pattern")
temp="${string/#$prefix}°C"
echo "$desc, $temp"