-
-
Notifications
You must be signed in to change notification settings - Fork 77
/
action_parse_date.go
40 lines (33 loc) · 1012 Bytes
/
action_parse_date.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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()
}