2021-10-11 12:06:06 +00:00
|
|
|
package match
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-01-18 01:23:20 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2023-10-16 03:39:41 +00:00
|
|
|
"github.com/stashapp/stash/pkg/performer"
|
2021-09-09 08:13:42 +00:00
|
|
|
"github.com/stashapp/stash/pkg/studio"
|
2021-05-26 04:36:05 +00:00
|
|
|
"github.com/stashapp/stash/pkg/tag"
|
2021-01-18 01:23:20 +00:00
|
|
|
)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
type PerformerFinder interface {
|
2023-10-16 03:39:41 +00:00
|
|
|
models.PerformerQueryer
|
2022-05-19 07:49:32 +00:00
|
|
|
FindByNames(ctx context.Context, names []string, nocase bool) ([]*models.Performer, error)
|
|
|
|
FindByStashID(ctx context.Context, stashID models.StashID) ([]*models.Performer, error)
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
type GroupNamesFinder interface {
|
|
|
|
FindByNames(ctx context.Context, names []string, nocase bool) ([]*models.Group, error)
|
2022-05-19 07:49:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 12:06:06 +00:00
|
|
|
// ScrapedPerformer matches the provided performer with the
|
2021-01-18 01:23:20 +00:00
|
|
|
// performers in the database and sets the ID field if one is found.
|
2022-05-19 07:49:32 +00:00
|
|
|
func ScrapedPerformer(ctx context.Context, qb PerformerFinder, p *models.ScrapedPerformer, stashBoxEndpoint *string) error {
|
2021-10-11 12:06:06 +00:00
|
|
|
if p.StoredID != nil || p.Name == nil {
|
2021-09-07 01:54:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-14 20:51:52 +00:00
|
|
|
// Check if a performer with the StashID already exists
|
|
|
|
if stashBoxEndpoint != nil && p.RemoteSiteID != nil {
|
2022-05-19 07:49:32 +00:00
|
|
|
performers, err := qb.FindByStashID(ctx, models.StashID{
|
2021-11-14 20:51:52 +00:00
|
|
|
StashID: *p.RemoteSiteID,
|
|
|
|
Endpoint: *stashBoxEndpoint,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(performers) > 0 {
|
|
|
|
id := strconv.Itoa(performers[0].ID)
|
|
|
|
p.StoredID = &id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
performers, err := qb.FindByNames(ctx, []string{*p.Name}, true)
|
2021-01-18 01:23:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-01-08 00:50:31 +00:00
|
|
|
if len(performers) == 0 {
|
|
|
|
// if no names matched, try match an exact alias
|
2023-10-16 03:39:41 +00:00
|
|
|
performers, err = performer.ByAlias(ctx, qb, *p.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-08 00:50:31 +00:00
|
|
|
}
|
2023-10-16 03:39:41 +00:00
|
|
|
|
2024-01-08 00:50:31 +00:00
|
|
|
if len(performers) != 1 {
|
|
|
|
// ignore - cannot match
|
|
|
|
return nil
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id := strconv.Itoa(performers[0].ID)
|
2021-09-07 01:54:22 +00:00
|
|
|
p.StoredID = &id
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
type StudioFinder interface {
|
2023-09-01 00:39:29 +00:00
|
|
|
models.StudioQueryer
|
2022-05-19 07:49:32 +00:00
|
|
|
FindByStashID(ctx context.Context, stashID models.StashID) ([]*models.Studio, error)
|
|
|
|
}
|
|
|
|
|
2021-10-11 12:06:06 +00:00
|
|
|
// ScrapedStudio matches the provided studio with the studios
|
2021-01-18 01:23:20 +00:00
|
|
|
// in the database and sets the ID field if one is found.
|
2022-05-19 07:49:32 +00:00
|
|
|
func ScrapedStudio(ctx context.Context, qb StudioFinder, s *models.ScrapedStudio, stashBoxEndpoint *string) error {
|
2021-10-11 12:06:06 +00:00
|
|
|
if s.StoredID != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-11-14 20:51:52 +00:00
|
|
|
// Check if a studio with the StashID already exists
|
|
|
|
if stashBoxEndpoint != nil && s.RemoteSiteID != nil {
|
2022-05-19 07:49:32 +00:00
|
|
|
studios, err := qb.FindByStashID(ctx, models.StashID{
|
2021-11-14 20:51:52 +00:00
|
|
|
StashID: *s.RemoteSiteID,
|
|
|
|
Endpoint: *stashBoxEndpoint,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(studios) > 0 {
|
|
|
|
id := strconv.Itoa(studios[0].ID)
|
|
|
|
s.StoredID = &id
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
st, err := studio.ByName(ctx, qb, s.Name)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-09 08:13:42 +00:00
|
|
|
if st == nil {
|
|
|
|
// try matching by alias
|
2022-05-19 07:49:32 +00:00
|
|
|
st, err = studio.ByAlias(ctx, qb, s.Name)
|
2021-09-09 08:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if st == nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
// ignore - cannot match
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-09 08:13:42 +00:00
|
|
|
id := strconv.Itoa(st.ID)
|
2021-09-07 01:54:22 +00:00
|
|
|
s.StoredID = &id
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
// ScrapedGroup matches the provided movie with the movies
|
|
|
|
// in the database and returns the ID field if one is found.
|
|
|
|
func ScrapedGroup(ctx context.Context, qb GroupNamesFinder, storedID *string, name *string) (matchedID *string, err error) {
|
|
|
|
if storedID != nil || name == nil {
|
|
|
|
return
|
2021-09-07 01:54:22 +00:00
|
|
|
}
|
|
|
|
|
2024-07-03 23:10:26 +00:00
|
|
|
movies, err := qb.FindByNames(ctx, []string{*name}, true)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2024-07-03 23:10:26 +00:00
|
|
|
return
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(movies) != 1 {
|
|
|
|
// ignore - cannot match
|
2024-07-03 23:10:26 +00:00
|
|
|
return
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id := strconv.Itoa(movies[0].ID)
|
2024-07-03 23:10:26 +00:00
|
|
|
matchedID = &id
|
|
|
|
return
|
2021-01-18 01:23:20 +00:00
|
|
|
}
|
|
|
|
|
2021-10-11 12:06:06 +00:00
|
|
|
// ScrapedTag matches the provided tag with the tags
|
2021-01-18 01:23:20 +00:00
|
|
|
// in the database and sets the ID field if one is found.
|
2023-09-01 00:39:29 +00:00
|
|
|
func ScrapedTag(ctx context.Context, qb models.TagQueryer, s *models.ScrapedTag) error {
|
2021-10-11 12:06:06 +00:00
|
|
|
if s.StoredID != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
t, err := tag.ByName(ctx, qb, s.Name)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-26 04:36:05 +00:00
|
|
|
if t == nil {
|
|
|
|
// try matching by alias
|
2022-05-19 07:49:32 +00:00
|
|
|
t, err = tag.ByAlias(ctx, qb, s.Name)
|
2021-05-26 04:36:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if t == nil {
|
2021-01-18 01:23:20 +00:00
|
|
|
// ignore - cannot match
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-26 04:36:05 +00:00
|
|
|
id := strconv.Itoa(t.ID)
|
2021-09-07 01:54:22 +00:00
|
|
|
s.StoredID = &id
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}
|