Files
TradarrV2/backend/internal/scraper/scraper.go

103 lines
2.3 KiB
Go

package scraper
import (
"context"
"strings"
"time"
"github.com/tradarr/backend/internal/models"
)
const (
ContentComplete = "complete"
ContentIncomplete = "incomplete"
ContentUnavailable = "unavailable"
)
// ContentStatus never upgrades a teaser, truncated body or unknown extraction.
func ContentStatus(a Article) string {
if strings.TrimSpace(a.Content) == "" || strings.TrimSpace(a.Content) == strings.TrimSpace(a.Title) {
return ContentUnavailable
}
if a.ContentState == ContentComplete && !a.ContentTruncated {
return ContentComplete
}
return ContentIncomplete
}
type Article struct {
ContentState string
Source string
Title string
Content string
ContentTruncated bool
URL string
PublishedAt *time.Time
Symbols []string
}
type Scraper interface {
Name() string
Scrape(ctx context.Context, symbols []string) ([]Article, error)
}
// ArticleEnricher is an optional, transient enrichment port. Generation calls
// it only after source limits, never for the full collected list.
type ArticleEnricher interface {
Enrich(context.Context, []Article) ([]Article, error)
}
// detectSymbols extrait les symboles mentionnés dans un texte
func DetectSymbols(text string, watchlist []string) []string {
found := map[string]bool{}
for _, s := range watchlist {
// Recherche du symbole en majuscules dans le texte
if containsWord(text, s) {
found[s] = true
}
}
result := make([]string, 0, len(found))
for s := range found {
result = append(result, s)
}
return result
}
func containsWord(text, word string) bool {
upper := []byte(text)
w := []byte(word)
for i := 0; i <= len(upper)-len(w); i++ {
match := true
for j := range w {
c := upper[i+j]
if c >= 'a' && c <= 'z' {
c -= 32
}
if c != w[j] {
match = false
break
}
}
if match {
// Vérifier que c'est un mot entier
before := i == 0 || !isAlphaNum(upper[i-1])
after := i+len(w) >= len(upper) || !isAlphaNum(upper[i+len(w)])
if before && after {
return true
}
}
}
return false
}
func isAlphaNum(b byte) bool {
return (b >= 'a' && b <= 'z') || (b >= 'A' && b <= 'Z') || (b >= '0' && b <= '9')
}
// ScraperResult est le résultat d'un job de scraping
type ScraperResult struct {
Source *models.Source
Articles []Article
Err error
}