feat(generation): select articles in three passes

This commit is contained in:
2026-11-24 00:38:02 +01:00
parent d22f84529f
commit 8022ee0547
27 changed files with 916 additions and 68 deletions

View File

@ -36,8 +36,9 @@ type scraperRequest struct {
}
type scraperArticle struct {
Title string `json:"title"`
URL string `json:"url"`
Title string `json:"title"`
URL string `json:"url"`
PublishedAt json.RawMessage `json:"published_at"`
}
type scraperResponse struct {
@ -72,7 +73,6 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
return nil, fmt.Errorf("bloomberg: %s", result.Error)
}
now := time.Now()
var articles []scraper.Article
for _, a := range result.Articles {
title := strings.TrimSpace(a.Title)
@ -80,12 +80,19 @@ func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, passwor
if title == "" || url == "" {
continue
}
var date *time.Time
var rawDate string
if json.Unmarshal(a.PublishedAt, &rawDate) == nil {
if parsed, err := time.Parse(time.RFC3339Nano, rawDate); err == nil {
date = &parsed
}
}
syms := scraper.DetectSymbols(title, symbols)
articles = append(articles, scraper.Article{
Title: title,
Content: title,
URL: url,
PublishedAt: &now,
PublishedAt: date,
Symbols: syms,
})
}

View File

@ -0,0 +1,28 @@
package bloomberg
import (
"context"
"io"
"net/http"
"strings"
"testing"
"time"
)
type dateTransport func(*http.Request) (*http.Response, error)
func (f dateTransport) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestTicket34BloombergUsesOnlyRealPublicationDates(t *testing.T) {
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(`{"articles":[{"title":"fixture-a","url":"https://fixture.invalid/a","published_at":"2026-01-02T03:04:05.000Z"},{"title":"fixture-b","url":"https://fixture.invalid/b","published_at":null},{"title":"fixture-c","url":"https://fixture.invalid/c","published_at":"invalid"}]}`)), Header: make(http.Header)}, nil
})}
got, err := b.ScrapeWithCredentials(context.Background(), "fixture", "fixture", nil)
if err != nil || len(got) != 3 {
t.Fatal(err)
}
want := time.Date(2026, 1, 2, 3, 4, 5, 0, time.UTC)
if got[0].PublishedAt == nil || !got[0].PublishedAt.Equal(want) || got[1].PublishedAt != nil || got[2].PublishedAt != nil {
t.Fatal("publication date invented or lost")
}
}