Added support for Area Forecast Discussions issued by the NWS
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
This commit is contained in:
@@ -19,6 +19,9 @@ var pollDriverRegistrations = []pollDriverRegistration{
|
||||
{driver: "nws_alerts", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return nws.NewAlertsSource(cfg) }},
|
||||
{driver: "nws_forecast_hourly", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return nws.NewHourlyForecastSource(cfg) }},
|
||||
{driver: "nws_forecast_narrative", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return nws.NewNarrativeForecastSource(cfg) }},
|
||||
{driver: "nws_forecast_discussion", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) {
|
||||
return nws.NewForecastDiscussionSource(cfg)
|
||||
}},
|
||||
{driver: "openmeteo_observation", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return openmeteo.NewObservationSource(cfg) }},
|
||||
{driver: "openmeteo_forecast", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) { return openmeteo.NewForecastSource(cfg) }},
|
||||
{driver: "openweather_observation", factory: func(cfg config.SourceConfig) (fksource.PollSource, error) {
|
||||
|
||||
@@ -34,6 +34,19 @@ func TestRegisterBuiltinsRegistersNWSNarrativeForecastDriver(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterBuiltinsRegistersNWSForecastDiscussionDriver(t *testing.T) {
|
||||
reg := fksource.NewRegistry()
|
||||
RegisterBuiltins(reg)
|
||||
|
||||
in, err := reg.BuildInput(sourceConfigForDriver("nws_forecast_discussion"))
|
||||
if err != nil {
|
||||
t.Fatalf("BuildInput(nws_forecast_discussion) error = %v", err)
|
||||
}
|
||||
if _, ok := in.(fksource.PollSource); !ok {
|
||||
t.Fatalf("BuildInput(nws_forecast_discussion) type = %T, want PollSource", in)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterBuiltinsDoesNotRegisterLegacyNWSForecastDriver(t *testing.T) {
|
||||
reg := fksource.NewRegistry()
|
||||
RegisterBuiltins(reg)
|
||||
@@ -56,6 +69,7 @@ func TestRegisterBuiltinsRegistersAllCurrentDrivers(t *testing.T) {
|
||||
"nws_alerts",
|
||||
"nws_forecast_hourly",
|
||||
"nws_forecast_narrative",
|
||||
"nws_forecast_discussion",
|
||||
"openmeteo_observation",
|
||||
"openmeteo_forecast",
|
||||
"openweather_observation",
|
||||
|
||||
68
internal/sources/nws/forecast_discussion.go
Normal file
68
internal/sources/nws/forecast_discussion.go
Normal file
@@ -0,0 +1,68 @@
|
||||
package nws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
fksources "gitea.maximumdirect.net/ejr/feedkit/sources"
|
||||
nwscommon "gitea.maximumdirect.net/ejr/weatherfeeder/internal/providers/nws"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||
)
|
||||
|
||||
// ForecastDiscussionSource polls an NWS forecast discussion HTML page and emits a RAW discussion Event.
|
||||
//
|
||||
// Output schema:
|
||||
// - standards.SchemaRawNWSForecastDiscussionV1
|
||||
type ForecastDiscussionSource struct {
|
||||
http *fksources.HTTPSource
|
||||
}
|
||||
|
||||
func NewForecastDiscussionSource(cfg config.SourceConfig) (*ForecastDiscussionSource, error) {
|
||||
const driver = "nws_forecast_discussion"
|
||||
|
||||
hs, err := fksources.NewHTTPSource(driver, cfg, "text/html, application/xhtml+xml")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ForecastDiscussionSource{http: hs}, nil
|
||||
}
|
||||
|
||||
func (s *ForecastDiscussionSource) Name() string { return s.http.Name }
|
||||
|
||||
func (s *ForecastDiscussionSource) Kinds() []event.Kind {
|
||||
return []event.Kind{event.Kind("forecast_discussion")}
|
||||
}
|
||||
|
||||
func (s *ForecastDiscussionSource) Poll(ctx context.Context) ([]event.Event, error) {
|
||||
body, changed, err := s.http.FetchBytesIfChanged(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !changed {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
rawHTML := string(body)
|
||||
parsed, err := nwscommon.ParseForecastDiscussionHTML(rawHTML)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
issuedAt := parsed.IssuedAt.UTC()
|
||||
effectiveAt := &issuedAt
|
||||
emittedAt := time.Now().UTC()
|
||||
eventID := fksources.DefaultEventID("", s.http.Name, effectiveAt, emittedAt)
|
||||
|
||||
return fksources.SingleEvent(
|
||||
event.Kind("forecast_discussion"),
|
||||
s.http.Name,
|
||||
standards.SchemaRawNWSForecastDiscussionV1,
|
||||
eventID,
|
||||
emittedAt,
|
||||
effectiveAt,
|
||||
rawHTML,
|
||||
)
|
||||
}
|
||||
138
internal/sources/nws/forecast_discussion_test.go
Normal file
138
internal/sources/nws/forecast_discussion_test.go
Normal file
@@ -0,0 +1,138 @@
|
||||
package nws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.maximumdirect.net/ejr/feedkit/config"
|
||||
"gitea.maximumdirect.net/ejr/feedkit/event"
|
||||
"gitea.maximumdirect.net/ejr/weatherfeeder/standards"
|
||||
)
|
||||
|
||||
func TestForecastDiscussionSourcePollEmitsExpectedEvent(t *testing.T) {
|
||||
rawHTML := loadForecastDiscussionSampleHTML(t)
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = w.Write([]byte(rawHTML))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
src, err := NewForecastDiscussionSource(forecastDiscussionSourceConfig(srv.URL))
|
||||
if err != nil {
|
||||
t.Fatalf("NewForecastDiscussionSource() error = %v", err)
|
||||
}
|
||||
if got := src.Kinds(); len(got) != 1 || got[0] != event.Kind("forecast_discussion") {
|
||||
t.Fatalf("Kinds() = %#v, want [forecast_discussion]", got)
|
||||
}
|
||||
|
||||
events, err := src.Poll(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("Poll() error = %v", err)
|
||||
}
|
||||
if len(events) != 1 {
|
||||
t.Fatalf("Poll() len = %d, want 1", len(events))
|
||||
}
|
||||
|
||||
got := events[0]
|
||||
if got.Kind != event.Kind("forecast_discussion") {
|
||||
t.Fatalf("Kind = %q, want forecast_discussion", got.Kind)
|
||||
}
|
||||
if got.Schema != standards.SchemaRawNWSForecastDiscussionV1 {
|
||||
t.Fatalf("Schema = %q, want %q", got.Schema, standards.SchemaRawNWSForecastDiscussionV1)
|
||||
}
|
||||
wantEffectiveAt := time.Date(2026, 3, 28, 19, 24, 0, 0, time.UTC)
|
||||
if got.EffectiveAt == nil || !got.EffectiveAt.Equal(wantEffectiveAt) {
|
||||
t.Fatalf("EffectiveAt = %v, want %s", got.EffectiveAt, wantEffectiveAt.Format(time.RFC3339))
|
||||
}
|
||||
payload, ok := got.Payload.(string)
|
||||
if !ok {
|
||||
t.Fatalf("Payload type = %T, want string", got.Payload)
|
||||
}
|
||||
if payload != rawHTML {
|
||||
t.Fatalf("Payload did not preserve exact HTML")
|
||||
}
|
||||
}
|
||||
|
||||
func TestForecastDiscussionSourcePollReturnsNoEventsWhenUnchanged(t *testing.T) {
|
||||
rawHTML := loadForecastDiscussionSampleHTML(t)
|
||||
const etag = `"discussion-v1"`
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Header.Get("If-None-Match") == etag {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
w.Header().Set("ETag", etag)
|
||||
_, _ = w.Write([]byte(rawHTML))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
src, err := NewForecastDiscussionSource(forecastDiscussionSourceConfig(srv.URL))
|
||||
if err != nil {
|
||||
t.Fatalf("NewForecastDiscussionSource() error = %v", err)
|
||||
}
|
||||
|
||||
first, err := src.Poll(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("first Poll() error = %v", err)
|
||||
}
|
||||
if len(first) != 1 {
|
||||
t.Fatalf("first Poll() len = %d, want 1", len(first))
|
||||
}
|
||||
|
||||
second, err := src.Poll(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("second Poll() error = %v", err)
|
||||
}
|
||||
if len(second) != 0 {
|
||||
t.Fatalf("second Poll() len = %d, want 0", len(second))
|
||||
}
|
||||
}
|
||||
|
||||
func TestForecastDiscussionSourcePollRejectsInvalidHTML(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
_, _ = w.Write([]byte("<html><body><div>missing discussion block</div></body></html>"))
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
src, err := NewForecastDiscussionSource(forecastDiscussionSourceConfig(srv.URL))
|
||||
if err != nil {
|
||||
t.Fatalf("NewForecastDiscussionSource() error = %v", err)
|
||||
}
|
||||
|
||||
_, err = src.Poll(context.Background())
|
||||
if err == nil {
|
||||
t.Fatalf("Poll() error = nil, want error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), "glossaryProduct") {
|
||||
t.Fatalf("error = %q, want glossaryProduct context", err)
|
||||
}
|
||||
}
|
||||
|
||||
func forecastDiscussionSourceConfig(url string) config.SourceConfig {
|
||||
return config.SourceConfig{
|
||||
Name: "test-forecast-discussion-source",
|
||||
Driver: "nws_forecast_discussion",
|
||||
Mode: config.SourceModePoll,
|
||||
Params: map[string]any{
|
||||
"url": url,
|
||||
"user_agent": "test-agent",
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func loadForecastDiscussionSampleHTML(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
path := filepath.Join("..", "..", "providers", "nws", "testdata", "forecast_discussion_sample.html")
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("os.ReadFile(%q) error = %v", path, err)
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
Reference in New Issue
Block a user