135 lines
3.6 KiB
Go
135 lines
3.6 KiB
Go
package bloomberg
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
)
|
|
|
|
type Bloomberg struct {
|
|
scraperURL string
|
|
client *http.Client
|
|
diagnostics io.Writer
|
|
}
|
|
|
|
func New(scraperURL string) *Bloomberg {
|
|
if scraperURL == "" {
|
|
scraperURL = "http://scraper:3001"
|
|
}
|
|
return &Bloomberg{
|
|
scraperURL: scraperURL,
|
|
client: &http.Client{Timeout: 10 * time.Minute},
|
|
}
|
|
}
|
|
|
|
// NewWithClient uses the same production RPC DTOs and validation with injected I/O.
|
|
func NewWithClient(scraperURL string, client *http.Client, diagnostics io.Writer) *Bloomberg {
|
|
b := New(scraperURL)
|
|
if client != nil {
|
|
b.client = client
|
|
}
|
|
b.diagnostics = diagnostics
|
|
return b
|
|
}
|
|
func (b *Bloomberg) Name() string { return "bloomberg" }
|
|
|
|
type scraperRequest struct {
|
|
Username string `json:"username"`
|
|
Password string `json:"password"`
|
|
}
|
|
|
|
type scraperArticle struct {
|
|
Title string `json:"title"`
|
|
URL string `json:"url"`
|
|
PublishedAt json.RawMessage `json:"published_at"`
|
|
}
|
|
|
|
type scraperResponse struct {
|
|
Articles []scraperArticle `json:"articles"`
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
func (b *Bloomberg) ScrapeWithCredentials(ctx context.Context, username, password string, symbols []string) ([]scraper.Article, error) {
|
|
payload, _ := json.Marshal(scraperRequest{Username: username, Password: password})
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, b.scraperURL+"/bloomberg/scrape", bytes.NewReader(payload))
|
|
if err != nil {
|
|
return nil, errors.New("bloomberg: request construction failed")
|
|
}
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
resp, err := b.client.Do(req)
|
|
if err != nil {
|
|
return nil, scraperFailure(ctx, err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("bloomberg: scraper HTTP %d", resp.StatusCode)
|
|
}
|
|
body, err := io.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, scraperFailure(ctx, err)
|
|
}
|
|
|
|
var result scraperResponse
|
|
if err := json.Unmarshal(body, &result); err != nil {
|
|
return nil, errors.New("bloomberg: invalid response")
|
|
}
|
|
if result.Error != "" {
|
|
return nil, errors.New("bloomberg: scraper failure")
|
|
}
|
|
|
|
var articles []scraper.Article
|
|
for _, a := range result.Articles {
|
|
title := strings.TrimSpace(a.Title)
|
|
url := a.URL
|
|
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,
|
|
URL: url,
|
|
PublishedAt: date,
|
|
Symbols: syms,
|
|
})
|
|
}
|
|
writer := b.diagnostics
|
|
if writer == nil {
|
|
writer = os.Stdout
|
|
}
|
|
fmt.Fprintf(writer, "bloomberg: %d articles fetched\n", len(articles))
|
|
return articles, nil
|
|
}
|
|
|
|
// Preserve only typed timeout/cancellation identity, never the raw transport error.
|
|
func scraperFailure(ctx context.Context, err error) error {
|
|
if errors.Is(ctx.Err(), context.DeadlineExceeded) || errors.Is(err, context.DeadlineExceeded) {
|
|
return fmt.Errorf("bloomberg: timeout: %w", context.DeadlineExceeded)
|
|
}
|
|
if errors.Is(ctx.Err(), context.Canceled) || errors.Is(err, context.Canceled) {
|
|
return fmt.Errorf("bloomberg: canceled: %w", context.Canceled)
|
|
}
|
|
var timeout interface{ Timeout() bool }
|
|
if errors.As(err, &timeout) && timeout.Timeout() {
|
|
return fmt.Errorf("bloomberg: timeout: %w", context.DeadlineExceeded)
|
|
}
|
|
return errors.New("bloomberg: transport failure")
|
|
}
|