mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 16:59:12 +00:00
feat(indexers): GGn improve release name parsing (#1366)
* feat(indexers): GGn improve IRC parsing * chore: organize imports
This commit is contained in:
parent
dea0b32b89
commit
fffd5bbf56
5 changed files with 435 additions and 403 deletions
|
@ -4,10 +4,7 @@
|
|||
package announce
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"net/url"
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/domain"
|
||||
"github.com/autobrr/autobrr/internal/indexer"
|
||||
|
@ -105,8 +102,8 @@ func (a *announceProcessor) processQueue(queue chan string) {
|
|||
rls.Protocol = domain.ReleaseProtocol(a.indexer.Protocol)
|
||||
|
||||
// on lines matched
|
||||
if err := a.onLinesMatched(a.indexer, tmpVars, rls); err != nil {
|
||||
a.log.Error().Err(err).Msg("error match line")
|
||||
if err := a.indexer.IRC.Parse.Parse(a.indexer, tmpVars, rls); err != nil {
|
||||
a.log.Error().Err(err).Msg("announce: could not parse announce for release")
|
||||
continue
|
||||
}
|
||||
|
||||
|
@ -138,113 +135,3 @@ func (a *announceProcessor) AddLineToQueue(channel string, line string) error {
|
|||
|
||||
return nil
|
||||
}
|
||||
|
||||
// onLinesMatched process vars into release
|
||||
func (a *announceProcessor) onLinesMatched(def *domain.IndexerDefinition, vars map[string]string, rls *domain.Release) error {
|
||||
// map variables from regex capture onto release struct
|
||||
if err := rls.MapVars(def, vars); err != nil {
|
||||
a.log.Error().Err(err).Msg("announce: could not map vars for release")
|
||||
return err
|
||||
}
|
||||
|
||||
// since OPS uses en-dashes as separators, which causes moistari/rls to not the torrentName properly,
|
||||
// we replace the en-dashes with hyphens here
|
||||
if def.Identifier == "ops" {
|
||||
rls.TorrentName = strings.ReplaceAll(rls.TorrentName, "–", "-")
|
||||
}
|
||||
|
||||
// parse fields
|
||||
// run before ParseMatch to not potentially use a reconstructed TorrentName
|
||||
rls.ParseString(rls.TorrentName)
|
||||
|
||||
// set baseUrl to default domain
|
||||
baseUrl := def.URLS[0]
|
||||
|
||||
// override baseUrl
|
||||
if def.BaseURL != "" {
|
||||
baseUrl = def.BaseURL
|
||||
}
|
||||
|
||||
// merge vars from regex captures on announce and vars from settings
|
||||
mergedVars := mergeVars(vars, def.SettingsMap)
|
||||
|
||||
// parse torrentUrl
|
||||
matched, err := def.IRC.Parse.ParseMatch(baseUrl, mergedVars)
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msgf("announce: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
if matched != nil {
|
||||
rls.DownloadURL = matched.TorrentURL
|
||||
|
||||
if matched.InfoURL != "" {
|
||||
rls.InfoURL = matched.InfoURL
|
||||
}
|
||||
|
||||
// only used by few indexers
|
||||
if matched.TorrentName != "" {
|
||||
rls.TorrentName = matched.TorrentName
|
||||
}
|
||||
}
|
||||
|
||||
// handle optional cookies
|
||||
if v, ok := def.SettingsMap["cookie"]; ok {
|
||||
rls.RawCookie = v
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *announceProcessor) processTorrentUrl(match string, vars map[string]string, extraVars map[string]string, encode []string) (string, error) {
|
||||
tmpVars := map[string]string{}
|
||||
|
||||
// copy vars to new tmp map
|
||||
for k, v := range vars {
|
||||
tmpVars[k] = v
|
||||
}
|
||||
|
||||
// merge extra vars with vars
|
||||
for k, v := range extraVars {
|
||||
tmpVars[k] = v
|
||||
}
|
||||
|
||||
// handle url encode of values
|
||||
for _, e := range encode {
|
||||
if v, ok := tmpVars[e]; ok {
|
||||
// url encode value
|
||||
t := url.QueryEscape(v)
|
||||
tmpVars[e] = t
|
||||
}
|
||||
}
|
||||
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New("torrenturl").Parse(match)
|
||||
if err != nil {
|
||||
a.log.Error().Err(err).Msg("could not create torrent url template")
|
||||
return "", err
|
||||
}
|
||||
|
||||
var b bytes.Buffer
|
||||
if err := tmpl.Execute(&b, &tmpVars); err != nil {
|
||||
a.log.Error().Err(err).Msg("could not write torrent url template output")
|
||||
return "", err
|
||||
}
|
||||
|
||||
a.log.Trace().Msg("torrenturl processed")
|
||||
|
||||
return b.String(), nil
|
||||
}
|
||||
|
||||
// mergeVars merge maps
|
||||
func mergeVars(data ...map[string]string) map[string]string {
|
||||
tmpVars := map[string]string{}
|
||||
|
||||
for _, vars := range data {
|
||||
// copy vars to new tmp map
|
||||
for k, v := range vars {
|
||||
tmpVars[k] = v
|
||||
}
|
||||
}
|
||||
return tmpVars
|
||||
}
|
||||
|
|
|
@ -1,71 +0,0 @@
|
|||
// Copyright (c) 2021 - 2024, Ludvig Lundgren and the autobrr contributors.
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
package announce
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_announceProcessor_processTorrentUrl(t *testing.T) {
|
||||
type args struct {
|
||||
match string
|
||||
vars map[string]string
|
||||
extraVars map[string]string
|
||||
encode []string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "passing with vars_1",
|
||||
args: args{
|
||||
match: "{{ .baseUrl }}api/v1/torrents/{{ .torrentId }}/torrent?key={{ .apikey }}",
|
||||
vars: map[string]string{
|
||||
"baseUrl": "https://example.test/",
|
||||
"torrentId": "000000",
|
||||
},
|
||||
extraVars: map[string]string{
|
||||
"apikey": "abababab+01010101",
|
||||
},
|
||||
encode: []string{"apikey"},
|
||||
},
|
||||
want: "https://example.test/api/v1/torrents/000000/torrent?key=abababab%2B01010101",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "passing with vars_2",
|
||||
args: args{
|
||||
match: "{{ .baseUrl }}/download.php/{{ .torrentId }}/{{ .torrentName }}.torrent?torrent_pass={{ .passkey }}",
|
||||
vars: map[string]string{
|
||||
"baseUrl": "https://example.test",
|
||||
"torrentId": "000000",
|
||||
"torrentName": "That Movie 2020 Blu-ray 1080p REMUX AVC DTS-HD MA 7 1 GROUP",
|
||||
},
|
||||
extraVars: map[string]string{
|
||||
"passkey": "abababab01010101",
|
||||
},
|
||||
encode: []string{"torrentName"},
|
||||
},
|
||||
want: "https://example.test/download.php/000000/That+Movie+2020+Blu-ray+1080p+REMUX+AVC+DTS-HD+MA+7+1+GROUP.torrent?torrent_pass=abababab01010101",
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
a := &announceProcessor{}
|
||||
got, err := a.processTorrentUrl(tt.args.match, tt.args.vars, tt.args.extraVars, tt.args.encode)
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("processTorrentUrl() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
assert.Equal(t, tt.want, got)
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue