diff --git a/pkg/scraper/mapped.go b/pkg/scraper/mapped.go index dd5adede7..45c41519c 100644 --- a/pkg/scraper/mapped.go +++ b/pkg/scraper/mapped.go @@ -393,6 +393,22 @@ func (p *postProcessParseDate) Apply(value string, q mappedQuery) string { return parsedValue.Format(internalDateFormat) } +type postProcessSubtractDays bool + +func (p *postProcessSubtractDays) Apply(value string, q mappedQuery) string { + const internalDateFormat = "2006-01-02" + + i, err := strconv.Atoi(value) + if err != nil { + logger.Warnf("Error parsing day string %s: %s", value, err) + return value + } + + dt := time.Now() + dt = dt.AddDate(0, 0, -i) + return dt.Format(internalDateFormat) +} + type postProcessReplace mappedRegexConfigs func (c *postProcessReplace) Apply(value string, q mappedQuery) string { @@ -479,12 +495,13 @@ func (p *postProcessLbToKg) Apply(value string, q mappedQuery) string { } type mappedPostProcessAction struct { - ParseDate string `yaml:"parseDate"` - Replace mappedRegexConfigs `yaml:"replace"` - SubScraper *mappedScraperAttrConfig `yaml:"subScraper"` - Map map[string]string `yaml:"map"` - FeetToCm bool `yaml:"feetToCm"` - LbToKg bool `yaml:"lbToKg"` + ParseDate string `yaml:"parseDate"` + SubtractDays bool `yaml:"subtractDays"` + Replace mappedRegexConfigs `yaml:"replace"` + SubScraper *mappedScraperAttrConfig `yaml:"subScraper"` + Map map[string]string `yaml:"map"` + FeetToCm bool `yaml:"feetToCm"` + LbToKg bool `yaml:"lbToKg"` } func (a mappedPostProcessAction) ToPostProcessAction() (postProcessAction, error) { @@ -536,6 +553,14 @@ func (a mappedPostProcessAction) ToPostProcessAction() (postProcessAction, error action := postProcessLbToKg(a.LbToKg) ret = &action } + if a.SubtractDays { + if found != "" { + return nil, fmt.Errorf("post-process actions must have a single field, found %s and %s", found, "subtractDays") + } + found = "subtractDays" + action := postProcessSubtractDays(a.SubtractDays) + ret = &action + } if ret == nil { return nil, errors.New("invalid post-process action") diff --git a/ui/v2.5/src/components/Changelog/versions/v080.md b/ui/v2.5/src/components/Changelog/versions/v080.md index 3fee2b020..d83e44312 100644 --- a/ui/v2.5/src/components/Changelog/versions/v080.md +++ b/ui/v2.5/src/components/Changelog/versions/v080.md @@ -2,5 +2,6 @@ * Added [DLNA server](/settings?tab=dlna). ([#1364](https://github.com/stashapp/stash/pull/1364)) ### 🎨 Improvements +* Add `subtractDays` post-process scraper action. ([#1399](https://github.com/stashapp/stash/pull/1399)) * Skip scanning directories if path matches image and video exclude patterns. ([#1382](https://github.com/stashapp/stash/pull/1382)) * Add button to remove studio stash ID. ([#1378](https://github.com/stashapp/stash/pull/1378)) diff --git a/ui/v2.5/src/docs/en/Scraping.md b/ui/v2.5/src/docs/en/Scraping.md index e537bfb2a..9dd8374a9 100644 --- a/ui/v2.5/src/docs/en/Scraping.md +++ b/ui/v2.5/src/docs/en/Scraping.md @@ -372,6 +372,18 @@ Height and weight are extracted from the selected spans and converted to `cm` an * `parseDate`: if present, the value is the date format using go's reference date (2006-01-02). For example, if an example date was `14-Mar-2003`, then the date format would be `02-Jan-2006`. See the [time.Parse documentation](https://golang.org/pkg/time/#Parse) for details. When present, the scraper will convert the input string into a date, then convert it to the string format used by stash (`YYYY-MM-DD`). Strings "Today", "Yesterday" are matched (case insensitive) and converted by the scraper so you don't need to edit/replace them. +* `subtractDays`: if set to `true` it subtracts the value in days from the current date and returns the resulting date in stash's date format. +Example: +```yaml +Date: + selector: //strong[contains(text(),"Added:")]/following-sibling::text() + postProcess: + - replace: + - regex: (\d+)\sdays\sago.+ + with: $1 + - subtractDays: true +``` + * `replace`: contains an array of sub-objects. Each sub-object must have a `regex` and `with` field. The `regex` field is the regex pattern to replace, and `with` is the string to replace it with. `$` is used to reference capture groups - `$1` is the first capture group, `$2` the second and so on. Replacements are performed in order of the array. Example: