All checks were successful
ci/woodpecker/push/build-image Pipeline was successful
69 lines
1.7 KiB
Go
69 lines
1.7 KiB
Go
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,
|
|
)
|
|
}
|