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
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/hash/md5"
|
2021-10-28 03:25:17 +00:00
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
|
|
)
|
|
|
|
|
2022-05-19 07:49:32 +00:00
|
|
|
type PerformerCreator interface {
|
|
|
|
Create(ctx context.Context, newPerformer models.Performer) (*models.Performer, error)
|
|
|
|
UpdateStashIDs(ctx context.Context, performerID int, stashIDs []models.StashID) error
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
created, err := w.Create(ctx, scrapedToPerformerInput(p))
|
2021-10-28 03:25:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating performer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint != "" && p.RemoteSiteID != nil {
|
2022-05-19 07:49:32 +00:00
|
|
|
if err := w.UpdateStashIDs(ctx, created.ID, []models.StashID{
|
2021-10-28 03:25:17 +00:00
|
|
|
{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
StashID: *p.RemoteSiteID,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return nil, fmt.Errorf("error setting performer stash id: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &created.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func scrapedToPerformerInput(performer *models.ScrapedPerformer) models.Performer {
|
|
|
|
currentTime := time.Now()
|
|
|
|
ret := models.Performer{
|
|
|
|
Name: sql.NullString{String: *performer.Name, Valid: true},
|
2022-03-17 00:33:59 +00:00
|
|
|
Checksum: md5.FromString(*performer.Name),
|
2021-10-28 03:25:17 +00:00
|
|
|
CreatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
|
|
|
UpdatedAt: models.SQLiteTimestamp{Timestamp: currentTime},
|
|
|
|
Favorite: sql.NullBool{Bool: false, Valid: true},
|
|
|
|
}
|
|
|
|
if performer.Birthdate != nil {
|
|
|
|
ret.Birthdate = models.SQLiteDate{String: *performer.Birthdate, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.DeathDate != nil {
|
|
|
|
ret.DeathDate = models.SQLiteDate{String: *performer.DeathDate, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Gender != nil {
|
|
|
|
ret.Gender = sql.NullString{String: *performer.Gender, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Ethnicity != nil {
|
|
|
|
ret.Ethnicity = sql.NullString{String: *performer.Ethnicity, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Country != nil {
|
|
|
|
ret.Country = sql.NullString{String: *performer.Country, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.EyeColor != nil {
|
|
|
|
ret.EyeColor = sql.NullString{String: *performer.EyeColor, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.HairColor != nil {
|
|
|
|
ret.HairColor = sql.NullString{String: *performer.HairColor, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Height != nil {
|
|
|
|
ret.Height = sql.NullString{String: *performer.Height, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Measurements != nil {
|
|
|
|
ret.Measurements = sql.NullString{String: *performer.Measurements, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.FakeTits != nil {
|
|
|
|
ret.FakeTits = sql.NullString{String: *performer.FakeTits, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.CareerLength != nil {
|
|
|
|
ret.CareerLength = sql.NullString{String: *performer.CareerLength, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Tattoos != nil {
|
|
|
|
ret.Tattoos = sql.NullString{String: *performer.Tattoos, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Piercings != nil {
|
|
|
|
ret.Piercings = sql.NullString{String: *performer.Piercings, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Aliases != nil {
|
|
|
|
ret.Aliases = sql.NullString{String: *performer.Aliases, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Twitter != nil {
|
|
|
|
ret.Twitter = sql.NullString{String: *performer.Twitter, Valid: true}
|
|
|
|
}
|
|
|
|
if performer.Instagram != nil {
|
|
|
|
ret.Instagram = sql.NullString{String: *performer.Instagram, Valid: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|