Add weather string

This commit is contained in:
Archie Fox
2025-07-18 21:05:29 +03:00
parent 03f8c05905
commit 6ec9f6dbc0
2 changed files with 86 additions and 1 deletions

View File

@@ -3,6 +3,7 @@ package main
import (
"fmt"
"goclock/dict"
"goclock/weather"
"log"
"os/exec"
"time"
@@ -18,8 +19,9 @@ func main() {
minute := now.Minute()
timeStr := dict.WeekdayRussian(weekday) + " " + dict.DayRussian(day) + " " + dict.MonthRussian(month) + " " + dict.HourRussian(hour) + " " + dict.MinuteRussian(minute)
strWeather := weather.CompilationWeatherString("KUM")
command := exec.Command("sh", "-c", fmt.Sprintf("echo '%s' | RHVoice-test -p Anna", timeStr))
command := exec.Command("sh", "-c", fmt.Sprintf("echo '%s' | RHVoice-test -p Anna", timeStr+" "+strWeather))
err := command.Run()
if err != nil {

83
weather/weather.go Normal file
View File

@@ -0,0 +1,83 @@
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
}