diff --git a/media/show.go b/media/show.go index b6626ae..41f036c 100644 --- a/media/show.go +++ b/media/show.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/l3uddz/nabarr/media/trakt" + "github.com/l3uddz/nabarr/util" "strconv" ) @@ -63,6 +64,7 @@ func (c *Client) GetShowInfo(item *FeedItem) (*Item, error) { Msg("Item was not found on tvdb") } else if ti != nil { mi.Tvdb = *ti + mi.Languages = util.StringSliceMergeUnique(mi.Languages, []string{ti.Language}) } return mi, nil diff --git a/util/slice.go b/util/slice.go index 3272f07..66ca311 100644 --- a/util/slice.go +++ b/util/slice.go @@ -10,3 +10,34 @@ func StringSliceContains(slice []string, val string) bool { } return false } + +func StringSliceMergeUnique(existingSlice []string, mergeSlice []string) []string { + // add existing + data := make([]string, 0) + for _, es := range existingSlice { + if es == "" { + continue + } + data = append(data, es) + } + + // add merge items (unique) + for _, ms := range mergeSlice { + if ms == "" { + continue + } + + merge := true + for _, es := range data { + if strings.EqualFold(es, ms) { + merge = false + break + } + } + + if merge { + data = append(data, ms) + } + } + return data +} diff --git a/util/slice_test.go b/util/slice_test.go index a56d81e..7a1d87f 100644 --- a/util/slice_test.go +++ b/util/slice_test.go @@ -1,6 +1,9 @@ package util -import "testing" +import ( + "reflect" + "testing" +) func TestStringSliceContains(t *testing.T) { type args struct { @@ -37,3 +40,55 @@ func TestStringSliceContains(t *testing.T) { }) } } + +func TestStringSliceMergeUnique(t *testing.T) { + type args struct { + existingSlice []string + mergeSlice []string + } + tests := []struct { + name string + args args + want []string + }{ + { + name: "no change", + args: args{ + existingSlice: []string{"test", "test2"}, + mergeSlice: []string{"test", "Test2"}, + }, + want: []string{"test", "test2"}, + }, + { + name: "no change empty", + args: args{ + existingSlice: []string{}, + mergeSlice: []string{}, + }, + want: []string{}, + }, + { + name: "with change", + args: args{ + existingSlice: []string{"test", "test2"}, + mergeSlice: []string{"test", "Test2", "test3"}, + }, + want: []string{"test", "test2", "test3"}, + }, + { + name: "with change no empty", + args: args{ + existingSlice: []string{"", "en"}, + mergeSlice: []string{"fr", "", "en"}, + }, + want: []string{"en", "fr"}, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := StringSliceMergeUnique(tt.args.existingSlice, tt.args.mergeSlice); !reflect.DeepEqual(got, tt.want) { + t.Errorf("StringSliceMergeUnique() = %v, want %v", got, tt.want) + } + }) + } +}