Skip to content

Commit

Permalink
feat(indexers): GGn improve release name parsing (#1366)
Browse files Browse the repository at this point in the history
* feat(indexers): GGn improve IRC parsing

* chore: organize imports
  • Loading branch information
zze0s committed Jan 24, 2024
1 parent dea0b32 commit fffd5bb
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 397 deletions.
117 changes: 2 additions & 115 deletions internal/announce/announce.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}
71 changes: 0 additions & 71 deletions internal/announce/announce_test.go

This file was deleted.

0 comments on commit fffd5bb

Please sign in to comment.