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"
|
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 {
|
|
|
|
FindByNames(ctx context.Context, names []string, nocase bool) ([]*models.Performer, error)
|
|
|
|
FindByStashID(ctx context.Context, stashID models.StashID) ([]*models.Performer, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type MovieNamesFinder interface {
|
|
|
|
FindByNames(ctx context.Context, names []string, nocase bool) ([]*models.Movie, error)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(performers) != 1 {
|
|
|
|
// ignore - cannot match
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
studio.Queryer
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-10-11 12:06:06 +00:00
|
|
|
// ScrapedMovie matches the provided movie with the movies
|
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 ScrapedMovie(ctx context.Context, qb MovieNamesFinder, m *models.ScrapedMovie) error {
|
2021-10-11 12:06:06 +00:00
|
|
|
if m.StoredID != nil || m.Name == nil {
|
2021-09-07 01:54:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
movies, err := qb.FindByNames(ctx, []string{*m.Name}, true)
|
2021-01-18 01:23:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(movies) != 1 {
|
|
|
|
// ignore - cannot match
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id := strconv.Itoa(movies[0].ID)
|
2021-09-07 01:54:22 +00:00
|
|
|
m.StoredID = &id
|
2021-01-18 01:23:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
2022-05-19 07:49:32 +00:00
|
|
|
func ScrapedTag(ctx context.Context, qb tag.Queryer, 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
|
|
|
|
}
|