summaryrefslogtreecommitdiff
path: root/pkg/weather
diff options
context:
space:
mode:
authorNeonXP <i@neonxp.dev>2023-01-04 18:52:25 +0300
committerNeonXP <i@neonxp.dev>2023-01-04 18:52:25 +0300
commit5947c1d643dfc077c19d3b4c01e599578e1dfe62 (patch)
tree6723c5982626403a507ed25c74711ae74eccbe23 /pkg/weather
Diffstat (limited to 'pkg/weather')
-rw-r--r--pkg/weather/weather.go30
1 files changed, 30 insertions, 0 deletions
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
+}