Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion internal/classifier/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package classifier
import (
"errors"
"fmt"
"strings"

"github.com/bitmagnet-io/bitmagnet/internal/classifier/classification"
)
Expand Down Expand Up @@ -61,14 +62,18 @@ outer:
return action{}, errors.Join(errs...)
}

path := ctx.path

return action{func(ctx executionContext) (classification.Result, error) {
for _, a := range actions {
for i, a := range actions {
ctx.logger = ctx._logger.Named(strings.Join(path, ".")+".["+fmt.Sprint(i)+"]")
result, err := a.run(ctx)
if err != nil {
return classification.Result{}, err
}
ctx = ctx.withResult(result)
}
ctx.logger = ctx._logger
return ctx.result, nil
}}, errors.Join(errs...)
}
Expand Down
1 change: 1 addition & 0 deletions internal/classifier/action_add_tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (addTagAction) compileAction(ctx compilerContext) (action, error) {

return action{
func(ctx executionContext) (classification.Result, error) {
ctx.logger.Info(tags)
cl := ctx.result
if cl.Tags == nil {
cl.Tags = make(map[string]struct{})
Expand Down
13 changes: 13 additions & 0 deletions internal/classifier/action_attach_local_content_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (attachLocalContentByIDAction) compileAction(ctx compilerContext) (action,
run: func(ctx executionContext) (classification.Result, error) {
cl := ctx.result
if ctx.torrent.Hint.IsNil() || !ctx.torrent.Hint.ContentSource.Valid {
ctx.logger.Info("hint missing or invalid")
return cl, classification.ErrUnmatched
}
content, err := ctx.search.ContentByID(ctx, model.ContentRef{
Expand All @@ -35,8 +36,20 @@ func (attachLocalContentByIDAction) compileAction(ctx compilerContext) (action,
ID: ctx.torrent.Hint.ContentID.String,
})
if err != nil {
ctx.logger.Infow(
"local search failed",
"source",ctx.torrent.Hint.ContentSource.String,
"type",ctx.torrent.Hint.ContentType.String(),
"id",ctx.torrent.Hint.ContentID.String)
return cl, err
}
ctx.logger.Infow(
"local search succeeded",
"source",ctx.torrent.Hint.ContentSource.String,
"type", content.Type.String(),
"id",content.ID,
"title",content.Title,
"year",content.ReleaseYear.String())
cl.AttachContent(&content)
return cl, nil
},
Expand Down
14 changes: 14 additions & 0 deletions internal/classifier/action_attach_local_content_by_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (attachLocalContentBySearchAction) compileAction(ctx compilerContext) (acti
run: func(ctx executionContext) (classification.Result, error) {
cl := ctx.result
if !cl.ContentType.Valid || !cl.BaseTitle.Valid {
ctx.logger.Info("invalid content type or base title")
return cl, classification.ErrUnmatched
}
content, err := ctx.search.ContentBySearch(
Expand All @@ -35,8 +36,21 @@ func (attachLocalContentBySearchAction) compileAction(ctx compilerContext) (acti
cl.Date.Year,
)
if err != nil {
ctx.logger.Infow(
"local search failed",
"type",cl.ContentType.ContentType.String(),
"base_title",cl.BaseTitle.String,
"date",cl.Date.IsoDateString())
return cl, err
}
ctx.logger.Infow(
"local search succeeded",
"base_title",cl.BaseTitle.String,
"date",cl.Date.IsoDateString(),
"id",content.ID,
"type",content.Type.String(),
"title",content.Title,
"year",content.ReleaseYear.String())
cl.AttachContent(&content)
return cl, nil
},
Expand Down
16 changes: 16 additions & 0 deletions internal/classifier/action_attach_tmdb_content_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (attachTMDBContentByIDAction) compileAction(ctx compilerContext) (action, e
var ref model.ContentRef
maybeRef := ctx.torrent.Hint.ContentRef()
if !maybeRef.Valid {
ctx.logger.Info("hint missing or invalid")
return cl, classification.ErrUnmatched
}
ref = maybeRef.Val
Expand All @@ -42,12 +43,14 @@ func (attachTMDBContentByIDAction) compileAction(ctx compilerContext) (action, e
case model.SourceTmdb:
id, err := strconv.Atoi(ref.ID)
if err != nil {
ctx.logger.Info("invalid tmdb id")
return cl, classification.ErrUnmatched
}
tmdbID = int64(id)
default:
id, err := ctx.tmdbGetTMDBIDByExternalID(ref)
if err != nil {
ctx.logger.Info("failed to get tmdb id by external id")
return cl, err
}
tmdbID = id
Expand All @@ -57,16 +60,29 @@ func (attachTMDBContentByIDAction) compileAction(ctx compilerContext) (action, e
case model.ContentTypeMovie, model.ContentTypeXxx:
c, err := ctx.tmdbGetMovieByTMDBID(tmdbID)
if err != nil {
ctx.logger.Infow("movie not found", "id", tmdbID)
return cl, err
}
ctx.logger.Infow(
"movie",
"id", tmdbID,
"title", c.Title,
"year", c.ReleaseYear.String())
content = &c
case model.ContentTypeTvShow:
c, err := ctx.tmdbGetTVShowByTMDBID(tmdbID)
if err != nil {
ctx.logger.Infow("tv show not found", "id", tmdbID)
return cl, err
}
ctx.logger.Infow(
"tv show",
"id", tmdbID,
"title", c.Title,
"year", c.ReleaseYear.String())
content = &c
default:
ctx.logger.Info("invalid content type")
return cl, classification.ErrUnmatched
}
cl.AttachContent(content)
Expand Down
24 changes: 24 additions & 0 deletions internal/classifier/action_attach_tmdb_content_by_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,48 @@ func (attachTmdbContentBySearchAction) compileAction(ctx compilerContext) (actio
run: func(ctx executionContext) (classification.Result, error) {
cl := ctx.result
if !cl.BaseTitle.Valid {
ctx.logger.Info("invalid base title")
return cl, classification.ErrUnmatched
}
var content *model.Content
switch cl.ContentType.ContentType {
case model.ContentTypeTvShow:
result, searchErr := ctx.tmdbSearchTVShow(cl.BaseTitle.String, cl.Date.Year)
if searchErr != nil {
ctx.logger.Infow(
"tv show not found",
"base_title", cl.BaseTitle.String,
"date", cl.Date.IsoDateString())
return cl, searchErr
}
ctx.logger.Infow(
"tv show",
"base_title", cl.BaseTitle.String,
"date", cl.Date.IsoDateString(),
"id", result.ID,
"title", result.Title,
"year", result.ReleaseYear.String())
content = &result
default:
if len(cl.Episodes) > 0 {
ctx.logger.Info("content type is not tv show but episodes are present")
return cl, classification.ErrUnmatched
}
result, searchErr := ctx.tmdbSearchMovie(cl.BaseTitle.String, cl.Date.Year)
if searchErr != nil {
ctx.logger.Infow(
"movie not found",
"base_title", cl.BaseTitle.String,
"date", cl.Date.IsoDateString())
return cl, searchErr
}
ctx.logger.Infow(
"movie",
"base_title", cl.BaseTitle.String,
"date", cl.Date.IsoDateString(),
"id", result.ID,
"title", result.Title,
"year", result.ReleaseYear.String())
content = &result
}
cl.AttachContent(content)
Expand Down
1 change: 1 addition & 0 deletions internal/classifier/action_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (deleteAction) compileAction(ctx compilerContext) (action, error) {

return action{
run: func(ctx executionContext) (classification.Result, error) {
ctx.logger.Info()
return ctx.result, classification.RuntimeError{
Cause: classification.ErrDeleteTorrent,
Path: path,
Expand Down
1 change: 1 addition & 0 deletions internal/classifier/action_find_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (findMatchAction) compileAction(ctx compilerContext) (action, error) {

return action{
func(ctx executionContext) (classification.Result, error) {
ctx.logger.Info()
for _, action := range actions {
result, err := action.run(ctx)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion internal/classifier/action_if_else.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,21 @@ func (ifElseAction) compileAction(ctx compilerContext) (action, error) {

return action{
run: func(ctx executionContext) (classification.Result, error) {
if result, err := cond.check(ctx); err != nil {
ctx.logger.Info()
logger:=ctx.logger
ctx.logger=logger.Named("if_else.condition")
result, err := cond.check(ctx)
ctx.logger=logger
if err != nil {
ctx.logger.Info("error evaluating condition")
return classification.Result{}, err
} else if result {
ctx.logger.Named("if_else.if_action").Info()
if ifAction.run != nil {
return ifAction.run(ctx)
}
} else {
ctx.logger.Named("if_else.else_action").Info()
if elseAction.run != nil {
return elseAction.run(ctx)
}
Expand Down
2 changes: 2 additions & 0 deletions internal/classifier/action_parse_date.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func (parseDateAction) compileAction(ctx compilerContext) (action, error) {
run: func(ctx executionContext) (classification.Result, error) {
parsed := parsers.ParseDate(ctx.torrent.Name)
if parsed.IsNil() {
ctx.logger.Info(nil)
return ctx.result, classification.ErrUnmatched
}
cl := ctx.result
cl.Date = parsed
ctx.logger.Info(parsed.IsoDateString())
return cl, nil
},
}, nil
Expand Down
17 changes: 17 additions & 0 deletions internal/classifier/action_parse_video_content.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package classifier

import (
"encoding/json"

"github.com/bitmagnet-io/bitmagnet/internal/classifier/classification"
"github.com/bitmagnet-io/bitmagnet/internal/classifier/parsers"
"github.com/bitmagnet-io/bitmagnet/internal/model"
)

const parseVideoContentName = "parse_video_content"
Expand All @@ -28,8 +31,22 @@ func (parseVideoContentAction) compileAction(ctx compilerContext) (action, error
parsed, err := parsers.ParseVideoContent(ctx.torrent, ctx.result)
cl := ctx.result
if err != nil {
ctx.logger.Info("error")
return cl, err
}

nulldate := model.Date{ Year: 0, Month: 0, Day: 0 }
var mparsed map[string]any
jparsed, _ := json.Marshal(parsed)
json.Unmarshal(jparsed, &mparsed)
for k, v := range mparsed {
arr, ok := v.([]any)
if (ok && len(arr) == 0) || v == nil || v == 0 || v == "" || v == "0001-01-01T00:00:00Z" || v == "0000000000000000000000000000000000000000" || v == nulldate {
delete(mparsed, k)
}
}

ctx.logger.Infow("result", "parsed", mparsed)
cl.Merge(parsed)
return cl, nil
},
Expand Down
1 change: 1 addition & 0 deletions internal/classifier/action_run_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (runWorkflowAction) compileAction(ctx compilerContext) (action, error) {
var err error
cl := ctx.result
for _, name := range names {
ctx.logger.Info(name)
cl, err = ctx.workflows[name].run(ctx.withResult(cl))
if err != nil {
return cl, err
Expand Down
1 change: 1 addition & 0 deletions internal/classifier/action_set_content_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (setContentTypeAction) compileAction(ctx compilerContext) (action, error) {
func(ctx executionContext) (classification.Result, error) {
cl := ctx.result
cl.ContentType = contentType
ctx.logger.Info(contentType.ContentType.String())
return cl, nil
},
}, nil
Expand Down
1 change: 1 addition & 0 deletions internal/classifier/action_unmatched.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func (unmatchedAction) compileAction(ctx compilerContext) (action, error) {

return action{
run: func(ctx executionContext) (classification.Result, error) {
ctx.logger.Info()
return ctx.result, classification.RuntimeError{Cause: classification.ErrUnmatched, Path: path}
},
}, nil
Expand Down
3 changes: 3 additions & 0 deletions internal/classifier/classifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
tmdb_mocks "github.com/bitmagnet-io/bitmagnet/internal/tmdb/mocks"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"go.uber.org/zap"
)

func TestClassifier(t *testing.T) {
Expand Down Expand Up @@ -250,6 +251,8 @@ func newTestClassifierMocks(t *testing.T) testClassifierMocks {
dependencies: dependencies{
search: search,
tmdbClient: tmdbClient,
_logger: zap.NewNop().Sugar(),
logger: zap.NewNop().Sugar(),
},
},
search: search,
Expand Down
11 changes: 9 additions & 2 deletions internal/classifier/condition_and.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package classifier

import "fmt"

const andName = "and"

type andCondition struct{}
Expand Down Expand Up @@ -39,8 +41,13 @@ func (andCondition) compileCondition(ctx compilerContext) (condition, error) {

return condition{
check: func(ctx executionContext) (bool, error) {
for _, c := range conds {
if result, err := c.check(ctx); err != nil {
ctx.logger.Info()
for i, c := range conds {
logger := ctx.logger
ctx.logger = logger.Named(fmt.Sprintf("and.[%d]", i))
result, err := c.check(ctx)
ctx.logger = logger
if err != nil {
return false, err
} else if !result {
return false, nil
Expand Down
5 changes: 5 additions & 0 deletions internal/classifier/condition_expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (expressionCondition) compileCondition(ctx compilerContext) (condition, err
return condition{}, ctx.error(err)
}

source := ctx.source

return condition{
check: func(ctx executionContext) (bool, error) {
vars := map[string]any{
Expand All @@ -71,12 +73,15 @@ func (expressionCondition) compileCondition(ctx compilerContext) (condition, err
}
result, _, err := prg.ContextEval(ctx.Context, vars)
if err != nil {
ctx.logger.Infow("error","source",source)
return false, err
}
bl, ok := result.Value().(bool)
if !ok {
ctx.logger.Infow("not bool","source",source)
return false, errors.New("not bool")
}
ctx.logger.Infow(fmt.Sprint(bl),"source",source)
return bl, nil
},
}, nil
Expand Down
1 change: 1 addition & 0 deletions internal/classifier/condition_not.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func (notCondition) compileCondition(ctx compilerContext) (condition, error) {

return condition{
check: func(ctx executionContext) (bool, error) {
ctx.logger.Info()
result, err := cond.check(ctx)
return !result, err
},
Expand Down
Loading