84 lines
1.7 KiB
Go
84 lines
1.7 KiB
Go
package weather
|
|
|
|
import (
|
|
"encoding/json"
|
|
"io"
|
|
"math"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
func inflect(d float64) string {
|
|
n := math.Abs(d) // убираем минус
|
|
deg := int(n)
|
|
deg = deg % 100 // две последние цифры
|
|
n1 := int(n) % 10 // последняя цифра
|
|
|
|
form := ""
|
|
|
|
if deg > 10 && deg < 20 {
|
|
form = "градусов"
|
|
} else if n1 > 1 && n1 < 5 {
|
|
form = "градуса"
|
|
} else if n1 == 1 {
|
|
form = "градус"
|
|
} else {
|
|
form = "градусов"
|
|
}
|
|
|
|
return form
|
|
}
|
|
|
|
func cond(deg int) string {
|
|
f := ""
|
|
if deg < 0 {
|
|
f = " мороза"
|
|
} else if deg >= 1 {
|
|
f = " тепла"
|
|
}
|
|
|
|
return f
|
|
}
|
|
|
|
type Weather struct {
|
|
City string `json:"name"`
|
|
Main struct {
|
|
Temp float32 `json:"temp"`
|
|
} `json:"main"`
|
|
Weather []struct {
|
|
Description string `json:"description"`
|
|
} `json:"weather"`
|
|
}
|
|
|
|
func getWeather(city string) *Weather {
|
|
geo := map[string]string{
|
|
"VLG": "472757",
|
|
"KUM": "539221",
|
|
}
|
|
weatherString := "http://api.openweathermap.org/data/2.5/weather?id=" + geo[city] + "&lang=ru&appid=6f9d925282c57d29a95977d346e2d528&units=metric"
|
|
resp, _ := http.Get(weatherString)
|
|
|
|
body, _ := io.ReadAll(resp.Body)
|
|
|
|
var weather Weather
|
|
json.Unmarshal(body, &weather)
|
|
|
|
return &weather
|
|
}
|
|
|
|
func CompilationWeatherString(city string) string {
|
|
geo := ""
|
|
if city == "KUM" {
|
|
geo = "станице кум*ылженской"
|
|
} else if city == "VLG" {
|
|
geo = "городе волгоград"
|
|
}
|
|
weatherStruct := getWeather(city)
|
|
|
|
deg := int(math.Abs(float64(weatherStruct.Main.Temp)))
|
|
degForm := inflect(float64(weatherStruct.Main.Temp))
|
|
condition := cond(deg)
|
|
|
|
return "Сейчас в " + geo + " " + strconv.Itoa(deg) + " " + degForm + condition + weatherStruct.Weather[0].Description
|
|
}
|