mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 08:49:13 +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
|
@ -232,11 +232,52 @@ type IndexerIRCParseMatched struct {
|
|||
TorrentName string
|
||||
}
|
||||
|
||||
func (p *IndexerIRCParse) ParseMatch(baseURL string, vars map[string]string) (*IndexerIRCParseMatched, error) {
|
||||
matched := &IndexerIRCParseMatched{}
|
||||
func parseTemplateURL(baseURL, sourceURL string, vars map[string]string, basename string) (*url.URL, error) {
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New(basename).Funcs(sprig.TxtFuncMap()).Parse(sourceURL)
|
||||
if err != nil {
|
||||
return nil, errors.New("could not create %s url template", basename)
|
||||
}
|
||||
|
||||
var urlBytes bytes.Buffer
|
||||
if err := tmpl.Execute(&urlBytes, &vars); err != nil {
|
||||
return nil, errors.New("could not write %s url template output", basename)
|
||||
}
|
||||
|
||||
templateUrl := urlBytes.String()
|
||||
parsedUrl, err := url.Parse(templateUrl)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not parse template url: %q", templateUrl)
|
||||
}
|
||||
|
||||
// for backwards compatibility remove Host and Scheme to rebuild url
|
||||
if parsedUrl.Host != "" {
|
||||
parsedUrl.Host = ""
|
||||
}
|
||||
if parsedUrl.Scheme != "" {
|
||||
parsedUrl.Scheme = ""
|
||||
}
|
||||
|
||||
// join baseURL with query
|
||||
baseUrlPath, err := url.JoinPath(baseURL, parsedUrl.Path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not join %s url", basename)
|
||||
}
|
||||
|
||||
// reconstruct url
|
||||
infoUrl, err := url.Parse(baseUrlPath)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not parse %s url", basename)
|
||||
}
|
||||
|
||||
infoUrl.RawQuery = parsedUrl.RawQuery
|
||||
|
||||
return infoUrl, nil
|
||||
}
|
||||
|
||||
func (p *IndexerIRCParseMatch) ParseURLs(baseURL string, vars map[string]string, rls *Release) error {
|
||||
// handle url encode of values
|
||||
for _, e := range p.Match.Encode {
|
||||
for _, e := range p.Encode {
|
||||
if v, ok := vars[e]; ok {
|
||||
// url encode value
|
||||
t := url.QueryEscape(v)
|
||||
|
@ -244,100 +285,86 @@ func (p *IndexerIRCParse) ParseMatch(baseURL string, vars map[string]string) (*I
|
|||
}
|
||||
}
|
||||
|
||||
if p.Match.InfoURL != "" {
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New("infourl").Funcs(sprig.TxtFuncMap()).Parse(p.Match.InfoURL)
|
||||
if p.InfoURL != "" {
|
||||
infoURL, err := parseTemplateURL(baseURL, p.InfoURL, vars, "infourl")
|
||||
if err != nil {
|
||||
return nil, errors.New("could not create info url template")
|
||||
return err
|
||||
}
|
||||
|
||||
var urlBytes bytes.Buffer
|
||||
if err := tmpl.Execute(&urlBytes, &vars); err != nil {
|
||||
return nil, errors.New("could not write info url template output")
|
||||
}
|
||||
|
||||
templateUrl := urlBytes.String()
|
||||
parsedUrl, err := url.Parse(templateUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// for backwards compatibility remove Host and Scheme to rebuild url
|
||||
if parsedUrl.Host != "" {
|
||||
parsedUrl.Host = ""
|
||||
}
|
||||
if parsedUrl.Scheme != "" {
|
||||
parsedUrl.Scheme = ""
|
||||
}
|
||||
|
||||
// join baseURL with query
|
||||
baseUrlPath, err := url.JoinPath(baseURL, parsedUrl.Path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not join info url")
|
||||
}
|
||||
|
||||
// reconstruct url
|
||||
infoUrl, _ := url.Parse(baseUrlPath)
|
||||
infoUrl.RawQuery = parsedUrl.RawQuery
|
||||
|
||||
matched.InfoURL = infoUrl.String()
|
||||
rls.InfoURL = infoURL.String()
|
||||
}
|
||||
|
||||
if p.Match.TorrentURL != "" {
|
||||
// setup text template to inject variables into
|
||||
tmpl, err := template.New("torrenturl").Funcs(sprig.TxtFuncMap()).Parse(p.Match.TorrentURL)
|
||||
if p.TorrentURL != "" {
|
||||
downloadURL, err := parseTemplateURL(baseURL, p.TorrentURL, vars, "torrenturl")
|
||||
if err != nil {
|
||||
return nil, errors.New("could not create torrent url template")
|
||||
return err
|
||||
}
|
||||
|
||||
var urlBytes bytes.Buffer
|
||||
if err := tmpl.Execute(&urlBytes, &vars); err != nil {
|
||||
return nil, errors.New("could not write torrent url template output")
|
||||
}
|
||||
|
||||
templateUrl := urlBytes.String()
|
||||
parsedUrl, err := url.Parse(templateUrl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// for backwards compatibility remove Host and Scheme to rebuild url
|
||||
if parsedUrl.Host != "" {
|
||||
parsedUrl.Host = ""
|
||||
}
|
||||
if parsedUrl.Scheme != "" {
|
||||
parsedUrl.Scheme = ""
|
||||
}
|
||||
|
||||
// join baseURL with query
|
||||
baseUrlPath, err := url.JoinPath(baseURL, parsedUrl.Path)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "could not join torrent url")
|
||||
}
|
||||
|
||||
// reconstruct url
|
||||
torrentUrl, _ := url.Parse(baseUrlPath)
|
||||
torrentUrl.RawQuery = parsedUrl.RawQuery
|
||||
|
||||
matched.TorrentURL = torrentUrl.String()
|
||||
rls.DownloadURL = downloadURL.String()
|
||||
}
|
||||
|
||||
if p.Match.TorrentName != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *IndexerIRCParseMatch) ParseTorrentName(vars map[string]string, rls *Release) error {
|
||||
if p.TorrentName != "" {
|
||||
// setup text template to inject variables into
|
||||
tmplName, err := template.New("torrentname").Funcs(sprig.TxtFuncMap()).Parse(p.Match.TorrentName)
|
||||
tmplName, err := template.New("torrentname").Funcs(sprig.TxtFuncMap()).Parse(p.TorrentName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
|
||||
var nameBytes bytes.Buffer
|
||||
if err := tmplName.Execute(&nameBytes, &vars); err != nil {
|
||||
return nil, errors.New("could not write torrent name template output")
|
||||
return errors.New("could not write torrent name template output")
|
||||
}
|
||||
|
||||
matched.TorrentName = nameBytes.String()
|
||||
rls.TorrentName = nameBytes.String()
|
||||
}
|
||||
|
||||
return matched, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *IndexerIRCParse) Parse(def *IndexerDefinition, vars map[string]string, rls *Release) error {
|
||||
if err := rls.MapVars(def, vars); err != nil {
|
||||
return errors.Wrap(err, "could not map variables for release")
|
||||
}
|
||||
|
||||
baseUrl := def.URLS[0]
|
||||
|
||||
// merge vars from regex captures on announce and vars from settings
|
||||
mergedVars := mergeVars(vars, def.SettingsMap)
|
||||
|
||||
// parse urls
|
||||
if err := def.IRC.Parse.Match.ParseURLs(baseUrl, mergedVars, rls); err != nil {
|
||||
return errors.Wrap(err, "could not parse urls for release")
|
||||
}
|
||||
|
||||
// parse torrent var
|
||||
if err := def.IRC.Parse.Match.ParseTorrentName(mergedVars, rls); err != nil {
|
||||
return errors.Wrap(err, "could not parse release name")
|
||||
}
|
||||
|
||||
var parser IRCParser
|
||||
|
||||
switch def.Identifier {
|
||||
case "ggn":
|
||||
parser = IRCParserGazelleGames{}
|
||||
case "ops":
|
||||
parser = IRCParserOrpheus{}
|
||||
default:
|
||||
parser = IRCParserDefault{}
|
||||
}
|
||||
|
||||
if err := parser.Parse(rls, vars); err != nil {
|
||||
return errors.Wrap(err, "could not parse release")
|
||||
}
|
||||
|
||||
if v, ok := def.SettingsMap["cookie"]; ok {
|
||||
rls.RawCookie = v
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type TorrentBasic struct {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue