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:
@ -4,7 +4,6 @@ import (
|
||||
"context"
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
@ -49,16 +48,31 @@ func (y *YahooFinance) Scrape(ctx context.Context, symbols []string) ([]scraper.
|
||||
}
|
||||
items, err := y.fetchSymbol(ctx, symbol)
|
||||
if err != nil {
|
||||
fmt.Printf("yahoofinance %s: %v\n", symbol, err)
|
||||
continue
|
||||
}
|
||||
articles = append(articles, items...)
|
||||
fmt.Printf("yahoofinance %s: %d articles fetched\n", symbol, len(items))
|
||||
}
|
||||
return articles, nil
|
||||
}
|
||||
|
||||
func (y *YahooFinance) fetchSymbol(ctx context.Context, symbol string) ([]scraper.Article, error) {
|
||||
func (y *YahooFinance) fetchSymbol(ctx context.Context, symbol string) (articles []scraper.Article, resultErr error) {
|
||||
status := 0
|
||||
defer func() {
|
||||
outcome := "ok"
|
||||
if resultErr != nil {
|
||||
outcome = "failed"
|
||||
}
|
||||
safeSymbol := strings.Map(func(r rune) rune {
|
||||
if r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || strings.ContainsRune(" .-^=", r) {
|
||||
return r
|
||||
}
|
||||
return '_'
|
||||
}, symbol)
|
||||
if len(safeSymbol) > 64 {
|
||||
safeSymbol = safeSymbol[:64]
|
||||
}
|
||||
fmt.Printf("yahoofinance symbol=%s yahoo_symbol=%s status=%d articles=%d outcome=%s\n", safeSymbol, safeSymbol, status, len(articles), outcome)
|
||||
}()
|
||||
url := fmt.Sprintf(
|
||||
"https://feeds.finance.yahoo.com/rss/2.0/headline?s=%s®ion=US&lang=en-US",
|
||||
symbol,
|
||||
@ -76,9 +90,9 @@ func (y *YahooFinance) fetchSymbol(ctx context.Context, symbol string) ([]scrape
|
||||
}
|
||||
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, string(body))
|
||||
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
|
||||
var feed rssFeed
|
||||
@ -87,8 +101,6 @@ func (y *YahooFinance) fetchSymbol(ctx context.Context, symbol string) ([]scrape
|
||||
}
|
||||
|
||||
const maxPerSymbol = 5
|
||||
|
||||
var articles []scraper.Article
|
||||
for _, item := range feed.Channel.Items {
|
||||
if len(articles) >= maxPerSymbol {
|
||||
break
|
||||
|
||||
Reference in New Issue
Block a user