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"
|
|
|
|
"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 StudioCreator interface {
|
2023-06-15 02:46:09 +00:00
|
|
|
Create(ctx context.Context, newStudio *models.Studio) error
|
2022-08-12 02:21:46 +00:00
|
|
|
UpdateStashIDs(ctx context.Context, studioID int, stashIDs []models.StashID) error
|
2022-05-19 07:49:32 +00:00
|
|
|
}
|
|
|
|
|
2022-07-13 06:30:54 +00:00
|
|
|
func createMissingStudio(ctx context.Context, endpoint string, w StudioCreator, studio *models.ScrapedStudio) (*int, error) {
|
2023-06-15 02:46:09 +00:00
|
|
|
studioInput := scrapedToStudioInput(studio)
|
|
|
|
err := w.Create(ctx, &studioInput)
|
2021-10-28 03:25:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error creating studio: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if endpoint != "" && studio.RemoteSiteID != nil {
|
2023-06-15 02:46:09 +00:00
|
|
|
if err := w.UpdateStashIDs(ctx, studioInput.ID, []models.StashID{
|
2021-10-28 03:25:17 +00:00
|
|
|
{
|
|
|
|
Endpoint: endpoint,
|
|
|
|
StashID: *studio.RemoteSiteID,
|
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return nil, fmt.Errorf("error setting studio stash id: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-15 02:46:09 +00:00
|
|
|
return &studioInput.ID, nil
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func scrapedToStudioInput(studio *models.ScrapedStudio) models.Studio {
|
|
|
|
currentTime := time.Now()
|
|
|
|
ret := models.Studio{
|
2023-06-15 02:46:09 +00:00
|
|
|
Name: studio.Name,
|
2022-03-17 00:33:59 +00:00
|
|
|
Checksum: md5.FromString(studio.Name),
|
2023-06-15 02:46:09 +00:00
|
|
|
CreatedAt: currentTime,
|
|
|
|
UpdatedAt: currentTime,
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if studio.URL != nil {
|
2023-06-15 02:46:09 +00:00
|
|
|
ret.URL = *studio.URL
|
2021-10-28 03:25:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|