package announce import ( "bytes" "fmt" "html" "net/url" "regexp" "strconv" "strings" "text/template" "github.com/autobrr/autobrr/internal/domain" "github.com/autobrr/autobrr/pkg/releaseinfo" "github.com/pkg/errors" "github.com/rs/zerolog/log" ) func (s *service) parseLineSingle(def *domain.IndexerDefinition, announce *domain.Announce, line string) error { for _, extract := range def.Parse.Lines { tmpVars := map[string]string{} var err error err = s.parseExtract(extract.Pattern, extract.Vars, tmpVars, line) if err != nil { log.Debug().Msgf("error parsing extract: %v", line) return err } // on lines matched err = s.onLinesMatched(def, tmpVars, announce) if err != nil { log.Debug().Msgf("error match line: %v", line) return err } } return nil } func (s *service) parseMultiLine() error { return nil } func (s *service) parseExtract(pattern string, vars []string, tmpVars map[string]string, line string) error { rxp, err := regExMatch(pattern, line) if err != nil { log.Debug().Msgf("did not match expected line: %v", line) } if rxp == nil { //return nil, nil return nil } // extract matched for i, v := range vars { value := "" if rxp[i] != "" { value = rxp[i] // tmpVars[v] = rxp[i] } tmpVars[v] = value } return nil } func (s *service) onLinesMatched(def *domain.IndexerDefinition, vars map[string]string, announce *domain.Announce) error { // TODO implement set tracker.lastAnnounce = now announce.TorrentName = vars["torrentName"] //err := s.postProcess(ti, vars, *announce) //if err != nil { // return err //} // TODO extractReleaseInfo err := s.extractReleaseInfo(vars, announce.TorrentName) if err != nil { return err } // resolution // source // encoder // canonicalize name err = s.mapToAnnounce(vars, announce) if err != nil { return err } // torrent url torrentUrl, err := s.processTorrentUrl(def.Parse.Match.TorrentURL, vars, def.SettingsMap, def.Parse.Match.Encode) if err != nil { log.Debug().Msgf("error torrent url: %v", err) return err } if torrentUrl != "" { announce.TorrentUrl = torrentUrl } return nil } func (s *service) 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 if extraVars != nil { for k, v := range extraVars { tmpVars[k] = v } } // handle url encode of values if encode != nil { 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 { log.Error().Err(err).Msg("could not create torrent url template") return "", err } var b bytes.Buffer err = tmpl.Execute(&b, &tmpVars) if err != nil { log.Error().Err(err).Msg("could not write torrent url template output") return "", err } return b.String(), nil } func split(r rune) bool { return r == ' ' || r == '.' } func Splitter(s string, splits string) []string { m := make(map[rune]int) for _, r := range splits { m[r] = 1 } splitter := func(r rune) bool { return m[r] == 1 } return strings.FieldsFunc(s, splitter) } func canonicalizeString(s string) []string { //a := strings.FieldsFunc(s, split) a := Splitter(s, " .") return a } func cleanReleaseName(input string) string { // Make a Regex to say we only want letters and numbers reg, err := regexp.Compile("[^a-zA-Z0-9]+") if err != nil { //log.Fatal(err) } processedString := reg.ReplaceAllString(input, " ") return processedString } func findLast(input string, pattern string) (string, error) { matched := make([]string, 0) //for _, s := range arr { rxp, err := regexp.Compile(pattern) if err != nil { return "", err //return errors.Wrapf(err, "invalid regex: %s", value) } matches := rxp.FindStringSubmatch(input) if matches != nil { log.Trace().Msgf("matches: %v", matches) // first value is the match, second value is the text if len(matches) >= 1 { last := matches[len(matches)-1] // add to temp slice matched = append(matched, last) } } //} // check if multiple values in temp slice, if so get the last one if len(matched) >= 1 { last := matched[len(matched)-1] return last, nil } return "", nil } func extractYear(releaseName string) (string, bool) { yearMatch, err := findLast(releaseName, "(?:^|\\D)(19[3-9]\\d|20[012]\\d)(?:\\D|$)") if err != nil { return "", false } log.Trace().Msgf("year matches: %v", yearMatch) return yearMatch, true } func extractSeason(releaseName string) (string, bool) { seasonMatch, err := findLast(releaseName, "\\sS(\\d+)\\s?[ED]\\d+/i") sm2, err := findLast(releaseName, "\\s(?:S|Season\\s*)(\\d+)/i") //sm3, err := findLast(releaseName, "\\s((?= len(s) || i < 0 { return nil, fmt.Errorf("Index is out of range. Index is %d with slice length %d", i, len(s)) } // This creates a new slice by creating 2 slices from the original: // s[:i] -> [1, 2] // s[i+1:] -> [4, 5, 6] // and joining them together using `append` return append(s[:i], s[i+1:]...), nil } func regExMatch(pattern string, value string) ([]string, error) { rxp, err := regexp.Compile(pattern) if err != nil { return nil, err //return errors.Wrapf(err, "invalid regex: %s", value) } matches := rxp.FindStringSubmatch(value) if matches == nil { return nil, nil } res := make([]string, 0) if matches != nil { res, err = removeElement(matches, 0) if err != nil { return nil, err } } return res, nil }