fix(scrapers): add sanitized collection diagnostics

Report source, HTTP status, article counts, and outcome without logging response bodies; preserve the existing extraction and parsing flow.

Refs #20

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 15:08:35 +01:00
parent d65222169a
commit 4131cf903e
4 changed files with 224 additions and 17 deletions

View File

@ -4,7 +4,6 @@ import (
"context"
"encoding/xml"
"fmt"
"io"
"net/http"
"strings"
"time"
@ -45,7 +44,6 @@ func (w *WatcherGuru) Scrape(ctx context.Context, _ []string) ([]scraper.Article
} {
articles, err := w.fetchRSS(ctx, feedURL)
if err == nil && len(articles) > 0 {
fmt.Printf("watcherguru rss: %d articles\n", len(articles))
return articles, nil
}
}
@ -55,11 +53,24 @@ func (w *WatcherGuru) Scrape(ctx context.Context, _ []string) ([]scraper.Article
if err != nil {
return nil, fmt.Errorf("watcherguru: %w", err)
}
fmt.Printf("watcherguru html: %d articles\n", len(articles))
return articles, nil
}
func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.Article, error) {
func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) (articles []scraper.Article, resultErr error) {
status := 0
feedLabel := "unknown"
if feedURL == baseURL+"/feed/" {
feedLabel = "/feed/"
} else if feedURL == baseURL+"/news/feed/" {
feedLabel = "/news/feed/"
}
defer func() {
outcome := "ok"
if resultErr != nil {
outcome = "failed"
}
fmt.Printf("watcherguru rss feed=%s status=%d articles=%d outcome=%s\n", feedLabel, status, len(articles), outcome)
}()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, feedURL, nil)
if err != nil {
return nil, err
@ -72,6 +83,7 @@ func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.A
}
defer resp.Body.Close()
status = resp.StatusCode
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
@ -81,7 +93,6 @@ func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.A
return nil, fmt.Errorf("parse RSS: %w", err)
}
var articles []scraper.Article
for _, item := range feed.Channel.Items {
title := strings.TrimSpace(item.Title)
link := strings.TrimSpace(item.Link)
@ -109,7 +120,15 @@ func (w *WatcherGuru) fetchRSS(ctx context.Context, feedURL string) ([]scraper.A
return articles, nil
}
func (w *WatcherGuru) scrapeHTML(ctx context.Context) ([]scraper.Article, error) {
func (w *WatcherGuru) scrapeHTML(ctx context.Context) (articles []scraper.Article, resultErr error) {
status := 0
defer func() {
outcome := "ok"
if resultErr != nil {
outcome = "failed"
}
fmt.Printf("watcherguru html status=%d articles=%d outcome=%s\n", status, len(articles), outcome)
}()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, baseURL+"/news/", nil)
if err != nil {
return nil, err
@ -123,9 +142,9 @@ func (w *WatcherGuru) scrapeHTML(ctx context.Context) ([]scraper.Article, error)
}
defer resp.Body.Close()
status = resp.StatusCode
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
return nil, fmt.Errorf("HTTP %d: %s", resp.StatusCode, body)
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
}
doc, err := html.Parse(resp.Body)
@ -133,7 +152,6 @@ func (w *WatcherGuru) scrapeHTML(ctx context.Context) ([]scraper.Article, error)
return nil, fmt.Errorf("parse HTML: %w", err)
}
var articles []scraper.Article
seen := make(map[string]bool)
now := time.Now()