package main import ( "context" "encoding/json" "errors" "fmt" "html" "net/http" "net/url" "strconv" "strings" "time" ) type day struct { Date string Max float64 Min float64 Code int } type openMeteoResponse struct { Daily struct { Time []string `json:"time"` Code []int `json:"weather_code"` Max []float64 `json:"temperature_2m_max"` Min []float64 `json:"temperature_2m_min"` } `json:"daily"` } func describeCode(code int) string { switch { case code == 0: return "Clear sky" case code >= 1 && code <= 3: return "Partly cloudy" case code == 45 || code == 48: return "Fog" case code >= 51 && code <= 57: return "Drizzle" case code >= 61 && code <= 67, code >= 80 && code <= 82: return "Rain" case code >= 71 && code <= 77, code == 85 || code == 86: return "Snow" case code >= 95: return "Thunderstorms" } return "Mixed conditions" } // parseDays reads the days_shown content value, clamped to 1..7, default 3. func parseDays(v any) int { n := 3 switch t := v.(type) { case string: if p, err := strconv.Atoi(strings.TrimSpace(t)); err == nil { n = p } case float64: n = int(t) case int: n = t } if n < 1 { return 3 } if n > 7 { return 7 } return n } func settingFloat(m map[string]any, key string) (float64, bool) { switch t := m[key].(type) { case float64: return t, true case string: f, err := strconv.ParseFloat(strings.TrimSpace(t), 64) return f, err == nil } return 0, false } func validCoords(lat, lon float64) bool { return lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180 } func fetchForecast(ctx context.Context, rt http.RoundTripper, lat, lon float64, days int) ([]day, error) { if rt == nil { return nil, errors.New("no outbound transport") } if !validCoords(lat, lon) { return nil, errors.New("invalid coordinates") } q := url.Values{} q.Set("latitude", strconv.FormatFloat(lat, 'f', 4, 64)) q.Set("longitude", strconv.FormatFloat(lon, 'f', 4, 64)) q.Set("daily", "weather_code,temperature_2m_max,temperature_2m_min") q.Set("timezone", "auto") q.Set("forecast_days", strconv.Itoa(days)) ctx, cancel := context.WithTimeout(ctx, 8*time.Second) defer cancel() req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.open-meteo.com/v1/forecast?"+q.Encode(), nil) if err != nil { return nil, err } client := http.Client{Transport: rt} resp, err := client.Do(req) if err != nil { return nil, err } defer func() { _ = resp.Body.Close() }() if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("forecast service status %d", resp.StatusCode) } var body openMeteoResponse if err := json.NewDecoder(http.MaxBytesReader(nil, resp.Body, 1<<20)).Decode(&body); err != nil { return nil, err } d := body.Daily n := len(d.Time) if len(d.Code) < n || len(d.Max) < n || len(d.Min) < n { return nil, errors.New("incomplete forecast data") } if n > days { n = days } out := make([]day, 0, n) for i := 0; i < n; i++ { out = append(out, day{Date: d.Time[i], Max: d.Max[i], Min: d.Min[i], Code: d.Code[i]}) } return out, nil } func renderForecast(heading, town string, days []day, note string) string { var b strings.Builder b.WriteString(`
`) if heading != "" { b.WriteString("

" + html.EscapeString(heading) + "

") } if len(days) == 0 { b.WriteString("

" + html.EscapeString(note) + "

") } else { if town != "" { b.WriteString("

" + html.EscapeString(town) + "

") } b.WriteString("") } b.WriteString("
") return b.String() }