Skip to content

Commit

Permalink
add utils.slices with Filter and ForEach
Browse files Browse the repository at this point in the history
  • Loading branch information
sonalys committed Feb 10, 2024
1 parent 8235a5b commit 8262915
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
29 changes: 8 additions & 21 deletions integrations/nyaa/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http"
"net/url"
"strings"

"github.com/sonalys/animeman/internal/utils"
)

type (
Expand Down Expand Up @@ -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 == "" {
Expand Down
11 changes: 2 additions & 9 deletions internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand Down
19 changes: 19 additions & 0 deletions internal/utils/slices.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 8262915

Please sign in to comment.