Files
TradarrV2/backend/internal/scraper/quanwenrss/collector.go

120 lines
3.4 KiB
Go

package quanwenrss
import (
"context"
"errors"
"io"
"net/http"
"net/url"
"time"
"github.com/tradarr/backend/internal/scraper"
"github.com/tradarr/backend/internal/scraper/sourcehttp"
"github.com/tradarr/backend/internal/sourcefeeds"
)
const MaxSourceBytes = 12 * 1024 * 1024
var errUnavailable = errors.New("rss_source_unavailable")
type FeedLoader interface {
SourceFeedURLs(context.Context, string) ([]string, error)
}
type Collector struct {
source string
loader FeedLoader
client *http.Client
}
func New(source string, loader FeedLoader, client *http.Client) *Collector {
if client == nil {
client = &http.Client{Transport: sourcehttp.NewTransport(nil, nil)}
}
bounded := *client
bounded.Timeout = 10 * time.Second
// Redirects are revalidated against the exact source/path allowlist; article
// hosts and cross-source paths are never delegated to the transport.
bounded.CheckRedirect = func(r *http.Request, via []*http.Request) error {
if len(via) >= 3 {
return http.ErrUseLastResponse
}
normalized, err := sourcefeeds.Validate(source, []string{r.URL.String()})
if err != nil {
return http.ErrUseLastResponse
}
r.URL, _ = url.Parse(normalized[0])
return nil
}
return &Collector{source: source, loader: loader, client: &bounded}
}
func (c *Collector) Name() string { return c.source }
func (c *Collector) Scrape(ctx context.Context, symbols []string) ([]scraper.Article, error) {
articles, _, err := c.ScrapeWithDiagnostics(ctx, symbols)
return articles, err
}
func (c *Collector) ScrapeWithDiagnostics(ctx context.Context, _ []string) ([]scraper.Article, scraper.FeedCounts, error) {
counts := scraper.FeedCounts{}
if c.loader == nil {
return nil, counts, errUnavailable
}
values, err := c.loader.SourceFeedURLs(ctx, c.source)
if err != nil {
return nil, counts, errUnavailable
}
values, err = sourcefeeds.Validate(c.source, values)
if err != nil {
return nil, counts, errUnavailable
}
ctx, cancel := context.WithTimeout(ctx, 45*time.Second)
defer cancel()
pools := [][]scraper.Article{}
consumed := 0
for _, value := range values {
counts.Attempted++
articles, n, err := c.fetch(ctx, value, MaxSourceBytes-consumed)
consumed += n
if err != nil {
counts.Failed++
continue
}
counts.Succeeded++
pools = append(pools, articles)
}
if counts.Succeeded == 0 {
return nil, counts, errUnavailable
}
return Merge(pools), counts, nil
}
func (c *Collector) fetch(ctx context.Context, value string, remaining int) ([]scraper.Article, int, error) {
if remaining <= 0 || ctx.Err() != nil {
return nil, 0, errUnavailable
}
// Defense in depth: never trust a stored configuration without validation.
if _, err := sourcefeeds.Validate(c.source, []string{value}); err != nil {
return nil, 0, errUnavailable
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, value, nil)
if err != nil {
return nil, 0, errUnavailable
}
req.Header.Set("Accept", "application/rss+xml, application/xml")
resp, err := c.client.Do(req)
if err != nil {
return nil, 0, errUnavailable
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, 0, errUnavailable
}
limit := min(remaining, MaxFeedBytes)
raw, err := io.ReadAll(io.LimitReader(resp.Body, int64(limit)+1))
if err != nil || len(raw) > limit {
return nil, len(raw), errUnavailable
}
articles, err := Parse(raw, c.source)
if err != nil {
return nil, len(raw), errUnavailable
}
return articles, len(raw), nil
}