2021-10-28 03:25:17 +00:00
|
|
|
package identify
|
|
|
|
|
|
|
|
import (
|
2022-05-19 07:49:32 +00:00
|
|
|
"context"
|
2021-10-28 03:25:17 +00:00
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2022-12-01 02:54:08 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
|
2021-10-28 03:25:17 +00:00
|
|
|
)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
type PerformerCreator interface {
|
2022-10-31 03:58:01 +00:00
|
|
|
Create(ctx context.Context, newPerformer *models.Performer) error
|
2022-05-19 07:49:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func getPerformerID(ctx context.Context, endpoint string, w PerformerCreator, p *models.ScrapedPerformer, createMissing bool) (*int, error) {
|
2021-10-28 03:25:17 +00:00
|
|
|
if p.StoredID != nil {
|
|
|
|
// existing performer, just add it
|
|
|
|
performerID, err := strconv.Atoi(*p.StoredID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error converting performer ID %s: %w", *p.StoredID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &performerID, nil
|
|
|
|
} else if createMissing && p.Name != nil { // name is mandatory
|
2022-05-19 07:49:32 +00:00
|
|
|
return createMissingPerformer(ctx, endpoint, w, p)
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
func createMissingPerformer(ctx context.Context, endpoint string, w PerformerCreator, p *models.ScrapedPerformer) (*int, error) {
|
2022-10-31 03:58:01 +00:00
|
|
|
performerInput := scrapedToPerformerInput(p)
|
2021-10-28 03:25:17 +00:00
|
|
|
if endpoint != "" && p.RemoteSiteID != nil {
|
2022-12-01 02:54:08 +00:00
|
|
|
performerInput.StashIDs = models.NewRelatedStashIDs([]models.StashID{
|
2021-10-28 03:25:17 +00:00
|
|
|
{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
StashID: *p.RemoteSiteID,
|
|
|
|
},
|
2022-12-01 02:54:08 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err := w.Create(ctx, &performerInput)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating performer: %w", err)
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
2022-10-31 03:58:01 +00:00
|
|
|
return &performerInput.ID, nil
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func scrapedToPerformerInput(performer *models.ScrapedPerformer) models.Performer {
|
|
|
|
currentTime := time.Now()
|
|
|
|
ret := models.Performer{
|
2022-10-31 03:58:01 +00:00
|
|
|
Name: *performer.Name,
|
|
|
|
CreatedAt: currentTime,
|
|
|
|
UpdatedAt: currentTime,
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Birthdate != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
d := models.NewDate(*performer.Birthdate)
|
|
|
|
ret.Birthdate = &d
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.DeathDate != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
d := models.NewDate(*performer.DeathDate)
|
|
|
|
ret.DeathDate = &d
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Gender != nil {
|
2023-05-24 03:19:35 +00:00
|
|
|
v := models.GenderEnum(*performer.Gender)
|
|
|
|
ret.Gender = &v
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Ethnicity != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.Ethnicity = *performer.Ethnicity
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Country != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.Country = *performer.Country
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.EyeColor != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.EyeColor = *performer.EyeColor
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.HairColor != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.HairColor = *performer.HairColor
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Height != nil {
|
2022-11-08 03:09:03 +00:00
|
|
|
h, err := strconv.Atoi(*performer.Height) // height is stored as an int
|
|
|
|
if err == nil {
|
|
|
|
ret.Height = &h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if performer.Weight != nil {
|
|
|
|
h, err := strconv.Atoi(*performer.Weight)
|
|
|
|
if err == nil {
|
|
|
|
ret.Weight = &h
|
|
|
|
}
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Measurements != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.Measurements = *performer.Measurements
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.FakeTits != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.FakeTits = *performer.FakeTits
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
2023-05-24 03:19:35 +00:00
|
|
|
if performer.PenisLength != nil {
|
|
|
|
h, err := strconv.ParseFloat(*performer.PenisLength, 64)
|
|
|
|
if err == nil {
|
|
|
|
ret.PenisLength = &h
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if performer.Circumcised != nil {
|
|
|
|
v := models.CircumisedEnum(*performer.Circumcised)
|
|
|
|
ret.Circumcised = &v
|
|
|
|
}
|
2021-10-28 03:25:17 +00:00
|
|
|
if performer.CareerLength != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.CareerLength = *performer.CareerLength
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Tattoos != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.Tattoos = *performer.Tattoos
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Piercings != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.Piercings = *performer.Piercings
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Aliases != nil {
|
2022-12-01 02:54:08 +00:00
|
|
|
ret.Aliases = models.NewRelatedStrings(stringslice.FromString(*performer.Aliases, ","))
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Twitter != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.Twitter = *performer.Twitter
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
if performer.Instagram != nil {
|
2022-10-31 03:58:01 +00:00
|
|
|
ret.Instagram = *performer.Instagram
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|