feat(indexers): GGn improve release name parsing (#1366)

* feat(indexers): GGn improve IRC parsing

* chore: organize imports
This commit is contained in:
ze0s 2024-01-24 17:58:46 +01:00 committed by GitHub
parent dea0b32b89
commit fffd5bbf56
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 435 additions and 403 deletions

View file

@ -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
}