173 lines
4.2 KiB
Go
173 lines
4.2 KiB
Go
package yahoofinance
|
|
|
|
import (
|
|
"context"
|
|
"encoding/xml"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/tradarr/backend/internal/scraper"
|
|
"github.com/tradarr/backend/internal/scraper/sourcehttp"
|
|
)
|
|
|
|
type YahooFinance struct {
|
|
client *http.Client
|
|
diagnostics io.Writer
|
|
}
|
|
|
|
func New() *YahooFinance {
|
|
return &YahooFinance{
|
|
client: &http.Client{Transport: sourcehttp.NewTransport(nil, nil), Timeout: 15 * time.Second,
|
|
CheckRedirect: func(r *http.Request, via []*http.Request) error {
|
|
if len(via) == 0 || len(via) >= 3 || r.URL.Scheme != "https" || r.URL.User != nil || r.URL.Host != via[0].URL.Host {
|
|
return http.ErrUseLastResponse
|
|
}
|
|
return nil
|
|
}},
|
|
}
|
|
}
|
|
|
|
// NewWithClient injects transport/diagnostic sinks without replacing ingestion.
|
|
func NewWithClient(client *http.Client, diagnostics io.Writer) *YahooFinance {
|
|
y := New()
|
|
if client != nil {
|
|
y.client = client
|
|
}
|
|
y.diagnostics = diagnostics
|
|
return y
|
|
}
|
|
func (y *YahooFinance) Name() string { return "stocktwits" } // garde le même type en DB
|
|
|
|
type rssFeed struct {
|
|
Channel struct {
|
|
Items []struct {
|
|
Title string `xml:"title"`
|
|
Link string `xml:"link"`
|
|
Description string `xml:"description"`
|
|
PubDate string `xml:"pubDate"`
|
|
GUID string `xml:"guid"`
|
|
} `xml:"item"`
|
|
} `xml:"channel"`
|
|
}
|
|
|
|
func (y *YahooFinance) Scrape(ctx context.Context, symbols []string) ([]scraper.Article, error) {
|
|
var articles []scraper.Article
|
|
reachable := false
|
|
|
|
for i, symbol := range symbols {
|
|
if i > 0 {
|
|
select {
|
|
case <-ctx.Done():
|
|
return articles, ctx.Err()
|
|
case <-time.After(300 * time.Millisecond):
|
|
}
|
|
}
|
|
items, err := y.fetchSymbol(ctx, symbol)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
reachable = true
|
|
articles = append(articles, items...)
|
|
}
|
|
if len(symbols) > 0 && !reachable {
|
|
return nil, errors.New("yahoofinance: source unavailable")
|
|
}
|
|
return articles, nil
|
|
}
|
|
|
|
func (y *YahooFinance) fetchSymbol(ctx context.Context, symbol string) (articles []scraper.Article, resultErr error) {
|
|
status := 0
|
|
defer func() {
|
|
outcome := "ok"
|
|
if resultErr != nil {
|
|
outcome = "failed"
|
|
}
|
|
safeSymbol := strings.Map(func(r rune) rune {
|
|
if r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || strings.ContainsRune(" .-^=", r) {
|
|
return r
|
|
}
|
|
return '_'
|
|
}, symbol)
|
|
if len(safeSymbol) > 64 {
|
|
safeSymbol = safeSymbol[:64]
|
|
}
|
|
writer := y.diagnostics
|
|
if writer == nil {
|
|
writer = os.Stdout
|
|
}
|
|
fmt.Fprintf(writer, "yahoofinance symbol=%s yahoo_symbol=%s status=%d articles=%d outcome=%s\n", safeSymbol, safeSymbol, status, len(articles), outcome)
|
|
}()
|
|
url := fmt.Sprintf(
|
|
"https://feeds.finance.yahoo.com/rss/2.0/headline?s=%s®ion=US&lang=en-US",
|
|
symbol,
|
|
)
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
req.Header.Set("User-Agent", "Mozilla/5.0 (compatible; Tradarr/1.0)")
|
|
req.Header.Set("Accept", "application/rss+xml, application/xml, text/xml")
|
|
|
|
resp, err := y.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
status = resp.StatusCode
|
|
if resp.StatusCode != http.StatusOK {
|
|
return nil, fmt.Errorf("HTTP %d", resp.StatusCode)
|
|
}
|
|
|
|
var feed rssFeed
|
|
if err := xml.NewDecoder(resp.Body).Decode(&feed); err != nil {
|
|
return nil, fmt.Errorf("parse RSS: %w", err)
|
|
}
|
|
|
|
const maxPerSymbol = 5
|
|
for _, item := range feed.Channel.Items {
|
|
if len(articles) >= maxPerSymbol {
|
|
break
|
|
}
|
|
title := strings.TrimSpace(item.Title)
|
|
link := strings.TrimSpace(item.Link)
|
|
if title == "" || link == "" {
|
|
continue
|
|
}
|
|
|
|
var publishedAt *time.Time
|
|
if item.PubDate != "" {
|
|
formats := []string{
|
|
time.RFC1123Z,
|
|
time.RFC1123,
|
|
"Mon, 02 Jan 2006 15:04:05 -0700",
|
|
}
|
|
for _, f := range formats {
|
|
if t, err := time.Parse(f, item.PubDate); err == nil {
|
|
publishedAt = &t
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
content := strings.TrimSpace(item.Description)
|
|
if content == "" {
|
|
content = title
|
|
}
|
|
|
|
articles = append(articles, scraper.Article{ContentState: scraper.ContentIncomplete,
|
|
Title: title,
|
|
Content: content,
|
|
URL: link,
|
|
PublishedAt: publishedAt,
|
|
Symbols: []string{symbol},
|
|
})
|
|
}
|
|
return articles, nil
|
|
}
|