diff --git a/integrations/nyaa/types.go b/integrations/nyaa/types.go index f3c213b..d243d05 100644 --- a/integrations/nyaa/types.go +++ b/integrations/nyaa/types.go @@ -5,6 +5,8 @@ import ( "net/http" "net/url" "strings" + + "github.com/sonalys/animeman/internal/utils" ) type ( @@ -43,34 +45,19 @@ func (q Query) Apply(req *http.Request) { req.URL.RawQuery = query.Encode() } -func filterNotEmpty(entries []string) []string { - notEmpty := make([]string, 0, len(entries)) - for i := range entries { - if entries[i] != "" { - notEmpty = append(notEmpty, entries[i]) - } - } - return notEmpty -} - -func quoteEntriesWithSpace(entries []string) []string { - out := make([]string, 0, len(entries)) - for i := range entries { - if strings.Contains(entries[i], " ") { - out = append(out, fmt.Sprintf("\"%s\"", entries[i])) - continue - } - out = append(out, entries[i]) +func quoteSpace(entries string) string { + if strings.Contains(entries, " ") { + return fmt.Sprintf("\"%s\"", entries) } - return out + return entries } func (entries OrQuery) Apply(req *http.Request) { query := req.URL.Query() prevQuery := query.Get("q") - entries = filterNotEmpty(entries) - entries = quoteEntriesWithSpace(entries) + entries = utils.Filter(entries, func(s string) bool { return s != "" }) + entries = utils.ForEach(entries, quoteSpace) curQuery := fmt.Sprintf("(%s)", strings.Join(entries, "|")) if prevQuery == "" { diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index 2cc0278..8f7e597 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -10,6 +10,7 @@ import ( "github.com/rs/zerolog/log" "github.com/sonalys/animeman/integrations/nyaa" "github.com/sonalys/animeman/internal/parser" + "github.com/sonalys/animeman/internal/utils" "github.com/sonalys/animeman/pkg/v1/animelist" "github.com/sonalys/animeman/pkg/v1/torrentclient" ) @@ -112,19 +113,11 @@ func filterNyaaFeed(entries []nyaa.Entry, latestTag string, animeStatus animelis return episodeFilter(parseNyaaEntries(entries), latestTag, !useBatch) } -func ForEach[T, T1 any](in []T, f func(T) T1) []T1 { - out := make([]T1, 0, len(in)) - for i := range in { - out = append(out, f(in[i])) - } - return out -} - // DigestAnimeListEntry receives an anime list entry and fetches the anime feed, looking for new content. func (c *Controller) DigestAnimeListEntry(ctx context.Context, entry animelist.Entry) (count int, err error) { // Build search query for Nyaa. // For title we filter for english and original titles. - titleQuery := nyaa.OrQuery(ForEach(entry.Titles, parser.TitleStrip)) + titleQuery := nyaa.OrQuery(utils.ForEach(entry.Titles, parser.TitleStrip)) sourceQuery := nyaa.OrQuery(c.dep.Config.Sources) qualityQuery := nyaa.OrQuery(c.dep.Config.Qualitites) diff --git a/internal/utils/slices.go b/internal/utils/slices.go new file mode 100644 index 0000000..3f8f8f0 --- /dev/null +++ b/internal/utils/slices.go @@ -0,0 +1,19 @@ +package utils + +func ForEach[T, T1 any](in []T, f func(T) T1) []T1 { + out := make([]T1, 0, len(in)) + for i := range in { + out = append(out, f(in[i])) + } + return out +} + +func Filter[T any](in []T, f func(T) bool) []T { + out := make([]T, 0, len(in)) + for i := range in { + if f(in[i]) { + out = append(out, in[i]) + } + } + return out +}