From 0399fc93167cde3ad36a5236050783340006c2c3 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 4 May 2025 10:34:03 -0600 Subject: [PATCH] map nyaa://q=X to https://nyaa.si/.../ --- src/feeds/http.go | 36 +++++++++++++++++++++++++++++++++++- src/feeds/http_test.go | 21 +++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/src/feeds/http.go b/src/feeds/http.go index 38b6dbd..cd2d7e3 100644 --- a/src/feeds/http.go +++ b/src/feeds/http.go @@ -49,7 +49,12 @@ func (feed Feed) ShouldExecute() (bool, error) { } func (feed Feed) Fetch(ctx context.Context) (Items, error) { - resp, err := proxyFetch(ctx, feed.Version.URL) + u, err := feed.FetchURL() + if err != nil { + return nil, err + } + + resp, err := proxyFetch(ctx, u.String()) if err != nil { return nil, err } @@ -132,3 +137,32 @@ func proxyFetch(ctx context.Context, u string) (string, error) { return string(b), nil } + +func (feed Feed) FetchURL() (*url.URL, error) { + u, err := url.Parse(feed.Version.URL) + if err != nil { + return nil, err + } + + switch u.Scheme { + case "nyaa": // `nyaa://?q=A B` to `https://nyaa.si/?page=rss&q=A%20B&c=0_0&f=0` + q := u.Query() + if q.Get("q") == "" { + return nil, fmt.Errorf("invalid nyaa:// (%s): no ?q", feed.Version.URL) + } + + q.Set("page", "rss") + q.Set("c", "0_0") + q.Set("f", "0") + + u.RawQuery = q.Encode() + u.Scheme = "https" + u.Host = "nyaa.si" + u.Path = "/" + case "http", "https": + default: + return nil, fmt.Errorf("not impl mapping %s:// to url", u.Scheme) + } + + return u, nil +} diff --git a/src/feeds/http_test.go b/src/feeds/http_test.go index 47e3288..6648c23 100644 --- a/src/feeds/http_test.go +++ b/src/feeds/http_test.go @@ -74,3 +74,24 @@ func TestFeedFetch(t *testing.T) { t.Errorf("expected\n\t%+v but got \n\t%+v", expect, items[0]) } } + +func TestFeedFetchURL(t *testing.T) { + cases := map[string]string{ + "http://host/path?k=v": "http://host/path?k=v", + "https://host/path?k=v": "https://host/path?k=v", + "nyaa://?q=a b&u=c d": "https://nyaa.si/?c=0_0&f=0&page=rss&q=a+b&u=c+d", + } + + for given, want := range cases { + given := given + want := want + t.Run(given, func(t *testing.T) { + f := feeds.Feed{} + f.Version.URL = given + got, _ := f.FetchURL() + if got := got.String(); got != want { + t.Errorf("expected %q but got %q", want, got) + } + }) + } +}