feat(summary): send complete articles within user budget (#39)

This commit is contained in:
2026-11-24 17:39:26 +01:00
parent d0c7cb7cae
commit 79953ce49f
73 changed files with 1688 additions and 288 deletions

View File

@ -9,11 +9,11 @@ import (
"github.com/tradarr/backend/internal/scraper"
"io"
"net/http"
"strings"
"time"
"unicode/utf8"
)
const maxContentBytes = 16 * 1024
const maxContentBytes = 2 * 1024 * 1024
const enrichmentTimeout = 100 * time.Second // Node total budget 90s plus transport margin.
func (b *Bloomberg) EnrichWithCredentials(ctx context.Context, username, password string, articles []scraper.Article) ([]scraper.Article, error) {
out := append([]scraper.Article(nil), articles...)
@ -44,7 +44,7 @@ func (b *Bloomberg) EnrichWithCredentials(ctx context.Context, username, passwor
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("bloomberg: enrichment HTTP %d", resp.StatusCode)
}
maximum := int64(len(out))*(6*maxContentBytes+8192) + 1024
maximum := min(int64(len(out))*(6*maxContentBytes+8192)+1024, 32*1024*1024)
raw, err := io.ReadAll(io.LimitReader(resp.Body, maximum+1))
if err != nil {
return nil, scraperFailure(ctx, err)
@ -54,6 +54,7 @@ func (b *Bloomberg) EnrichWithCredentials(ctx context.Context, username, passwor
}
var result struct {
Articles []struct {
ContentState string `json:"content_state"`
URL string `json:"url"`
Content string `json:"content"`
ContentTruncated bool `json:"content_truncated"`
@ -70,13 +71,15 @@ func (b *Bloomberg) EnrichWithCredentials(ctx context.Context, username, passwor
}
content := a.Content
truncated := a.ContentTruncated
if len(content) > maxContentBytes {
content = content[:maxContentBytes]
for !utf8.ValidString(content) {
content = content[:len(content)-1]
}
truncated = true
state := a.ContentState
if state != scraper.ContentComplete || truncated || len(content) > maxContentBytes {
state = scraper.ContentIncomplete
}
if strings.TrimSpace(content) == "" {
state = scraper.ContentUnavailable
}
out[i].ContentState = state
out[i].Content = content
out[i].ContentTruncated = truncated && content != ""
var date string