Files
bitmagnet/internal/classifier/action_parse_date.go
T
mgdigital c16f76130c Classifier rewrite (#213)
The classifier has been re-implemented and now uses a DSL allowing for full customisation. Several bugs have also been fixed.

- Closes https://github.com/bitmagnet-io/bitmagnet/issues/182
- Closes https://github.com/bitmagnet-io/bitmagnet/issues/70
- Closes https://github.com/bitmagnet-io/bitmagnet/issues/68
- Hopefully fixes https://github.com/bitmagnet-io/bitmagnet/issues/126
2024-04-21 16:24:10 +01:00

41 lines
1012 B
Go

package classifier
import (
"github.com/bitmagnet-io/bitmagnet/internal/classifier/classification"
"github.com/bitmagnet-io/bitmagnet/internal/classifier/parsers"
)
const parseDateName = "parse_date"
type parseDateAction struct{}
func (parseDateAction) name() string {
return parseDateName
}
var parseDatePayloadSpec = payloadLiteral[string]{
literal: parseDateName,
description: "Try to parse a date from the name of the current torrent",
}
func (parseDateAction) compileAction(ctx compilerContext) (action, error) {
if _, err := parseDatePayloadSpec.Unmarshal(ctx); err != nil {
return action{}, ctx.error(err)
}
return action{
run: func(ctx executionContext) (classification.Result, error) {
parsed := parsers.ParseDate(ctx.torrent.Name)
if parsed.IsNil() {
return ctx.result, classification.ErrUnmatched
}
cl := ctx.result
cl.Date = parsed
return cl, nil
},
}, nil
}
func (parseDateAction) JsonSchema() JsonSchema {
return parseDatePayloadSpec.JsonSchema()
}