mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +00:00
feat(indexers): TorrentName templating (#381)
* feat(indexers): animebytes improve parsing * Update animebytes.yaml fix music parsing * parse releaseEpisode * add torrentname template * improve releaseTags parsing * add torrentName templating conditional rendering of group and episode number add freeleech parsing * fix(indexers): improve ab releasename parsing * feat(macros): expose TorrentID, GroupID, and Size * Revert "feat(macros): expose TorrentID, GroupID, and Size" This reverts commit dae40116a1cce40f3c18d057d0af697af4407274. * change year to use parentheses Co-authored-by: ze0s <ze0s@riseup.net> Co-authored-by: varoOP <varoOP@protonmail.com>
This commit is contained in:
parent
ec07c57612
commit
48d6468503
7 changed files with 232 additions and 16 deletions
|
@ -7,6 +7,7 @@ import (
|
|||
"net/url"
|
||||
"text/template"
|
||||
|
||||
"github.com/Masterminds/sprig/v3"
|
||||
"github.com/dustin/go-humanize"
|
||||
)
|
||||
|
||||
|
@ -122,11 +123,12 @@ type IndexerParseExtract struct {
|
|||
}
|
||||
|
||||
type IndexerParseMatch struct {
|
||||
TorrentURL string `json:"torrenturl"`
|
||||
Encode []string `json:"encode"`
|
||||
TorrentURL string `json:"torrenturl"`
|
||||
TorrentName string `json:"torrentname"`
|
||||
Encode []string `json:"encode"`
|
||||
}
|
||||
|
||||
func (p *IndexerParse) ParseTorrentUrl(vars map[string]string, extraVars map[string]string, release *Release) error {
|
||||
func (p *IndexerParse) ParseMatch(vars map[string]string, extraVars map[string]string, release *Release) error {
|
||||
tmpVars := map[string]string{}
|
||||
|
||||
// copy vars to new tmp map
|
||||
|
@ -152,19 +154,37 @@ func (p *IndexerParse) ParseTorrentUrl(vars map[string]string, extraVars map[str
|
|||
}
|
||||
}
|
||||
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New("torrenturl").Parse(p.Match.TorrentURL)
|
||||
if err != nil {
|
||||
return errors.New("could not create torrent url template")
|
||||
if p.Match.TorrentURL != "" {
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New("torrenturl").Funcs(sprig.TxtFuncMap()).Parse(p.Match.TorrentURL)
|
||||
if err != nil {
|
||||
return errors.New("could not create torrent url template")
|
||||
}
|
||||
|
||||
var urlBytes bytes.Buffer
|
||||
err = tmpl.Execute(&urlBytes, &tmpVars)
|
||||
if err != nil {
|
||||
return errors.New("could not write torrent url template output")
|
||||
}
|
||||
|
||||
release.TorrentURL = urlBytes.String()
|
||||
}
|
||||
|
||||
var urlBytes bytes.Buffer
|
||||
err = tmpl.Execute(&urlBytes, &tmpVars)
|
||||
if err != nil {
|
||||
return errors.New("could not write torrent url template output")
|
||||
}
|
||||
if p.Match.TorrentName != "" {
|
||||
// setup text template to inject variables into
|
||||
tmplName, err := template.New("torrentname").Funcs(sprig.TxtFuncMap()).Parse(p.Match.TorrentName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
release.TorrentURL = urlBytes.String()
|
||||
var nameBytes bytes.Buffer
|
||||
err = tmplName.Execute(&nameBytes, &tmpVars)
|
||||
if err != nil {
|
||||
return errors.New("could not write torrent name template output")
|
||||
}
|
||||
|
||||
release.TorrentName = nameBytes.String()
|
||||
}
|
||||
|
||||
// handle cookies
|
||||
if v, ok := extraVars["cookie"]; ok {
|
||||
|
|
163
internal/domain/indexer_test.go
Normal file
163
internal/domain/indexer_test.go
Normal file
|
@ -0,0 +1,163 @@
|
|||
package domain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestIndexerParse_ParseMatch(t *testing.T) {
|
||||
type fields struct {
|
||||
Type string
|
||||
ForceSizeUnit string
|
||||
Lines []IndexerParseExtract
|
||||
Match IndexerParseMatch
|
||||
}
|
||||
type args struct {
|
||||
vars map[string]string
|
||||
extraVars map[string]string
|
||||
release *Release
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
expect *Release
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "test_01",
|
||||
fields: fields{
|
||||
Type: "",
|
||||
ForceSizeUnit: "",
|
||||
Lines: []IndexerParseExtract{
|
||||
{
|
||||
Test: nil,
|
||||
Pattern: "New Torrent Announcement:\\s*<([^>]*)>\\s*Name:'(.*)' uploaded by '([^']*)'\\s*(freeleech)*\\s*-\\s*(https?\\:\\/\\/[^\\/]+\\/)torrent\\/(\\d+)",
|
||||
Vars: []string{
|
||||
"category",
|
||||
"torrentName",
|
||||
"uploader",
|
||||
"freeleech",
|
||||
"baseUrl",
|
||||
"torrentId",
|
||||
},
|
||||
},
|
||||
},
|
||||
Match: IndexerParseMatch{
|
||||
TorrentURL: "{{ .baseUrl }}rss/download/{{ .torrentId }}/{{ .rsskey }}/{{ .torrentName }}.torrent",
|
||||
TorrentName: "",
|
||||
Encode: []string{"torrentName"},
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
vars: map[string]string{
|
||||
"category": "TV :: Episodes HD",
|
||||
"torrentName": "The Show 2019 S03E08 2160p DV WEBRip 6CH x265 HEVC-GROUP",
|
||||
"uploader": "Anonymous",
|
||||
"freeleech": "",
|
||||
"baseUrl": "https://mock.org/",
|
||||
"torrentId": "240860011",
|
||||
},
|
||||
extraVars: map[string]string{
|
||||
"rsskey": "00000000000000000000",
|
||||
},
|
||||
release: &Release{
|
||||
Indexer: "mock",
|
||||
FilterStatus: ReleaseStatusFilterPending,
|
||||
Rejections: []string{},
|
||||
Protocol: ReleaseProtocolTorrent,
|
||||
Implementation: ReleaseImplementationIRC,
|
||||
},
|
||||
},
|
||||
expect: &Release{
|
||||
Indexer: "mock",
|
||||
FilterStatus: ReleaseStatusFilterPending,
|
||||
Rejections: []string{},
|
||||
Protocol: ReleaseProtocolTorrent,
|
||||
Implementation: ReleaseImplementationIRC,
|
||||
TorrentURL: "https://mock.org/rss/download/240860011/00000000000000000000/The+Show+2019+S03E08+2160p+DV+WEBRip+6CH+x265+HEVC-GROUP.torrent",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "test_02",
|
||||
fields: fields{
|
||||
Type: "",
|
||||
ForceSizeUnit: "",
|
||||
Lines: []IndexerParseExtract{
|
||||
{
|
||||
Test: nil,
|
||||
Pattern: `(.*?)(?: - )?(Visual Novel|Light Novel|TV.*|Movie|Manga|OVA|ONA|DVD Special|BD Special|Oneshot|Anthology|Manhwa|Manhua|Artbook|Game|Live Action.*|)[\s\p{Zs}]{2,}\[(\d+)\] :: (.*?)(?: \/ (?:RAW|Softsubs|Hardsubs|Translated)\s\((.+)\)(?:.*Episode\s(\d+))?(?:.*(Freeleech))?.*)? \|\| (https.*)\/torrents.*\?id=\d+&torrentid=(\d+) \|\| (.+?(?:(?:\|\| Uploaded by|$))?) (?:\|\| Uploaded by: (.*))?$`,
|
||||
Vars: []string{
|
||||
"torrentName",
|
||||
"category",
|
||||
"year",
|
||||
"releaseTags",
|
||||
"releaseGroup",
|
||||
"releaseEpisode",
|
||||
"freeleech",
|
||||
"baseUrl",
|
||||
"torrentId",
|
||||
"tags",
|
||||
"uploader",
|
||||
},
|
||||
},
|
||||
},
|
||||
Match: IndexerParseMatch{
|
||||
TorrentURL: "{{ .baseUrl }}/torrent/{{ .torrentId }}/download/{{ .passkey }}",
|
||||
TorrentName: `{{ if .releaseGroup }}[{{ .releaseGroup }}] {{ end }}{{ .torrentName }} [{{ .year }}] {{ if .releaseEpisode }}{{ printf "- %02s " .releaseEpisode }}{{ end }}{{ print "[" .releaseTags "]" | replace " / " "][" }}`,
|
||||
Encode: nil,
|
||||
},
|
||||
},
|
||||
args: args{
|
||||
vars: map[string]string{
|
||||
"torrentName": "Great BluRay SoftSubbed Anime",
|
||||
"category": "TV Series",
|
||||
"year": "2020",
|
||||
"releaseTags": "Blu-ray / MKV / h264 10-bit / 1080p / FLAC 2.0 / Dual Audio / Softsubs (Sub Group) / Freeleech",
|
||||
"releaseGroup": "Softsubs",
|
||||
"releaseEpisode": "",
|
||||
"freeleech": "freeleech",
|
||||
"baseUrl": "https://mock.org",
|
||||
"torrentId": "240860011",
|
||||
"tags": "comedy, drama, school.life, sports",
|
||||
"uploader": "Uploader",
|
||||
},
|
||||
extraVars: map[string]string{
|
||||
"passkey": "00000000000000000000",
|
||||
},
|
||||
release: &Release{
|
||||
Indexer: "mock",
|
||||
FilterStatus: ReleaseStatusFilterPending,
|
||||
Rejections: []string{},
|
||||
Protocol: ReleaseProtocolTorrent,
|
||||
Implementation: ReleaseImplementationIRC,
|
||||
},
|
||||
},
|
||||
expect: &Release{
|
||||
Indexer: "mock",
|
||||
FilterStatus: ReleaseStatusFilterPending,
|
||||
Rejections: []string{},
|
||||
Protocol: ReleaseProtocolTorrent,
|
||||
Implementation: ReleaseImplementationIRC,
|
||||
TorrentURL: "https://mock.org/torrent/240860011/download/00000000000000000000",
|
||||
TorrentName: "[Softsubs] Great BluRay SoftSubbed Anime [2020] [Blu-ray][MKV][h264 10-bit][1080p][FLAC 2.0][Dual Audio][Softsubs (Sub Group)][Freeleech]",
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
p := &IndexerParse{
|
||||
Type: tt.fields.Type,
|
||||
ForceSizeUnit: tt.fields.ForceSizeUnit,
|
||||
Lines: tt.fields.Lines,
|
||||
Match: tt.fields.Match,
|
||||
}
|
||||
|
||||
p.ParseMatch(tt.args.vars, tt.args.extraVars, tt.args.release)
|
||||
assert.Equal(t, tt.expect, tt.args.release)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -484,6 +484,11 @@ func (r *Release) MapVars(def *IndexerDefinition, varMap map[string]string) erro
|
|||
r.Group = releaseGroup
|
||||
}
|
||||
|
||||
if episodeVal, err := getStringMapValue(varMap, "releaseEpisode"); err == nil {
|
||||
episode, _ := strconv.Atoi(episodeVal);
|
||||
r.Episode = episode
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue