From 5947c1d643dfc077c19d3b4c01e599578e1dfe62 Mon Sep 17 00:00:00 2001 From: NeonXP Date: Wed, 4 Jan 2023 18:52:25 +0300 Subject: init --- pkg/weather/weather.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 pkg/weather/weather.go (limited to 'pkg/weather/weather.go') diff --git a/pkg/weather/weather.go b/pkg/weather/weather.go new file mode 100644 index 0000000..4674ee1 --- /dev/null +++ b/pkg/weather/weather.go @@ -0,0 +1,30 @@ +package weather + +import ( + "fmt" + "io" + "net/http" + "time" +) + +func Get(city string) (string, error) { + client := http.Client{ + Timeout: 30 * time.Second, + } + u := fmt.Sprintf(`https://wttr.in/%s?format=%%l:+%%t+(ощущается+как+%%f)+%%c+%%C`, city) + req, _ := http.NewRequest(http.MethodGet, u, nil) + req.Header.Set("Accept-Language", "ru") + resp, err := client.Do(req) + if err != nil { + return "", err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + return "", fmt.Errorf("Не смог получить погоду") + } + result, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + return string(result), nil +} -- cgit v1.2.3