feat(filters): add external script and webhook checks

This commit is contained in:
ze0s 2022-07-23 15:19:28 +02:00
parent 16dd8c5419
commit d56693cd33
17 changed files with 635 additions and 200 deletions

View file

@ -135,7 +135,7 @@ func (s *service) delugeV1(client *domain.DownloadClient, action domain.Action,
}
// macros handle args and replace vars
m := NewMacro(release)
m := domain.NewMacro(release)
options, err := s.prepareDelugeOptions(action, m)
if err != nil {
@ -224,7 +224,7 @@ func (s *service) delugeV2(client *domain.DownloadClient, action domain.Action,
}
// macros handle args and replace vars
m := NewMacro(release)
m := domain.NewMacro(release)
// set options
options, err := s.prepareDelugeOptions(action, m)
@ -265,7 +265,7 @@ func (s *service) delugeV2(client *domain.DownloadClient, action domain.Action,
return nil, nil
}
func (s *service) prepareDelugeOptions(action domain.Action, m Macro) (delugeClient.Options, error) {
func (s *service) prepareDelugeOptions(action domain.Action, m domain.Macro) (delugeClient.Options, error) {
// set options
options := delugeClient.Options{}

View file

@ -56,7 +56,7 @@ func (s *service) execCmd(action domain.Action, release domain.Release) error {
func (s *service) parseExecArgs(release domain.Release, execArgs string) ([]string, error) {
// handle args and replace vars
m := NewMacro(release)
m := domain.NewMacro(release)
// parse and replace values in argument string before continuing
parsedArgs, err := m.Parse(execArgs)

View file

@ -1,79 +0,0 @@
package action
import (
"bytes"
"strings"
"text/template"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
)
type Macro struct {
TorrentName string
TorrentPathName string
TorrentHash string
TorrentUrl string
Indexer string
Title string
Resolution string
Source string
HDR string
FilterName string
Season int
Episode int
Year int
CurrentYear int
CurrentMonth int
CurrentDay int
CurrentHour int
CurrentMinute int
CurrentSecond int
}
func NewMacro(release domain.Release) Macro {
currentTime := time.Now()
ma := Macro{
TorrentName: release.TorrentName,
TorrentUrl: release.TorrentURL,
TorrentPathName: release.TorrentTmpFile,
TorrentHash: release.TorrentHash,
Indexer: release.Indexer,
Title: release.Title,
Resolution: release.Resolution,
Source: release.Source,
HDR: strings.Join(release.HDR, ", "),
FilterName: release.FilterName,
Season: release.Season,
Episode: release.Episode,
Year: release.Year,
CurrentYear: currentTime.Year(),
CurrentMonth: int(currentTime.Month()),
CurrentDay: currentTime.Day(),
CurrentHour: currentTime.Hour(),
CurrentMinute: currentTime.Minute(),
CurrentSecond: currentTime.Second(),
}
return ma
}
// Parse takes a string and replaces valid vars
func (m Macro) Parse(text string) (string, error) {
// setup template
tmpl, err := template.New("macro").Parse(text)
if err != nil {
return "", errors.Wrap(err, "could parse macro template")
}
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, m)
if err != nil {
return "", errors.Wrap(err, "could not parse macro")
}
return tpl.String(), nil
}

View file

@ -1,186 +0,0 @@
package action
import (
"fmt"
"testing"
"time"
"github.com/autobrr/autobrr/internal/domain"
"github.com/stretchr/testify/assert"
)
func TestMacros_Parse(t *testing.T) {
currentTime := time.Now()
type fields struct {
TorrentName string
TorrentPathName string
TorrentUrl string
Indexer string
}
type args struct {
text string
}
tests := []struct {
name string
fields fields
release domain.Release
args args
want string
wantErr bool
}{
{
name: "test_ok",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "Print mee {{.TorrentPathName}}"},
want: "Print mee /tmp/a-temporary-file.torrent",
wantErr: false,
},
{
name: "test_bad",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "Print mee {{TorrentPathName}}"},
want: "",
wantErr: true,
},
{
name: "test_program_arg",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "add {{.TorrentPathName}} --category test"},
want: "add /tmp/a-temporary-file.torrent --category test",
wantErr: false,
},
{
name: "test_program_arg_bad",
release: domain.Release{
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "add {{.TorrenttPathName}} --category test"},
want: "",
wantErr: true,
},
{
name: "test_program_arg",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "add {{.TorrentPathName}} --category test --other {{.TorrentName}}"},
want: "add /tmp/a-temporary-file.torrent --category test --other This movie 2021",
wantErr: false,
},
{
name: "test_args_long",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.TorrentName}} {{.TorrentUrl}} SOME_LONG_TOKEN"},
want: "This movie 2021 https://some.site/download/fakeid SOME_LONG_TOKEN",
wantErr: false,
},
{
name: "test_args_long_1",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}} {{.TorrentName}} {{.TorrentUrl}} SOME_LONG_TOKEN"},
want: "mock1 This movie 2021 https://some.site/download/fakeid SOME_LONG_TOKEN",
wantErr: false,
},
{
name: "test_args_category",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}}-race"},
want: "mock1-race",
wantErr: false,
},
{
name: "test_args_category_year",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}}-{{.CurrentYear}}-race"},
want: fmt.Sprintf("mock1-%v-race", currentTime.Year()),
wantErr: false,
},
{
name: "test_args_category_year",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: []string{"DV"},
},
args: args{text: "movies-{{.Resolution}}{{ if .HDR }}-{{.HDR}}{{ end }}"},
want: "movies-2160p-DV",
wantErr: false,
},
{
name: "test_args_category_and_if",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: []string{"HDR"},
},
args: args{text: "movies-{{.Resolution}}{{ if .HDR }}-{{.HDR}}{{ end }}"},
want: "movies-2160p-HDR",
wantErr: false,
},
{
name: "test_release_year_1",
release: domain.Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: []string{"HDR"},
Year: 2021,
},
args: args{text: "movies-{{.Year}}"},
want: "movies-2021",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
m := NewMacro(tt.release)
got, err := m.Parse(tt.args.text)
assert.Equal(t, currentTime.Year(), m.CurrentYear)
if (err != nil) != tt.wantErr {
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
return
}
assert.Equal(t, tt.want, got)
})
}
}

View file

@ -76,7 +76,7 @@ func (s *service) qbittorrent(action domain.Action, release domain.Release) ([]s
}
// macros handle args and replace vars
m := NewMacro(release)
m := domain.NewMacro(release)
options, err := s.prepareQbitOptions(action, m)
if err != nil {
@ -100,7 +100,7 @@ func (s *service) qbittorrent(action domain.Action, release domain.Release) ([]s
return nil, nil
}
func (s *service) prepareQbitOptions(action domain.Action, m Macro) (map[string]string, error) {
func (s *service) prepareQbitOptions(action domain.Action, m domain.Macro) (map[string]string, error) {
options := map[string]string{}

View file

@ -136,7 +136,7 @@ func (s *service) watchFolder(action domain.Action, release domain.Release) erro
}
}
m := NewMacro(release)
m := domain.NewMacro(release)
// parse and replace values in argument string before continuing
watchFolderArgs, err := m.Parse(action.WatchFolder)
@ -187,7 +187,7 @@ func (s *service) webhook(action domain.Action, release domain.Release) error {
}
}
m := NewMacro(release)
m := domain.NewMacro(release)
// parse and replace values in argument string before continuing
dataArgs, err := m.Parse(action.WebhookData)