feat(scraper): bound article enrichment diagnostics (#40)
This commit is contained in:
@ -15,16 +15,34 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"github.com/tradarr/backend/internal/scraper/sourcehttp"
|
||||
"golang.org/x/net/html"
|
||||
)
|
||||
|
||||
const feedURL = "https://www.cnbc.com/id/10001147/device/rss/rss.html"
|
||||
const maxFeedBytes = 2 * 1024 * 1024
|
||||
|
||||
type CNBCBusiness struct{ client *http.Client }
|
||||
type CNBCBusiness struct {
|
||||
client *http.Client
|
||||
diagnostics io.Writer
|
||||
}
|
||||
|
||||
func New() *CNBCBusiness {
|
||||
return &CNBCBusiness{client: &http.Client{Timeout: 15 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}
|
||||
return &CNBCBusiness{client: &http.Client{Transport: sourcehttp.NewTransport(nil, nil), Timeout: 15 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}}
|
||||
}
|
||||
|
||||
// NewWithClient preserves the production collector/enricher with injected I/O.
|
||||
func NewWithClient(client *http.Client, diagnostics io.Writer) *CNBCBusiness {
|
||||
c := New()
|
||||
if client != nil {
|
||||
injected := *client
|
||||
// RSS discovery retains its production no-redirect policy. Article
|
||||
// enrichment applies its own bounded same-source redirect validation.
|
||||
injected.CheckRedirect = c.client.CheckRedirect
|
||||
c.client = &injected
|
||||
}
|
||||
c.diagnostics = diagnostics
|
||||
return c
|
||||
}
|
||||
func (*CNBCBusiness) Name() string { return "cnbc_business" }
|
||||
|
||||
@ -50,7 +68,11 @@ func (c *CNBCBusiness) Scrape(ctx context.Context, _ []string) (articles []scrap
|
||||
if resultErr != nil {
|
||||
outcome = "failed"
|
||||
}
|
||||
log.Printf("source=cnbc_business rss status=%d articles=%d outcome=%s cause=%s", status, len(articles), outcome, cause)
|
||||
logger := log.Default()
|
||||
if c.diagnostics != nil {
|
||||
logger = log.New(c.diagnostics, "", 0)
|
||||
}
|
||||
logger.Printf("source=cnbc_business rss status=%d articles=%d outcome=%s cause=%s", status, len(articles), outcome, cause)
|
||||
}()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, feedURL, nil)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user