Add subtractDays pp action to scraper (#1399)

This commit is contained in:
bnkai
2021-05-21 05:20:12 +03:00
committed by GitHub
parent 76019af3e5
commit ab24d0f625
3 changed files with 44 additions and 6 deletions

View File

@@ -393,6 +393,22 @@ func (p *postProcessParseDate) Apply(value string, q mappedQuery) string {
return parsedValue.Format(internalDateFormat) 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 type postProcessReplace mappedRegexConfigs
func (c *postProcessReplace) Apply(value string, q mappedQuery) string { 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 { type mappedPostProcessAction struct {
ParseDate string `yaml:"parseDate"` ParseDate string `yaml:"parseDate"`
Replace mappedRegexConfigs `yaml:"replace"` SubtractDays bool `yaml:"subtractDays"`
SubScraper *mappedScraperAttrConfig `yaml:"subScraper"` Replace mappedRegexConfigs `yaml:"replace"`
Map map[string]string `yaml:"map"` SubScraper *mappedScraperAttrConfig `yaml:"subScraper"`
FeetToCm bool `yaml:"feetToCm"` Map map[string]string `yaml:"map"`
LbToKg bool `yaml:"lbToKg"` FeetToCm bool `yaml:"feetToCm"`
LbToKg bool `yaml:"lbToKg"`
} }
func (a mappedPostProcessAction) ToPostProcessAction() (postProcessAction, error) { func (a mappedPostProcessAction) ToPostProcessAction() (postProcessAction, error) {
@@ -536,6 +553,14 @@ func (a mappedPostProcessAction) ToPostProcessAction() (postProcessAction, error
action := postProcessLbToKg(a.LbToKg) action := postProcessLbToKg(a.LbToKg)
ret = &action 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 { if ret == nil {
return nil, errors.New("invalid post-process action") return nil, errors.New("invalid post-process action")

View File

@@ -2,5 +2,6 @@
* Added [DLNA server](/settings?tab=dlna). ([#1364](https://github.com/stashapp/stash/pull/1364)) * Added [DLNA server](/settings?tab=dlna). ([#1364](https://github.com/stashapp/stash/pull/1364))
### 🎨 Improvements ### 🎨 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)) * 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)) * Add button to remove studio stash ID. ([#1378](https://github.com/stashapp/stash/pull/1378))

View File

@@ -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. * `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. * `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: Example: