From 81446c66fc9386b73c81dc8df0d3d0b8d3d9df08 Mon Sep 17 00:00:00 2001 From: soup Date: Sat, 24 Aug 2024 14:11:55 +0200 Subject: [PATCH] fix(arrs): deduplicate titles (#91) --- internal/processor/radarr.go | 25 ++++++++++++++----------- internal/processor/sonarr.go | 23 +++++++++++++++++------ 2 files changed, 31 insertions(+), 17 deletions(-) diff --git a/internal/processor/radarr.go b/internal/processor/radarr.go index d4bc165..6f0e32b 100644 --- a/internal/processor/radarr.go +++ b/internal/processor/radarr.go @@ -2,6 +2,7 @@ package processor import ( "context" + "sort" "strings" "time" @@ -89,7 +90,7 @@ func (s Service) processRadarr(ctx context.Context, cfg *domain.ArrConfig, logge logger.Debug().Msgf("found %d movies to process", len(movies)) - var titles []string + titleSet := make(map[string]struct{}) var monitoredTitles int for _, movie := range movies { @@ -119,20 +120,22 @@ func (s Service) processRadarr(ctx context.Context, cfg *domain.ArrConfig, logge monitoredTitles++ // Taking the international title and the original title and appending them to the titles array. - titlesMap := make(map[string]bool) // Initialize a map to keep track of unique titles. - t := m.Title - ot := m.OriginalTitle - - for _, title := range []string{t, ot} { - if title != "" && !titlesMap[title] { - titlesMap[title] = true - titles = append(titles, processTitle(title, cfg.MatchRelease)...) + for _, title := range []string{m.Title, m.OriginalTitle} { + if title != "" { + for _, t := range processTitle(title, cfg.MatchRelease) { + titleSet[t] = struct{}{} + } } } + } + uniqueTitles := make([]string, 0, len(titleSet)) + for title := range titleSet { + uniqueTitles = append(uniqueTitles, title) } - logger.Debug().Msgf("from a total of %d movies we found %d monitored and created %d release titles", len(movies), monitoredTitles, len(titles)) + sort.Strings(uniqueTitles) + logger.Debug().Msgf("from a total of %d movies we found %d monitored and created %d release titles", len(movies), monitoredTitles, len(uniqueTitles)) - return titles, nil + return uniqueTitles, nil } diff --git a/internal/processor/sonarr.go b/internal/processor/sonarr.go index 0f1c20d..f3b6f2f 100644 --- a/internal/processor/sonarr.go +++ b/internal/processor/sonarr.go @@ -97,7 +97,7 @@ func (s Service) processSonarr(ctx context.Context, cfg *domain.ArrConfig, logge logger.Debug().Msgf("found %d shows to process", len(shows)) - var titles []string + titleSet := make(map[string]struct{}) var monitoredTitles int for _, show := range shows { @@ -129,17 +129,28 @@ func (s Service) processSonarr(ctx context.Context, cfg *domain.ArrConfig, logge //titles = append(titles, rls.MustNormalize(s.Title)) //titles = append(titles, rls.MustClean(s.Title)) - titles = append(titles, processTitle(s.Title, cfg.MatchRelease)...) + titles := processTitle(s.Title, cfg.MatchRelease) + for _, title := range titles { + titleSet[title] = struct{}{} + } if !cfg.ExcludeAlternateTitles { for _, title := range s.AlternateTitles { - titles = append(titles, processTitle(title.Title, cfg.MatchRelease)...) + altTitles := processTitle(title.Title, cfg.MatchRelease) + for _, altTitle := range altTitles { + titleSet[altTitle] = struct{}{} + } } } } - sort.Strings(titles) - logger.Debug().Msgf("from a total of %d shows we found %d monitored and created %d release titles", len(shows), monitoredTitles, len(titles)) + uniqueTitles := make([]string, 0, len(titleSet)) + for title := range titleSet { + uniqueTitles = append(uniqueTitles, title) + } + + sort.Strings(uniqueTitles) + logger.Debug().Msgf("from a total of %d shows we found %d monitored and created %d release titles", len(shows), monitoredTitles, len(uniqueTitles)) - return titles, nil + return uniqueTitles, nil }