fix(watcherguru): use watcherguru.com host

Update the Watcher.Guru base host and recognize absolute article links on the current domain while preserving RSS and HTML fallback behavior.

Refs #21

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 16:14:39 +01:00
parent 4131cf903e
commit 1ff78a7b31
2 changed files with 88 additions and 2 deletions

View File

@ -0,0 +1,86 @@
package watcherguru
import (
"context"
"io"
"net/http"
"reflect"
"strings"
"testing"
)
func TestTicket21UsesAuthorizedHostAndPreservesFallbackPaths(t *testing.T) {
var paths []string
watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
if r.URL.Scheme != "https" || r.URL.Host != "watcherguru.com" {
t.Errorf("request origin=%s://%s; want https://watcherguru.com", r.URL.Scheme, r.URL.Host)
}
if r.Method != http.MethodGet {
t.Errorf("method=%s; want GET", r.Method)
}
paths = append(paths, r.URL.Path)
body := "<rss><channel></channel></rss>"
if r.URL.Path == "/news/" {
body = `<html><body><a href="/news/synthetic-market">Synthetic market headline for host contract</a></body></html>`
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
})}}
articles, err := watcher.Scrape(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(paths, []string{"/feed/", "/news/feed/", "/news/"}) {
t.Fatalf("paths=%v", paths)
}
if len(articles) != 1 || articles[0].URL != "https://watcherguru.com/news/synthetic-market" {
t.Fatalf("relative link resolution changed: %+v", articles)
}
}
func TestTicket21RSSSuccessStillStopsBeforeFallback(t *testing.T) {
calls := 0
watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
calls++
if r.URL.String() != "https://watcherguru.com/feed/" {
t.Errorf("request=%s", r.URL)
}
return &http.Response{StatusCode: 200, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`<rss><channel><item><title>Synthetic RSS headline</title><link>https://fixture.invalid/article</link><description>Synthetic evidence</description></item></channel></rss>`)), Request: r}, nil
})}}
articles, err := watcher.Scrape(context.Background(), nil)
if err != nil || calls != 1 || len(articles) != 1 {
t.Fatalf("calls=%d articles=%d err=%v", calls, len(articles), err)
}
if articles[0].Title != "Synthetic RSS headline" || articles[0].Content != "Synthetic evidence" || articles[0].URL != "https://fixture.invalid/article" {
t.Fatalf("RSS contract changed: %+v", articles[0])
}
}
func TestTicket21ExtractsAbsoluteNewDomainLinkWithoutNewsPath(t *testing.T) {
const articleURL = "https://watcherguru.com/synthetic-market-update"
const title = "Synthetic absolute headline for new domain"
var paths []string
watcher := &WatcherGuru{client: &http.Client{Transport: ticket20RoundTrip(func(r *http.Request) (*http.Response, error) {
if r.URL.Scheme != "https" || r.URL.Host != "watcherguru.com" {
t.Fatalf("unexpected request origin: %s", r.URL)
}
paths = append(paths, r.URL.Path)
body := "<rss><channel></channel></rss>"
if r.URL.Path == "/news/" {
body = `<html><body><a href="` + articleURL + `">` + title + `</a></body></html>`
}
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(body)), Request: r}, nil
})}}
articles, err := watcher.Scrape(context.Background(), nil)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(paths, []string{"/feed/", "/news/feed/", "/news/"}) {
t.Fatalf("paths=%v", paths)
}
if len(articles) != 1 {
t.Fatalf("articles=%d; want absolute new-domain link without /news/", len(articles))
}
if articles[0].URL != articleURL || articles[0].Title != title || articles[0].Content != title {
t.Fatalf("unexpected article: %+v", articles[0])
}
}

View File

@ -13,7 +13,7 @@ import (
"github.com/tradarr/backend/internal/scraper"
)
const baseURL = "https://watcher.guru"
const baseURL = "https://watcherguru.com"
type WatcherGuru struct {
client *http.Client
@ -165,7 +165,7 @@ func (w *WatcherGuru) scrapeHTML(ctx context.Context) (articles []scraper.Articl
return
}
// Collect links that look like news articles
if strings.Contains(href, "/news/") || strings.Contains(href, "watcher.guru") {
if strings.Contains(href, "/news/") || strings.Contains(href, "watcherguru.com") {
text := strings.TrimSpace(nodeText(n))
if len(text) > 20 {
url := href