2021-04-26 02:51:31 +00:00
|
|
|
// Package autotag provides methods to auto-tag scenes with performers,
|
|
|
|
// studios and tags.
|
|
|
|
//
|
|
|
|
// The autotag engine tags scenes with performers/studios/tags if the scene's
|
|
|
|
// path matches the performer/studio/tag name. A scene's path is considered
|
|
|
|
// a match if it contains the performer/studio/tag's full name, ignoring any
|
|
|
|
// '.', '-', '_' characters in the path.
|
|
|
|
//
|
|
|
|
// For example, for a performer "foo bar", the following paths would be
|
|
|
|
// considered a match: "foo bar.mp4", "foobar.mp4", "foo.bar.mp4",
|
|
|
|
// "foo-bar.mp4", "aaa.foo bar.bbb.mp4".
|
|
|
|
// The following would not be considered a match:
|
|
|
|
// "aafoo bar.mp4", "foo barbb.mp4", "foo/bar.mp4"
|
|
|
|
package autotag
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-04-26 02:51:31 +00:00
|
|
|
"fmt"
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/gallery"
|
|
|
|
"github.com/stashapp/stash/pkg/image"
|
2021-04-26 02:51:31 +00:00
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
2021-10-11 12:06:06 +00:00
|
|
|
"github.com/stashapp/stash/pkg/match"
|
2022-07-13 06:30:54 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-05-19 07:49:32 +00:00
|
|
|
"github.com/stashapp/stash/pkg/scene"
|
2021-04-26 02:51:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type tagger struct {
|
2022-06-07 23:02:11 +00:00
|
|
|
ID int
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
Path string
|
|
|
|
trimExt bool
|
2022-03-09 01:01:56 +00:00
|
|
|
|
|
|
|
cache *match.Cache
|
2021-04-26 02:51:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type addLinkFunc func(subjectID, otherID int) (bool, error)
|
2022-07-13 06:30:54 +00:00
|
|
|
type addImageLinkFunc func(o *models.Image) (bool, error)
|
|
|
|
type addGalleryLinkFunc func(o *models.Gallery) (bool, error)
|
|
|
|
type addSceneLinkFunc func(o *models.Scene) (bool, error)
|
2021-04-26 02:51:31 +00:00
|
|
|
|
|
|
|
func (t *tagger) addError(otherType, otherName string, err error) error {
|
|
|
|
return fmt.Errorf("error adding %s '%s' to %s '%s': %s", otherType, otherName, t.Type, t.Name, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *tagger) addLog(otherType, otherName string) {
|
|
|
|
logger.Infof("Added %s '%s' to %s '%s'", otherType, otherName, t.Type, t.Name)
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (t *tagger) tagPerformers(ctx context.Context, performerReader match.PerformerAutoTagQueryer, addFunc addLinkFunc) error {
|
|
|
|
others, err := match.PathToPerformers(ctx, t.Path, performerReader, t.cache, t.trimExt)
|
2021-04-26 02:51:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range others {
|
|
|
|
added, err := addFunc(t.ID, p.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return t.addError("performer", p.Name.String, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if added {
|
|
|
|
t.addLog("performer", p.Name.String)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (t *tagger) tagStudios(ctx context.Context, studioReader match.StudioAutoTagQueryer, addFunc addLinkFunc) error {
|
|
|
|
studio, err := match.PathToStudio(ctx, t.Path, studioReader, t.cache, t.trimExt)
|
2021-04-26 02:51:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-25 21:29:25 +00:00
|
|
|
if studio != nil {
|
2021-04-26 02:51:31 +00:00
|
|
|
added, err := addFunc(t.ID, studio.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return t.addError("studio", studio.Name.String, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if added {
|
|
|
|
t.addLog("studio", studio.Name.String)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func (t *tagger) tagTags(ctx context.Context, tagReader match.TagAutoTagQueryer, addFunc addLinkFunc) error {
|
|
|
|
others, err := match.PathToTags(ctx, t.Path, tagReader, t.cache, t.trimExt)
|
2021-04-26 02:51:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range others {
|
|
|
|
added, err := addFunc(t.ID, p.ID)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return t.addError("tag", p.Name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if added {
|
|
|
|
t.addLog("tag", p.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (t *tagger) tagScenes(ctx context.Context, paths []string, sceneReader scene.Queryer, addFunc addSceneLinkFunc) error {
|
2022-05-19 07:49:32 +00:00
|
|
|
others, err := match.PathToScenes(ctx, t.Name, paths, sceneReader)
|
2021-04-26 02:51:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range others {
|
2022-07-13 06:30:54 +00:00
|
|
|
added, err := addFunc(p)
|
2021-04-26 02:51:31 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return t.addError("scene", p.GetTitle(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if added {
|
|
|
|
t.addLog("scene", p.GetTitle())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2021-05-03 03:09:46 +00:00
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (t *tagger) tagImages(ctx context.Context, paths []string, imageReader image.Queryer, addFunc addImageLinkFunc) error {
|
2022-05-19 07:49:32 +00:00
|
|
|
others, err := match.PathToImages(ctx, t.Name, paths, imageReader)
|
2021-05-03 03:09:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range others {
|
2022-07-13 06:30:54 +00:00
|
|
|
added, err := addFunc(p)
|
2021-05-03 03:09:46 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return t.addError("image", p.GetTitle(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if added {
|
|
|
|
t.addLog("image", p.GetTitle())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func (t *tagger) tagGalleries(ctx context.Context, paths []string, galleryReader gallery.Queryer, addFunc addGalleryLinkFunc) error {
|
2022-05-19 07:49:32 +00:00
|
|
|
others, err := match.PathToGalleries(ctx, t.Name, paths, galleryReader)
|
2021-05-03 03:09:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, p := range others {
|
2022-07-13 06:30:54 +00:00
|
|
|
added, err := addFunc(p)
|
2021-05-03 03:09:46 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return t.addError("gallery", p.GetTitle(), err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if added {
|
|
|
|
t.addLog("gallery", p.GetTitle())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|