feat(summary): send complete articles within user budget (#39)
This commit is contained in:
@ -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
|
||||
|
||||
@ -54,7 +54,7 @@ func TestTicket36EnrichmentValidationAndUTF8Limit(t *testing.T) {
|
||||
default:
|
||||
content := ""
|
||||
if scenario == "oversize" {
|
||||
content = strings.Repeat("🙂é", maxContentBytes)
|
||||
content = strings.Repeat("🙂é", 32*1024)
|
||||
}
|
||||
body, _ := json.Marshal(map[string]any{"articles": []any{map[string]any{"url": url, "content": content, "published_at": nil}}})
|
||||
raw = string(body)
|
||||
@ -66,10 +66,10 @@ func TestTicket36EnrichmentValidationAndUTF8Limit(t *testing.T) {
|
||||
if err != nil || len(got) != 1 || !got[0].PublishedAt.Equal(existing) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got[0].Content) > maxContentBytes || !utf8.ValidString(got[0].Content) {
|
||||
if !utf8.ValidString(got[0].Content) || scenario == "oversize" && got[0].Content != strings.Repeat("🙂é", 32*1024) {
|
||||
t.Fatal("invalid content boundary")
|
||||
}
|
||||
if got[0].ContentTruncated != (scenario == "oversize") {
|
||||
if got[0].ContentTruncated {
|
||||
t.Fatal("missing limit flag")
|
||||
}
|
||||
} else if err == nil || got != nil || strings.Contains(err.Error(), "PRIVATE") || strings.Contains(err.Error(), "http") {
|
||||
|
||||
@ -0,0 +1,41 @@
|
||||
package bloomberg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/tradarr/backend/internal/scraper"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTicket39BloombergFixtureCompleteStateAndLongBody(t *testing.T) {
|
||||
raw, err := os.ReadFile("testdata/enriched_complete.json")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, long := range []bool{false, true} {
|
||||
var response struct {
|
||||
Articles []map[string]any `json:"articles"`
|
||||
}
|
||||
if json.Unmarshal(raw, &response) != nil {
|
||||
t.Fatal("bad fixture")
|
||||
}
|
||||
want := "Synthetic first editorial paragraph.\n\nFinal editorial paragraph."
|
||||
if long {
|
||||
want = strings.Repeat(want, 600)
|
||||
response.Articles[0]["content"] = want
|
||||
}
|
||||
payload, _ := json.Marshal(response)
|
||||
b := New("http://fixture.invalid")
|
||||
b.client = &http.Client{Transport: dateTransport(func(*http.Request) (*http.Response, error) {
|
||||
return &http.Response{StatusCode: 200, Body: io.NopCloser(strings.NewReader(string(payload))), Header: make(http.Header)}, nil
|
||||
})}
|
||||
got, err := b.EnrichWithCredentials(context.Background(), "fixture", "fixture", []scraper.Article{{Title: "Original", URL: "https://www.bloomberg.com/news/articles/fixture"}})
|
||||
if err != nil || len(got) != 1 || got[0].Content != want || scraper.ContentStatus(got[0]) != scraper.ContentComplete || got[0].PublishedAt == nil || got[0].ContentTruncated {
|
||||
t.Fatal("complete full-body DTO lost")
|
||||
}
|
||||
}
|
||||
}
|
||||
1
backend/internal/scraper/bloomberg/testdata/enriched_complete.json
vendored
Normal file
1
backend/internal/scraper/bloomberg/testdata/enriched_complete.json
vendored
Normal file
@ -0,0 +1 @@
|
||||
{"articles":[{"url":"https://www.bloomberg.com/news/articles/fixture","content":"Synthetic first editorial paragraph.\n\nFinal editorial paragraph.","content_state":"complete","content_truncated":false,"published_at":"2026-01-02T03:04:05Z"}]}
|
||||
Reference in New Issue
Block a user