Skip to content

Commit

Permalink
fix batch of part of a season getting downloaded even with later epis…
Browse files Browse the repository at this point in the history
…odes in the qBittorrent
  • Loading branch information
sonalys committed Feb 8, 2024
1 parent bebaf69 commit c560bfb
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.21.6
require (
github.com/rs/zerolog v1.32.0
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3
golang.org/x/time v0.5.0
gopkg.in/yaml.v3 v3.0.1
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0=
github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3 h1:/RIbNt/Zr7rVhIkQhooTxCxFcdWLGIKnZA4IXNFSrvo=
golang.org/x/exp v0.0.0-20240205201215-2c58cdc269a3/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
Expand Down
4 changes: 2 additions & 2 deletions internal/discovery/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ func (c *Controller) DigestMALEntry(ctx context.Context, entry myanimelist.Anime
nyaaEntries, err := c.dep.NYAA.List(ctx, titleQuery, sourceQuery, qualityQuery)
log.Debug().Str("entry", entry.GetTitle()).Msgf("found %d torrents", len(nyaaEntries))
if err != nil {
return 0, fmt.Errorf("getting nyaa list: %w", err)
return count, fmt.Errorf("getting nyaa list: %w", err)
}
// There should always be torrents for entries, if there aren't we can just exit the routine.
if len(nyaaEntries) == 0 {
log.Error().Msgf("no torrents found for entry '%s'", entry.GetTitle())
return 0, nil
return count, nil
}
latestTag, err := c.GetLatestTag(ctx, entry)
if err != nil {
Expand Down
43 changes: 39 additions & 4 deletions internal/discovery/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import (
"github.com/sonalys/animeman/integrations/qbittorrent"
"github.com/sonalys/animeman/internal/parser"
"github.com/sonalys/animeman/internal/utils"
"golang.org/x/exp/constraints"
)

var numberExpr = regexp.MustCompile(`\d+`)
var batchReplaceExpr = regexp.MustCompile(`(\d+)~(\d+)`)

func strSliceToInt(from []string) []int64 {
out := make([]int64, 0, len(from))
Expand All @@ -23,11 +25,42 @@ func strSliceToInt(from []string) []int64 {
return out
}

func min(a, b int) int {
if a < b {
return a
func min[T constraints.Ordered](values ...T) (min T) {
if len(values) == 0 {
return
}
return b
min = values[0]
for i := range values {
if values[i] < min {
min = values[i]
}
}
return min
}

func max[T constraints.Ordered](values ...T) (max T) {
if len(values) == 0 {
return
}
max = values[0]
for i := range values {
if values[i] > max {
max = values[i]
}
}
return max
}

func mergeBatchEpisodes(tag string) string {
matches := batchReplaceExpr.FindAllStringSubmatch(tag, -1)
if len(matches) == 0 {
return tag
}
values := matches[0][1:]
return batchReplaceExpr.ReplaceAllString(
tag,
fmt.Sprint(max(strSliceToInt(values)...)),
)
}

func compareTags(a, b string) int {
Expand All @@ -37,6 +70,8 @@ func compareTags(a, b string) int {
if a != "" && b == "" {
return 1
}
a = mergeBatchEpisodes(a)
b = mergeBatchEpisodes(b)
aNums := strSliceToInt(numberExpr.FindAllString(a, -1))
bNums := strSliceToInt(numberExpr.FindAllString(b, -1))
lenA, lenB := len(aNums), len(bNums)
Expand Down
49 changes: 49 additions & 0 deletions internal/discovery/torrent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,17 @@ func Test_getLatestTag(t *testing.T) {
},
want: "S3",
},
{
name: "batch and season",
args: args{
torrents: []qbittorrent.Torrent{
{Tags: "S1E1~13"},
{Tags: "S1E2"},
{Tags: "S1E3"},
},
},
want: "S1E1~13",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -80,3 +91,41 @@ func Test_getLatestTag(t *testing.T) {
})
}
}

func Test_mergeBatchEpisodes(t *testing.T) {
type args struct {
tag string
}
tests := []struct {
name string
args args
want string
}{
{
name: "empty",
args: args{},
want: "",
},
{
name: "ok",
args: args{
tag: "S03E1~12",
},
want: "S03E12",
},
{
name: "no batch",
args: args{
tag: "S03E1",
},
want: "S03E1",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := mergeBatchEpisodes(tt.args.tag); got != tt.want {
t.Errorf("mergeBatchEpisodes() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 1 addition & 1 deletion internal/parser/title.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func ParseTitle(title string) ParsedTitle {

func (t ParsedTitle) BuildSeasonEpisodeTag() string {
resp := fmt.Sprintf("%s S%s", t.Title, t.Season)
if !t.IsMultiEpisode {
if t.Episode != "" {
resp = resp + fmt.Sprintf("E%s", t.Episode)
}
return resp
Expand Down

0 comments on commit c560bfb

Please sign in to comment.