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

@ -49,61 +49,69 @@ const (
)
type Filter struct {
ID int `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
MinSize string `json:"min_size"`
MaxSize string `json:"max_size"`
Delay int `json:"delay"`
Priority int32 `json:"priority"`
MaxDownloads int `json:"max_downloads"`
MaxDownloadsUnit FilterMaxDownloadsUnit `json:"max_downloads_unit"`
MatchReleases string `json:"match_releases"`
ExceptReleases string `json:"except_releases"`
UseRegex bool `json:"use_regex"`
MatchReleaseGroups string `json:"match_release_groups"`
ExceptReleaseGroups string `json:"except_release_groups"`
Scene bool `json:"scene"`
Origins []string `json:"origins"`
Bonus []string `json:"bonus"`
Freeleech bool `json:"freeleech"`
FreeleechPercent string `json:"freeleech_percent"`
Shows string `json:"shows"`
Seasons string `json:"seasons"`
Episodes string `json:"episodes"`
Resolutions []string `json:"resolutions"` // SD, 480i, 480p, 576p, 720p, 810p, 1080i, 1080p.
Codecs []string `json:"codecs"` // XviD, DivX, x264, h.264 (or h264), mpeg2 (or mpeg-2), VC-1 (or VC1), WMV, Remux, h.264 Remux (or h264 Remux), VC-1 Remux (or VC1 Remux).
Sources []string `json:"sources"` // DSR, PDTV, HDTV, HR.PDTV, HR.HDTV, DVDRip, DVDScr, BDr, BD5, BD9, BDRip, BRRip, DVDR, MDVDR, HDDVD, HDDVDRip, BluRay, WEB-DL, TVRip, CAM, R5, TELESYNC, TS, TELECINE, TC. TELESYNC and TS are synonyms (you don't need both). Same for TELECINE and TC
Containers []string `json:"containers"`
MatchHDR []string `json:"match_hdr"`
ExceptHDR []string `json:"except_hdr"`
MatchOther []string `json:"match_other"`
ExceptOther []string `json:"except_other"`
Years string `json:"years"`
Artists string `json:"artists"`
Albums string `json:"albums"`
MatchReleaseTypes []string `json:"match_release_types"` // Album,Single,EP
ExceptReleaseTypes string `json:"except_release_types"`
Formats []string `json:"formats"` // MP3, FLAC, Ogg, AAC, AC3, DTS
Quality []string `json:"quality"` // 192, 320, APS (VBR), V2 (VBR), V1 (VBR), APX (VBR), V0 (VBR), q8.x (VBR), Lossless, 24bit Lossless, Other
Media []string `json:"media"` // CD, DVD, Vinyl, Soundboard, SACD, DAT, Cassette, WEB, Other
PerfectFlac bool `json:"perfect_flac"`
Cue bool `json:"cue"`
Log bool `json:"log"`
LogScore int `json:"log_score"`
MatchCategories string `json:"match_categories"`
ExceptCategories string `json:"except_categories"`
MatchUploaders string `json:"match_uploaders"`
ExceptUploaders string `json:"except_uploaders"`
Tags string `json:"tags"`
ExceptTags string `json:"except_tags"`
TagsAny string `json:"tags_any"`
ExceptTagsAny string `json:"except_tags_any"`
Actions []*Action `json:"actions"`
Indexers []Indexer `json:"indexers"`
Downloads *FilterDownloads `json:"-"`
ID int `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
MinSize string `json:"min_size"`
MaxSize string `json:"max_size"`
Delay int `json:"delay"`
Priority int32 `json:"priority"`
MaxDownloads int `json:"max_downloads"`
MaxDownloadsUnit FilterMaxDownloadsUnit `json:"max_downloads_unit"`
MatchReleases string `json:"match_releases"`
ExceptReleases string `json:"except_releases"`
UseRegex bool `json:"use_regex"`
MatchReleaseGroups string `json:"match_release_groups"`
ExceptReleaseGroups string `json:"except_release_groups"`
Scene bool `json:"scene"`
Origins []string `json:"origins"`
Bonus []string `json:"bonus"`
Freeleech bool `json:"freeleech"`
FreeleechPercent string `json:"freeleech_percent"`
Shows string `json:"shows"`
Seasons string `json:"seasons"`
Episodes string `json:"episodes"`
Resolutions []string `json:"resolutions"` // SD, 480i, 480p, 576p, 720p, 810p, 1080i, 1080p.
Codecs []string `json:"codecs"` // XviD, DivX, x264, h.264 (or h264), mpeg2 (or mpeg-2), VC-1 (or VC1), WMV, Remux, h.264 Remux (or h264 Remux), VC-1 Remux (or VC1 Remux).
Sources []string `json:"sources"` // DSR, PDTV, HDTV, HR.PDTV, HR.HDTV, DVDRip, DVDScr, BDr, BD5, BD9, BDRip, BRRip, DVDR, MDVDR, HDDVD, HDDVDRip, BluRay, WEB-DL, TVRip, CAM, R5, TELESYNC, TS, TELECINE, TC. TELESYNC and TS are synonyms (you don't need both). Same for TELECINE and TC
Containers []string `json:"containers"`
MatchHDR []string `json:"match_hdr"`
ExceptHDR []string `json:"except_hdr"`
MatchOther []string `json:"match_other"`
ExceptOther []string `json:"except_other"`
Years string `json:"years"`
Artists string `json:"artists"`
Albums string `json:"albums"`
MatchReleaseTypes []string `json:"match_release_types"` // Album,Single,EP
ExceptReleaseTypes string `json:"except_release_types"`
Formats []string `json:"formats"` // MP3, FLAC, Ogg, AAC, AC3, DTS
Quality []string `json:"quality"` // 192, 320, APS (VBR), V2 (VBR), V1 (VBR), APX (VBR), V0 (VBR), q8.x (VBR), Lossless, 24bit Lossless, Other
Media []string `json:"media"` // CD, DVD, Vinyl, Soundboard, SACD, DAT, Cassette, WEB, Other
PerfectFlac bool `json:"perfect_flac"`
Cue bool `json:"cue"`
Log bool `json:"log"`
LogScore int `json:"log_score"`
MatchCategories string `json:"match_categories"`
ExceptCategories string `json:"except_categories"`
MatchUploaders string `json:"match_uploaders"`
ExceptUploaders string `json:"except_uploaders"`
Tags string `json:"tags"`
ExceptTags string `json:"except_tags"`
TagsAny string `json:"tags_any"`
ExceptTagsAny string `json:"except_tags_any"`
ExternalScriptEnabled bool `json:"external_script_enabled"`
ExternalScriptCmd string `json:"external_script_cmd"`
ExternalScriptArgs string `json:"external_script_args"`
ExternalScriptExpectStatus int `json:"external_script_expect_status"`
ExternalWebhookEnabled bool `json:"external_webhook_enabled"`
ExternalWebhookHost string `json:"external_webhook_host"`
ExternalWebhookData string `json:"external_webhook_data"`
ExternalWebhookExpectStatus int `json:"external_webhook_expect_status"`
Actions []*Action `json:"actions"`
Indexers []Indexer `json:"indexers"`
Downloads *FilterDownloads `json:"-"`
}
func (f Filter) CheckFilter(r *Release) ([]string, bool) {

78
internal/domain/macros.go Normal file
View file

@ -0,0 +1,78 @@
package domain
import (
"bytes"
"strings"
"text/template"
"time"
"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 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

@ -0,0 +1,184 @@
package domain
import (
"fmt"
"testing"
"time"
"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 Release
args args
want string
wantErr bool
}{
{
name: "test_ok",
release: 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: 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: 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: Release{
TorrentTmpFile: "/tmp/a-temporary-file.torrent",
Indexer: "mock1",
},
args: args{text: "add {{.TorrenttPathName}} --category test"},
want: "",
wantErr: true,
},
{
name: "test_program_arg",
release: 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: 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: 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: 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: 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: 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: 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: 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

@ -344,6 +344,10 @@ func (r *Release) addRejection(reason string) {
r.Rejections = append(r.Rejections, reason)
}
func (r *Release) AddRejectionF(format string, v ...interface{}) {
r.addRejectionF(format, v...)
}
func (r *Release) addRejectionF(format string, v ...interface{}) {
r.Rejections = append(r.Rejections, fmt.Sprintf(format, v...))
}