main
bel 2025-05-04 10:34:03 -06:00
parent 32a010e697
commit 0399fc9316
2 changed files with 56 additions and 1 deletions

View File

@ -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
}

View File

@ -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)
}
})
}
}