feat(macros): add SizeString DownloadURL and InfoURL (#1080)

* feat(macro): add new variables

* SizeString
* DownloadURL
* InfoURL

* feat(macro): add new test cases
This commit is contained in:
ze0s 2023-09-03 13:27:47 +02:00 committed by GitHub
parent 63660946eb
commit 1bfbe38335
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 86 additions and 52 deletions

View file

@ -12,6 +12,7 @@ import (
"github.com/autobrr/autobrr/pkg/errors"
"github.com/Masterminds/sprig/v3"
"github.com/dustin/go-humanize"
)
type Macro struct {
@ -23,6 +24,8 @@ type Macro struct {
TorrentDataRawBytes []byte
MagnetURI string
GroupID string
DownloadUrl string
InfoUrl string
Indexer string
Title string
Category string
@ -32,6 +35,7 @@ type Macro struct {
HDR string
FilterName string
Size uint64
SizeString string
Season int
Episode int
Year int
@ -48,13 +52,15 @@ func NewMacro(release Release) Macro {
ma := Macro{
TorrentName: release.TorrentName,
TorrentUrl: release.TorrentURL,
TorrentUrl: release.DownloadURL,
TorrentPathName: release.TorrentTmpFile,
TorrentDataRawBytes: release.TorrentDataRawBytes,
TorrentHash: release.TorrentHash,
TorrentID: release.TorrentID,
MagnetURI: release.MagnetURI,
GroupID: release.GroupID,
InfoUrl: release.InfoURL,
DownloadUrl: release.DownloadURL,
Indexer: release.Indexer,
Title: release.Title,
Category: release.Category,
@ -64,6 +70,7 @@ func NewMacro(release Release) Macro {
HDR: strings.Join(release.HDR, ", "),
FilterName: release.FilterName,
Size: release.Size,
SizeString: humanize.Bytes(release.Size),
Season: release.Season,
Episode: release.Episode,
Year: release.Year,

View file

@ -89,7 +89,7 @@ func TestMacros_Parse(t *testing.T) {
name: "test_args_long",
release: Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
DownloadURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.TorrentName}} {{.TorrentUrl}} SOME_LONG_TOKEN"},
@ -100,7 +100,7 @@ func TestMacros_Parse(t *testing.T) {
name: "test_args_long_1",
release: Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
DownloadURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}} {{.TorrentName}} {{.TorrentUrl}} SOME_LONG_TOKEN"},
@ -111,7 +111,7 @@ func TestMacros_Parse(t *testing.T) {
name: "test_args_category",
release: Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
DownloadURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}}-race"},
@ -122,7 +122,7 @@ func TestMacros_Parse(t *testing.T) {
name: "test_args_category_year",
release: Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
DownloadURL: "https://some.site/download/fakeid",
Indexer: "mock1",
},
args: args{text: "{{.Indexer}}-{{.CurrentYear}}-race"},
@ -133,7 +133,7 @@ func TestMacros_Parse(t *testing.T) {
name: "test_args_category_year",
release: Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
DownloadURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: []string{"DV"},
@ -146,7 +146,7 @@ func TestMacros_Parse(t *testing.T) {
name: "test_args_category_and_if",
release: Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
DownloadURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: []string{"HDR"},
@ -159,7 +159,7 @@ func TestMacros_Parse(t *testing.T) {
name: "test_release_year_1",
release: Release{
TorrentName: "This movie 2021",
TorrentURL: "https://some.site/download/fakeid",
DownloadURL: "https://some.site/download/fakeid",
Indexer: "mock1",
Resolution: "2160p",
HDR: []string{"HDR"},
@ -178,6 +178,15 @@ func TestMacros_Parse(t *testing.T) {
want: "3.57 GB",
wantErr: false,
},
{
name: "test_size_string",
release: Release{
Size: 3834225472,
},
args: args{text: "Size: {{ .SizeString }}"},
want: "Size: 3.8 GB",
wantErr: false,
},
{
name: "test_text_manipulation",
release: Release{
@ -187,6 +196,24 @@ func TestMacros_Parse(t *testing.T) {
want: "[Blu-ray][MKV][h264 10-bit][1080p][FLAC 2.0][Dual Audio][Softsubs (Sub Group)][Freeleech]",
wantErr: false,
},
{
name: "test_info_url",
release: Release{
InfoURL: "https://test.local/this/page",
},
args: args{text: "InfoUrl: {{ .InfoUrl }}"},
want: "InfoUrl: https://test.local/this/page",
wantErr: false,
},
{
name: "test_download_url",
release: Release{
DownloadURL: "https://test.local/this/page/1001",
},
args: args{text: "DownloadUrl: {{ .DownloadUrl }}"},
want: "DownloadUrl: https://test.local/this/page/1001",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

View file

@ -52,7 +52,7 @@ type Release struct {
Implementation ReleaseImplementation `json:"implementation"` // irc, rss, api
Timestamp time.Time `json:"timestamp"`
InfoURL string `json:"info_url"`
TorrentURL string `json:"download_url"`
DownloadURL string `json:"download_url"`
MagnetURI string `json:"-"`
GroupID string `json:"group_id"`
TorrentID string `json:"torrent_id"`
@ -374,7 +374,7 @@ func (r *Release) downloadTorrentFile(ctx context.Context) error {
return errors.New("could not download file: protocol %s is not supported", r.Protocol)
}
if r.TorrentURL == "" {
if r.DownloadURL == "" {
return errors.New("download_file: url can't be empty")
} else if r.TorrentTmpFile != "" {
// already downloaded
@ -388,7 +388,7 @@ func (r *Release) downloadTorrentFile(ctx context.Context) error {
Timeout: time.Second * 45,
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.TorrentURL, nil)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, r.DownloadURL, nil)
if err != nil {
return errors.Wrap(err, "error downloading file")
}
@ -428,22 +428,22 @@ func (r *Release) downloadTorrentFile(ctx context.Context) error {
// Continue processing the response
//case http.StatusMovedPermanently, http.StatusFound, http.StatusSeeOther, http.StatusTemporaryRedirect, http.StatusPermanentRedirect:
// // Handle redirect
// return retry.Unrecoverable(errors.New("redirect encountered for torrent (%v) file (%v) - status code: %d - check indexer keys for %s", r.TorrentName, r.TorrentURL, resp.StatusCode, r.Indexer))
// return retry.Unrecoverable(errors.New("redirect encountered for torrent (%s) file (%s) - status code: %d - check indexer keys for %s", r.TorrentName, r.DownloadURL, resp.StatusCode, r.Indexer))
case http.StatusUnauthorized, http.StatusForbidden:
return retry.Unrecoverable(errors.New("unrecoverable error downloading torrent (%v) file (%v) - status code: %d - check indexer keys for %s", r.TorrentName, r.TorrentURL, resp.StatusCode, r.Indexer))
return retry.Unrecoverable(errors.New("unrecoverable error downloading torrent (%s) file (%s) - status code: %d - check indexer keys for %s", r.TorrentName, r.DownloadURL, resp.StatusCode, r.Indexer))
case http.StatusMethodNotAllowed:
return retry.Unrecoverable(errors.New("unrecoverable error downloading torrent (%v) file (%v) from '%v' - status code: %d. Check if the request method is correct", r.TorrentName, r.TorrentURL, r.Indexer, resp.StatusCode))
return retry.Unrecoverable(errors.New("unrecoverable error downloading torrent (%s) file (%s) from '%s' - status code: %d. Check if the request method is correct", r.TorrentName, r.DownloadURL, r.Indexer, resp.StatusCode))
case http.StatusNotFound:
return errors.New("torrent %s not found on %s (%d) - retrying", r.TorrentName, r.Indexer, resp.StatusCode)
case http.StatusBadGateway, http.StatusServiceUnavailable, http.StatusGatewayTimeout:
return errors.New("server error (%d) encountered while downloading torrent (%v) file (%v) from '%v' - retrying", resp.StatusCode, r.TorrentName, r.TorrentURL, r.Indexer)
return errors.New("server error (%d) encountered while downloading torrent (%s) file (%s) from '%s' - retrying", resp.StatusCode, r.TorrentName, r.DownloadURL, r.Indexer)
case http.StatusInternalServerError:
return errors.New("server error (%d) encountered while downloading torrent (%v) file (%v) - check indexer keys for %s", resp.StatusCode, r.TorrentName, r.TorrentURL, r.Indexer)
return errors.New("server error (%d) encountered while downloading torrent (%s) file (%s) - check indexer keys for %s", resp.StatusCode, r.TorrentName, r.DownloadURL, r.Indexer)
default:
return retry.Unrecoverable(errors.New("unexpected status code %d: check indexer keys for %s", resp.StatusCode, r.Indexer))
@ -686,7 +686,7 @@ func (r *Release) MapVars(def *IndexerDefinition, varMap map[string]string) erro
if torrentSize, err := getStringMapValue(varMap, "torrentSize"); err == nil {
// handling for indexer who doesn't explicitly set which size unit is used like (AR)
if def.IRC != nil && def.IRC.Parse != nil && def.IRC.Parse.ForceSizeUnit != "" {
torrentSize = fmt.Sprintf("%v %v", torrentSize, def.IRC.Parse.ForceSizeUnit)
torrentSize = fmt.Sprintf("%s %s", torrentSize, def.IRC.Parse.ForceSizeUnit)
}
size, err := humanize.ParseBytes(torrentSize)

View file

@ -620,7 +620,7 @@ func TestRelease_ParseString(t *testing.T) {
Timestamp: tt.fields.Timestamp,
GroupID: tt.fields.GroupID,
TorrentID: tt.fields.TorrentID,
TorrentURL: tt.fields.TorrentURL,
DownloadURL: tt.fields.DownloadURL,
TorrentTmpFile: tt.fields.TorrentTmpFile,
TorrentHash: tt.fields.TorrentHash,
TorrentName: tt.fields.TorrentName,
@ -746,7 +746,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
Timestamp time.Time
GroupID string
TorrentID string
TorrentURL string
DownloadURL string
TorrentTmpFile string
TorrentDataRawBytes []byte
TorrentHash string
@ -799,7 +799,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
TorrentURL: fmt.Sprintf("%s/%d", ts.URL, 401),
DownloadURL: fmt.Sprintf("%s/%d", ts.URL, 401),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
@ -809,7 +809,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
TorrentURL: fmt.Sprintf("%s/%d", ts.URL, 403),
DownloadURL: fmt.Sprintf("%s/%d", ts.URL, 403),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
@ -819,7 +819,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
TorrentURL: fmt.Sprintf("%s/%d", ts.URL, 500),
DownloadURL: fmt.Sprintf("%s/%d", ts.URL, 500),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
@ -829,7 +829,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
TorrentURL: fmt.Sprintf("%s/%s", ts.URL, "file.torrent"),
DownloadURL: fmt.Sprintf("%s/%s", ts.URL, "file.torrent"),
Protocol: ReleaseProtocolTorrent,
},
wantErr: false,
@ -839,7 +839,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
TorrentURL: fmt.Sprintf("%s/files/%s", ts.URL, "valid_torrent_as_html"),
DownloadURL: fmt.Sprintf("%s/files/%s", ts.URL, "valid_torrent_as_html"),
Protocol: ReleaseProtocolTorrent,
},
wantErr: false,
@ -849,7 +849,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
fields: fields{
Indexer: "mock-indexer",
TorrentName: "Test.Release-GROUP",
TorrentURL: fmt.Sprintf("%s/files/%s", ts.URL, "invalid_torrent_as_html"),
DownloadURL: fmt.Sprintf("%s/files/%s", ts.URL, "invalid_torrent_as_html"),
Protocol: ReleaseProtocolTorrent,
},
wantErr: true,
@ -869,7 +869,7 @@ func TestRelease_DownloadTorrentFile(t *testing.T) {
Timestamp: tt.fields.Timestamp,
GroupID: tt.fields.GroupID,
TorrentID: tt.fields.TorrentID,
TorrentURL: tt.fields.TorrentURL,
DownloadURL: tt.fields.DownloadURL,
TorrentTmpFile: tt.fields.TorrentTmpFile,
TorrentDataRawBytes: tt.fields.TorrentDataRawBytes,
TorrentHash: tt.fields.TorrentHash,