Generated plugins (acceptance.json at the root) fail on: HTTP routes not registered through pluginsdk route or a public route reading personal columns; HTML built from unescaped non-constant strings; public POSTs outside route.Form; personal data names in log calls; outbound requests carrying personal data or values the spec does not send to that host. First-party plugins get the same findings as warnings. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
158 lines
3.7 KiB
Go
158 lines
3.7 KiB
Go
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(`<section class="weather-forecast">`)
|
|
if heading != "" {
|
|
b.WriteString("<h2>" + html.EscapeString(heading) + "</h2>")
|
|
}
|
|
if len(days) == 0 {
|
|
b.WriteString("<p>" + html.EscapeString(note) + "</p>")
|
|
} else {
|
|
if town != "" {
|
|
b.WriteString("<p>" + html.EscapeString(town) + "</p>")
|
|
}
|
|
b.WriteString("<ul>")
|
|
for _, d := range days {
|
|
b.WriteString(fmt.Sprintf("<li><strong>%s</strong>: %s, %.0f°C / %.0f°C</li>",
|
|
html.EscapeString(d.Date), html.EscapeString(describeCode(d.Code)), d.Max, d.Min))
|
|
}
|
|
b.WriteString("</ul>")
|
|
}
|
|
b.WriteString("</section>")
|
|
return b.String()
|
|
}
|