Skip to content

Commit

Permalink
refactor: clean up client-to-torrent episode matching (#151)
Browse files Browse the repository at this point in the history
* refactor: clean up client-to-torrent eps matching

* chore(tests): properly use assert package

* chore(tests): sort imports

* chore: fix import order and rename CompareInfo fields

* chore(processor): var rename to follow current naming
  • Loading branch information
nuxencs authored Oct 11, 2024
1 parent 3ed6e0e commit 38421c3
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 222 deletions.
13 changes: 10 additions & 3 deletions internal/domain/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

package domain

import (
"fmt"
)
import "fmt"

type StatusCode int

Expand All @@ -21,6 +19,9 @@ const (
StatusStreamingServiceMismatch StatusCode = 208
StatusAlreadyInClient StatusCode = 210
StatusNotASeasonPack StatusCode = 211
StatusSizeMismatch StatusCode = 212
StatusSeasonMismatch StatusCode = 213
StatusEpisodeMismatch StatusCode = 214
StatusBelowThreshold StatusCode = 230
StatusSuccessfulMatch StatusCode = 250
StatusSuccessfulHardlink StatusCode = 250
Expand Down Expand Up @@ -62,6 +63,12 @@ func (s StatusCode) String() string {
return "release already in client"
case StatusNotASeasonPack:
return "release is not a season pack"
case StatusSizeMismatch:
return "size did not match"
case StatusSeasonMismatch:
return "season did not match"
case StatusEpisodeMismatch:
return "episode did not match"
case StatusBelowThreshold:
return "number of matches below threshold"
case StatusSuccessfulMatch:
Expand Down
6 changes: 3 additions & 3 deletions internal/domain/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
package domain

type CompareInfo struct {
StatusCode StatusCode
RequestRejectField any
ClientRejectField any
StatusCode StatusCode
RejectValueA any
RejectValueB any
}
16 changes: 8 additions & 8 deletions internal/http/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,8 @@ func (p *processor) processSeasonPack() (domain.StatusCode, error) {
domain.StatusCutMismatch, domain.StatusEditionMismatch, domain.StatusRepackStatusMismatch,
domain.StatusHdrMismatch, domain.StatusStreamingServiceMismatch:
p.log.Info().Msgf("%s: request(%s => %v), client(%s => %v)",
compareInfo.StatusCode, requestRls.String(), compareInfo.RequestRejectField,
clientEntry.r.String(), compareInfo.ClientRejectField)
compareInfo.StatusCode, requestRls.String(), compareInfo.RejectValueA,
clientEntry.r.String(), compareInfo.RejectValueB)
codeSet[compareInfo.StatusCode] = true
continue

Expand Down Expand Up @@ -453,7 +453,7 @@ func (p *processor) parseTorrent() (domain.StatusCode, error) {
successfulHardlink := false

var matchedEpPath string
var matchErr error
var compareInfo domain.CompareInfo
var targetEpPath string

targetPackDir := filepath.Join(clientCfg.PreImportPath, parsedPackName)
Expand All @@ -463,11 +463,11 @@ func (p *processor) parseTorrent() (domain.StatusCode, error) {
// reset targetEpPath for each checked torrentEp
targetEpPath = ""

matchedEpPath, matchErr = utils.MatchEpToSeasonPackEp(match.clientEpPath, match.clientEpSize,
matchedEpPath, compareInfo = release.MatchEpToSeasonPackEp(match.clientEpPath, match.clientEpSize,
torrentEp.Path, torrentEp.Size)
if matchErr != nil {
p.log.Debug().Err(matchErr).Msgf("episode did not match: client(%s), torrent(%s)",
filepath.Base(match.clientEpPath), torrentEp.Path)
if len(matchedEpPath) == 0 {
p.log.Debug().Msgf("%s: client(%s => %v), torrent(%s => %v)", compareInfo.StatusCode,
filepath.Base(match.clientEpPath), compareInfo.RejectValueA, torrentEp.Path, compareInfo.RejectValueB)
continue
}
targetEpPath = filepath.Join(targetPackDir, matchedEpPath)
Expand All @@ -482,7 +482,7 @@ func (p *processor) parseTorrent() (domain.StatusCode, error) {

break
}
if matchErr != nil {
if len(matchedEpPath) == 0 {
p.log.Error().Msgf("error matching episode to file in pack, skipping hardlink: %s",
filepath.Base(match.clientEpPath))
continue
Expand Down
92 changes: 68 additions & 24 deletions internal/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package release

import (
"path/filepath"

"github.com/nuxencs/seasonpackarr/internal/domain"
"github.com/nuxencs/seasonpackarr/internal/utils"

Expand All @@ -23,51 +25,51 @@ func CheckCandidates(requestRls, clientRls rls.Release, fuzzyMatching domain.Fuz
func compareReleases(requestRls, clientRls rls.Release, fuzzyMatching domain.FuzzyMatching) domain.CompareInfo {
if rls.MustNormalize(requestRls.Resolution) != rls.MustNormalize(clientRls.Resolution) {
return domain.CompareInfo{
StatusCode: domain.StatusResolutionMismatch,
RequestRejectField: requestRls.Resolution,
ClientRejectField: clientRls.Resolution,
StatusCode: domain.StatusResolutionMismatch,
RejectValueA: requestRls.Resolution,
RejectValueB: clientRls.Resolution,
}
}

if rls.MustNormalize(requestRls.Source) != rls.MustNormalize(clientRls.Source) {
return domain.CompareInfo{
StatusCode: domain.StatusSourceMismatch,
RequestRejectField: requestRls.Source,
ClientRejectField: clientRls.Source,
StatusCode: domain.StatusSourceMismatch,
RejectValueA: requestRls.Source,
RejectValueB: clientRls.Source,
}
}

if rls.MustNormalize(requestRls.Group) != rls.MustNormalize(clientRls.Group) {
return domain.CompareInfo{
StatusCode: domain.StatusRlsGrpMismatch,
RequestRejectField: requestRls.Group,
ClientRejectField: clientRls.Group,
StatusCode: domain.StatusRlsGrpMismatch,
RejectValueA: requestRls.Group,
RejectValueB: clientRls.Group,
}
}

if !utils.EqualElements(requestRls.Cut, clientRls.Cut) {
return domain.CompareInfo{
StatusCode: domain.StatusCutMismatch,
RequestRejectField: requestRls.Cut,
ClientRejectField: clientRls.Cut,
StatusCode: domain.StatusCutMismatch,
RejectValueA: requestRls.Cut,
RejectValueB: clientRls.Cut,
}
}

if !utils.EqualElements(requestRls.Edition, clientRls.Edition) {
return domain.CompareInfo{
StatusCode: domain.StatusEditionMismatch,
RequestRejectField: requestRls.Edition,
ClientRejectField: clientRls.Edition,
StatusCode: domain.StatusEditionMismatch,
RejectValueA: requestRls.Edition,
RejectValueB: clientRls.Edition,
}
}

// skip comparing repack status when skipRepackCompare is enabled
if !fuzzyMatching.SkipRepackCompare {
if !utils.EqualElements(requestRls.Other, clientRls.Other) {
return domain.CompareInfo{
StatusCode: domain.StatusRepackStatusMismatch,
RequestRejectField: requestRls.Other,
ClientRejectField: clientRls.Other,
StatusCode: domain.StatusRepackStatusMismatch,
RejectValueA: requestRls.Other,
RejectValueB: clientRls.Other,
}
}
}
Expand All @@ -80,17 +82,17 @@ func compareReleases(requestRls, clientRls rls.Release, fuzzyMatching domain.Fuz

if !utils.EqualElements(requestRls.HDR, clientRls.HDR) {
return domain.CompareInfo{
StatusCode: domain.StatusHdrMismatch,
RequestRejectField: requestRls.HDR,
ClientRejectField: clientRls.HDR,
StatusCode: domain.StatusHdrMismatch,
RejectValueA: requestRls.HDR,
RejectValueB: clientRls.HDR,
}
}

if requestRls.Collection != clientRls.Collection {
return domain.CompareInfo{
StatusCode: domain.StatusStreamingServiceMismatch,
RequestRejectField: requestRls.Collection,
ClientRejectField: clientRls.Collection,
StatusCode: domain.StatusStreamingServiceMismatch,
RejectValueA: requestRls.Collection,
RejectValueB: clientRls.Collection,
}
}

Expand All @@ -101,6 +103,48 @@ func compareReleases(requestRls, clientRls rls.Release, fuzzyMatching domain.Fuz
return domain.CompareInfo{StatusCode: domain.StatusSuccessfulMatch}
}

func MatchEpToSeasonPackEp(clientEpPath string, clientEpSize int64, torrentEpPath string, torrentEpSize int64) (string, domain.CompareInfo) {
if clientEpSize != torrentEpSize {
return "", domain.CompareInfo{
StatusCode: domain.StatusSizeMismatch,
RejectValueA: clientEpSize,
RejectValueB: torrentEpSize,
}
}

clientEpRls := rls.ParseString(filepath.Base(clientEpPath))
torrentEpRls := rls.ParseString(filepath.Base(torrentEpPath))

switch {
case clientEpRls.Series != torrentEpRls.Series:
return "", domain.CompareInfo{
StatusCode: domain.StatusSeasonMismatch,
RejectValueA: clientEpRls.Series,
RejectValueB: torrentEpRls.Series,
}
case clientEpRls.Episode != torrentEpRls.Episode:
return "", domain.CompareInfo{
StatusCode: domain.StatusEpisodeMismatch,
RejectValueA: clientEpRls.Episode,
RejectValueB: torrentEpRls.Episode,
}
case clientEpRls.Resolution != torrentEpRls.Resolution:
return "", domain.CompareInfo{
StatusCode: domain.StatusResolutionMismatch,
RejectValueA: clientEpRls.Resolution,
RejectValueB: torrentEpRls.Resolution,
}
case rls.MustNormalize(clientEpRls.Group) != rls.MustNormalize(torrentEpRls.Group):
return "", domain.CompareInfo{
StatusCode: domain.StatusRlsGrpMismatch,
RejectValueA: clientEpRls.Group,
RejectValueB: torrentEpRls.Group,
}
}

return torrentEpPath, domain.CompareInfo{}
}

func PercentOfTotalEpisodes(totalEps int, foundEps int) float32 {
if totalEps == 0 {
return 0
Expand Down
Loading

0 comments on commit 38421c3

Please sign in to comment.