feat: add backend

This commit is contained in:
Ludvig Lundgren 2021-08-11 15:26:17 +02:00
parent bc418ff248
commit a838d994a6
68 changed files with 9561 additions and 0 deletions

342
internal/filter/service.go Normal file
View file

@ -0,0 +1,342 @@
package filter
import (
"strings"
"github.com/rs/zerolog/log"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/indexer"
"github.com/autobrr/autobrr/pkg/wildcard"
)
type Service interface {
//FindFilter(announce domain.Announce) (*domain.Filter, error)
FindByID(filterID int) (*domain.Filter, error)
FindByIndexerIdentifier(announce domain.Announce) (*domain.Filter, error)
ListFilters() ([]domain.Filter, error)
Store(filter domain.Filter) (*domain.Filter, error)
Update(filter domain.Filter) (*domain.Filter, error)
Delete(filterID int) error
}
type service struct {
repo domain.FilterRepo
actionRepo domain.ActionRepo
indexerSvc indexer.Service
}
func NewService(repo domain.FilterRepo, actionRepo domain.ActionRepo, indexerSvc indexer.Service) Service {
return &service{
repo: repo,
actionRepo: actionRepo,
indexerSvc: indexerSvc,
}
}
func (s *service) ListFilters() ([]domain.Filter, error) {
// get filters
filters, err := s.repo.ListFilters()
if err != nil {
return nil, err
}
var ret []domain.Filter
for _, filter := range filters {
indexers, err := s.indexerSvc.FindByFilterID(filter.ID)
if err != nil {
return nil, err
}
filter.Indexers = indexers
ret = append(ret, filter)
}
return ret, nil
}
func (s *service) FindByID(filterID int) (*domain.Filter, error) {
// find filter
filter, err := s.repo.FindByID(filterID)
if err != nil {
return nil, err
}
// find actions and attach
//actions, err := s.actionRepo.FindFilterActions(filter.ID)
actions, err := s.actionRepo.FindByFilterID(filter.ID)
if err != nil {
log.Error().Msgf("could not find filter actions: %+v", &filter.ID)
}
filter.Actions = actions
// find indexers and attach
indexers, err := s.indexerSvc.FindByFilterID(filter.ID)
if err != nil {
log.Error().Err(err).Msgf("could not find indexers for filter: %+v", &filter.Name)
return nil, err
}
filter.Indexers = indexers
//log.Debug().Msgf("found filter: %+v", filter)
return filter, nil
}
func (s *service) FindByIndexerIdentifier(announce domain.Announce) (*domain.Filter, error) {
// get filter for tracker
filters, err := s.repo.FindByIndexerIdentifier(announce.Site)
if err != nil {
log.Error().Err(err).Msgf("could not find filters for indexer: %v", announce.Site)
return nil, err
}
// match against announce/releaseInfo
for _, filter := range filters {
// if match, return the filter
matchedFilter := s.checkFilter(filter, announce)
if matchedFilter {
log.Trace().Msgf("found filter: %+v", &filter)
log.Debug().Msgf("found filter: %+v", &filter.Name)
// find actions and attach
actions, err := s.actionRepo.FindByFilterID(filter.ID)
if err != nil {
log.Error().Err(err).Msgf("could not find filter actions: %+v", &filter.ID)
return nil, err
}
// if no actions found, check next filter
if actions == nil {
continue
}
filter.Actions = actions
return &filter, nil
}
}
// if no match, return nil
return nil, nil
}
//func (s *service) FindFilter(announce domain.Announce) (*domain.Filter, error) {
// // get filter for tracker
// filters, err := s.repo.FindFiltersForSite(announce.Site)
// if err != nil {
// return nil, err
// }
//
// // match against announce/releaseInfo
// for _, filter := range filters {
// // if match, return the filter
// matchedFilter := s.checkFilter(filter, announce)
// if matchedFilter {
//
// log.Debug().Msgf("found filter: %+v", &filter)
//
// // find actions and attach
// actions, err := s.actionRepo.FindByFilterID(filter.ID)
// if err != nil {
// log.Error().Msgf("could not find filter actions: %+v", &filter.ID)
// }
// filter.Actions = actions
//
// return &filter, nil
// }
// }
//
// // if no match, return nil
// return nil, nil
//}
func (s *service) Store(filter domain.Filter) (*domain.Filter, error) {
// validate data
// store
f, err := s.repo.Store(filter)
if err != nil {
log.Error().Err(err).Msgf("could not store filter: %v", filter)
return nil, err
}
return f, nil
}
func (s *service) Update(filter domain.Filter) (*domain.Filter, error) {
// validate data
// store
f, err := s.repo.Update(filter)
if err != nil {
log.Error().Err(err).Msgf("could not update filter: %v", filter.Name)
return nil, err
}
// take care of connected indexers
if err = s.repo.DeleteIndexerConnections(f.ID); err != nil {
log.Error().Err(err).Msgf("could not delete filter indexer connections: %v", filter.Name)
return nil, err
}
for _, i := range filter.Indexers {
if err = s.repo.StoreIndexerConnection(f.ID, i.ID); err != nil {
log.Error().Err(err).Msgf("could not store filter indexer connections: %v", filter.Name)
return nil, err
}
}
return f, nil
}
func (s *service) Delete(filterID int) error {
if filterID == 0 {
return nil
}
// delete
if err := s.repo.Delete(filterID); err != nil {
log.Error().Err(err).Msgf("could not delete filter: %v", filterID)
return err
}
return nil
}
// checkFilter tries to match filter against announce
func (s *service) checkFilter(filter domain.Filter, announce domain.Announce) bool {
if !filter.Enabled {
return false
}
if filter.Scene && announce.Scene != filter.Scene {
return false
}
if filter.Freeleech && announce.Freeleech != filter.Freeleech {
return false
}
if filter.Shows != "" && !checkFilterStrings(announce.TorrentName, filter.Shows) {
return false
}
//if filter.Seasons != "" && !checkFilterStrings(announce.TorrentName, filter.Seasons) {
// return false
//}
//
//if filter.Episodes != "" && !checkFilterStrings(announce.TorrentName, filter.Episodes) {
// return false
//}
// matchRelease
if filter.MatchReleases != "" && !checkFilterStrings(announce.TorrentName, filter.MatchReleases) {
return false
}
if filter.MatchReleaseGroups != "" && !checkFilterStrings(announce.TorrentName, filter.MatchReleaseGroups) {
return false
}
if filter.ExceptReleaseGroups != "" && checkFilterStrings(announce.TorrentName, filter.ExceptReleaseGroups) {
return false
}
if filter.MatchUploaders != "" && !checkFilterStrings(announce.Uploader, filter.MatchUploaders) {
return false
}
if filter.ExceptUploaders != "" && checkFilterStrings(announce.Uploader, filter.ExceptUploaders) {
return false
}
if len(filter.Resolutions) > 0 && !checkFilterSlice(announce.TorrentName, filter.Resolutions) {
return false
}
if len(filter.Codecs) > 0 && !checkFilterSlice(announce.TorrentName, filter.Codecs) {
return false
}
if len(filter.Sources) > 0 && !checkFilterSlice(announce.TorrentName, filter.Sources) {
return false
}
if len(filter.Containers) > 0 && !checkFilterSlice(announce.TorrentName, filter.Containers) {
return false
}
if filter.Years != "" && !checkFilterStrings(announce.TorrentName, filter.Years) {
return false
}
if filter.MatchCategories != "" && !checkFilterStrings(announce.Category, filter.MatchCategories) {
return false
}
if filter.ExceptCategories != "" && checkFilterStrings(announce.Category, filter.ExceptCategories) {
return false
}
if filter.Tags != "" && !checkFilterStrings(announce.Tags, filter.Tags) {
return false
}
if filter.ExceptTags != "" && checkFilterStrings(announce.Tags, filter.ExceptTags) {
return false
}
return true
}
func checkFilterSlice(name string, filterList []string) bool {
name = strings.ToLower(name)
for _, filter := range filterList {
filter = strings.ToLower(filter)
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
a := strings.ContainsAny(filter, "?|*")
if a {
match := wildcard.Match(filter, name)
if match {
return true
}
} else {
b := strings.Contains(name, filter)
if b {
return true
}
}
}
return false
}
func checkFilterStrings(name string, filterList string) bool {
filterSplit := strings.Split(filterList, ",")
name = strings.ToLower(name)
for _, s := range filterSplit {
s = strings.ToLower(s)
// check if line contains * or ?, if so try wildcard match, otherwise try substring match
a := strings.ContainsAny(s, "?|*")
if a {
match := wildcard.Match(s, name)
if match {
return true
}
} else {
b := strings.Contains(name, s)
if b {
return true
}
}
}
return false
}

View file

@ -0,0 +1,651 @@
package filter
import (
"testing"
"github.com/autobrr/autobrr/internal/domain"
"github.com/stretchr/testify/assert"
)
func Test_checkFilterStrings(t *testing.T) {
type args struct {
name string
filterList string
}
tests := []struct {
name string
args args
want bool
}{
{
name: "test_01",
args: args{
name: "The End",
filterList: "The End, Other movie",
},
want: true,
},
{
name: "test_02",
args: args{
name: "The Simpsons S12",
filterList: "The End, Other movie",
},
want: false,
},
{
name: "test_03",
args: args{
name: "The.Simpsons.S12",
filterList: "The?Simpsons*, Other movie",
},
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := checkFilterStrings(tt.args.name, tt.args.filterList)
assert.Equal(t, tt.want, got)
})
}
}
func Test_service_checkFilter(t *testing.T) {
type args struct {
filter domain.Filter
announce domain.Announce
}
svcMock := &service{
repo: nil,
actionRepo: nil,
indexerSvc: nil,
}
tests := []struct {
name string
args args
expected bool
}{
{
name: "freeleech",
args: args{
announce: domain.Announce{
Freeleech: true,
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
Freeleech: true,
},
},
},
expected: true,
},
{
name: "scene",
args: args{
announce: domain.Announce{
Scene: true,
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
Scene: true,
},
},
},
expected: true,
},
{
name: "not_scene",
args: args{
announce: domain.Announce{
Scene: false,
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
Scene: true,
},
},
},
expected: false,
},
{
name: "shows_1",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Shows: "That show",
},
},
},
expected: true,
},
{
name: "shows_2",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Shows: "That show, The Other show",
},
},
},
expected: true,
},
{
name: "shows_3",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Shows: "That?show*, The?Other?show",
},
},
},
expected: true,
},
{
name: "shows_4",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Shows: "The Other show",
},
},
},
expected: false,
},
{
name: "shows_5",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Shows: "*show*",
},
},
},
expected: true,
},
{
name: "shows_6",
args: args{
announce: domain.Announce{
TorrentName: "That.Show.S06.1080p.BluRay.DD5.1.x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Shows: "*show*",
},
},
},
expected: true,
},
{
name: "shows_7",
args: args{
announce: domain.Announce{
TorrentName: "That.Show.S06.1080p.BluRay.DD5.1.x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Shows: "That?show*",
},
},
},
expected: true,
},
{
name: "match_releases_single",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
MatchReleases: "That show",
},
},
},
expected: true,
},
{
name: "match_releases_single_wildcard",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
MatchReleases: "That show*",
},
},
},
expected: true,
},
{
name: "match_releases_multiple",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
MatchReleases: "That show*, Other one",
},
},
},
expected: true,
},
{
name: "match_release_groups",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
ReleaseGroup: "GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
MatchReleaseGroups: "GROUP1",
},
},
},
expected: true,
},
{
name: "match_release_groups_multiple",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
ReleaseGroup: "GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
MatchReleaseGroups: "GROUP1,GROUP2",
},
},
},
expected: true,
},
{
name: "match_release_groups_dont_match",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
ReleaseGroup: "GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
MatchReleaseGroups: "GROUP2",
},
},
},
expected: false,
},
{
name: "except_release_groups",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
ReleaseGroup: "GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterP2P: domain.FilterP2P{
ExceptReleaseGroups: "GROUP1",
},
},
},
expected: false,
},
{
name: "match_uploaders",
args: args{
announce: domain.Announce{
Uploader: "Uploader1",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
MatchUploaders: "Uploader1",
},
},
},
expected: true,
},
{
name: "non_match_uploaders",
args: args{
announce: domain.Announce{
Uploader: "Uploader2",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
MatchUploaders: "Uploader1",
},
},
},
expected: false,
},
{
name: "except_uploaders",
args: args{
announce: domain.Announce{
Uploader: "Uploader1",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
ExceptUploaders: "Uploader1",
},
},
},
expected: false,
},
{
name: "resolutions_1080p",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 1080p BluRay DD5.1 x264-GROUP1",
Resolution: "1080p",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Resolutions: []string{"1080p"},
},
},
},
expected: true,
},
{
name: "resolutions_2160p",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 2160p BluRay DD5.1 x264-GROUP1",
Resolution: "2160p",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Resolutions: []string{"2160p"},
},
},
},
expected: true,
},
{
name: "resolutions_no_match",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 2160p BluRay DD5.1 x264-GROUP1",
Resolution: "2160p",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Resolutions: []string{"1080p"},
},
},
},
expected: false,
},
{
name: "codecs_1_match",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Codecs: []string{"x264"},
},
},
},
expected: true,
},
{
name: "codecs_2_no_match",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Codecs: []string{"h264"},
},
},
},
expected: false,
},
{
name: "sources_1_match",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Sources: []string{"BluRay"},
},
},
},
expected: true,
},
{
name: "sources_2_no_match",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Sources: []string{"WEB"},
},
},
},
expected: false,
},
{
name: "years_1",
args: args{
announce: domain.Announce{
TorrentName: "That Movie 2020 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Years: "2020",
},
},
},
expected: true,
},
{
name: "years_2",
args: args{
announce: domain.Announce{
TorrentName: "That Movie 2020 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Years: "2020,1990",
},
},
},
expected: true,
},
{
name: "years_3_no_match",
args: args{
announce: domain.Announce{
TorrentName: "That Movie 2020 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Years: "1990",
},
},
},
expected: false,
},
{
name: "years_4_no_match",
args: args{
announce: domain.Announce{
TorrentName: "That Show S06 2160p BluRay DD5.1 x264-GROUP1",
},
filter: domain.Filter{
Enabled: true,
FilterTVMovies: domain.FilterTVMovies{
Years: "2020",
},
},
},
expected: false,
},
{
name: "match_categories_1",
args: args{
announce: domain.Announce{
Category: "TV",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
MatchCategories: "TV",
},
},
},
expected: true,
},
{
name: "match_categories_2",
args: args{
announce: domain.Announce{
Category: "TV :: HD",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
MatchCategories: "*TV*",
},
},
},
expected: true,
},
{
name: "match_categories_3",
args: args{
announce: domain.Announce{
Category: "TV :: HD",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
MatchCategories: "*TV*, *HD*",
},
},
},
expected: true,
},
{
name: "match_categories_4_no_match",
args: args{
announce: domain.Announce{
Category: "TV :: HD",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
MatchCategories: "Movies",
},
},
},
expected: false,
},
{
name: "except_categories_1",
args: args{
announce: domain.Announce{
Category: "Movies",
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
ExceptCategories: "Movies",
},
},
},
expected: false,
},
{
name: "match_multiple_fields_1",
args: args{
announce: domain.Announce{
TorrentName: "That Movie 2020 2160p BluRay DD5.1 x264-GROUP1",
Category: "Movies",
Freeleech: true,
},
filter: domain.Filter{
Enabled: true,
FilterAdvanced: domain.FilterAdvanced{
MatchCategories: "Movies",
},
FilterTVMovies: domain.FilterTVMovies{
Resolutions: []string{"2160p"},
Sources: []string{"BluRay"},
Years: "2020",
},
FilterP2P: domain.FilterP2P{
MatchReleaseGroups: "GROUP1",
MatchReleases: "That movie",
Freeleech: true,
},
},
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := svcMock.checkFilter(tt.args.filter, tt.args.announce)
assert.Equal(t, tt.expected, got)
})
}
}