Skip to content

Commit

Permalink
media: merge tvdb and trakt languages
Browse files Browse the repository at this point in the history
  • Loading branch information
l3uddz committed Feb 24, 2021
1 parent cea77e7 commit bf23d93
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
2 changes: 2 additions & 0 deletions media/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/l3uddz/nabarr/media/trakt"
"github.com/l3uddz/nabarr/util"
"strconv"
)

Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions util/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
57 changes: 56 additions & 1 deletion util/slice_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package util

import "testing"
import (
"reflect"
"testing"
)

func TestStringSliceContains(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit bf23d93

Please sign in to comment.