mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 04:14:39 +03:00
Refactor stashbox package (#5699)
* Move stashbox package under pkg * Remove StashBox from method names * Add fingerprint conversion methods to Fingerprint Refactor Fingerprints methods * Make FindSceneByFingerprints accept fingerprints not scene ids * Refactor SubmitSceneDraft to not require readers * Have SubmitFingerprints accept scenes Remove SceneReader dependency * Move ScrapedScene to models package * Move ScrapedImage into models package * Move ScrapedGallery into models package * Move Scene relationship matching out of stashbox package This is now expected to be done in the client code * Remove TagFinder dependency from stashbox.Client * Make stashbox scene find full hierarchy of studios * Move studio resolution into separate method * Move studio matching out of stashbox package This is now client code responsibility * Move performer matching out of FindPerformerByID and FindPerformerByName * Refactor performer querying logic and remove unused stashbox models Renames FindStashBoxPerformersByPerformerNames to QueryPerformers and accepts names instead of performer ids * Refactor SubmitPerformerDraft to not load relationships This will be the responsibility of the calling code * Remove repository references
This commit is contained in:
48
pkg/stashbox/client.go
Normal file
48
pkg/stashbox/client.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Package stashbox provides a client interface to a stash-box server instance.
|
||||
package stashbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/Yamashou/gqlgenc/clientv2"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/scraper"
|
||||
"github.com/stashapp/stash/pkg/stashbox/graphql"
|
||||
)
|
||||
|
||||
// Client represents the client interface to a stash-box server instance.
|
||||
type Client struct {
|
||||
client *graphql.Client
|
||||
box models.StashBox
|
||||
|
||||
// tag patterns to be excluded
|
||||
excludeTagRE []*regexp.Regexp
|
||||
}
|
||||
|
||||
// NewClient returns a new instance of a stash-box client.
|
||||
func NewClient(box models.StashBox, excludeTagPatterns []string) *Client {
|
||||
authHeader := func(ctx context.Context, req *http.Request, gqlInfo *clientv2.GQLRequestInfo, res interface{}, next clientv2.RequestInterceptorFunc) error {
|
||||
req.Header.Set("ApiKey", box.APIKey)
|
||||
return next(ctx, req, gqlInfo, res)
|
||||
}
|
||||
|
||||
client := &graphql.Client{
|
||||
Client: clientv2.NewClient(http.DefaultClient, box.Endpoint, nil, authHeader),
|
||||
}
|
||||
|
||||
return &Client{
|
||||
client: client,
|
||||
box: box,
|
||||
excludeTagRE: scraper.CompileExclusionRegexps(excludeTagPatterns),
|
||||
}
|
||||
}
|
||||
|
||||
func (c Client) getHTTPClient() *http.Client {
|
||||
return c.client.Client.Client
|
||||
}
|
||||
|
||||
func (c Client) GetUser(ctx context.Context) (*graphql.Me, error) {
|
||||
return c.client.Me(ctx)
|
||||
}
|
||||
132
pkg/stashbox/draft.go
Normal file
132
pkg/stashbox/draft.go
Normal file
@@ -0,0 +1,132 @@
|
||||
package stashbox
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
|
||||
"github.com/Yamashou/gqlgenc/clientv2"
|
||||
"github.com/Yamashou/gqlgenc/graphqljson"
|
||||
)
|
||||
|
||||
func (c *Client) submitDraft(ctx context.Context, query string, input interface{}, image io.Reader, ret interface{}) error {
|
||||
vars := map[string]interface{}{
|
||||
"input": input,
|
||||
}
|
||||
|
||||
r := &clientv2.Request{
|
||||
Query: query,
|
||||
Variables: vars,
|
||||
OperationName: "",
|
||||
}
|
||||
|
||||
requestBody, err := json.Marshal(r)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode: %w", err)
|
||||
}
|
||||
|
||||
body := &bytes.Buffer{}
|
||||
writer := multipart.NewWriter(body)
|
||||
if err := writer.WriteField("operations", string(requestBody)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if image != nil {
|
||||
if err := writer.WriteField("map", "{ \"0\": [\"variables.input.image\"] }"); err != nil {
|
||||
return err
|
||||
}
|
||||
part, _ := writer.CreateFormFile("0", "draft")
|
||||
if _, err := io.Copy(part, image); err != nil {
|
||||
return err
|
||||
}
|
||||
} else if err := writer.WriteField("map", "{}"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
writer.Close()
|
||||
|
||||
req, _ := http.NewRequestWithContext(ctx, "POST", c.box.Endpoint, body)
|
||||
req.Header.Add("Content-Type", writer.FormDataContentType())
|
||||
req.Header.Set("ApiKey", c.box.APIKey)
|
||||
|
||||
httpClient := c.client.Client.Client
|
||||
resp, err := httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
responseBytes, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
type response struct {
|
||||
Data json.RawMessage `json:"data"`
|
||||
Errors json.RawMessage `json:"errors"`
|
||||
}
|
||||
|
||||
var respGQL response
|
||||
|
||||
if err := json.Unmarshal(responseBytes, &respGQL); err != nil {
|
||||
return fmt.Errorf("failed to decode data %s: %w", string(responseBytes), err)
|
||||
}
|
||||
|
||||
if len(respGQL.Errors) > 0 {
|
||||
// try to parse standard graphql error
|
||||
errors := &clientv2.GqlErrorList{}
|
||||
if e := json.Unmarshal(responseBytes, errors); e != nil {
|
||||
return fmt.Errorf("failed to parse graphql errors. Response content %s - %w ", string(responseBytes), e)
|
||||
}
|
||||
|
||||
return errors
|
||||
}
|
||||
|
||||
if err := graphqljson.UnmarshalData(respGQL.Data, ret); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
// we can't currently use this due to https://github.com/Yamashou/gqlgenc/issues/109
|
||||
// func uploadImage(image io.Reader) client.HTTPRequestOption {
|
||||
// return func(req *http.Request) {
|
||||
// if image == nil {
|
||||
// // return without changing anything
|
||||
// return
|
||||
// }
|
||||
|
||||
// // we can't handle errors in here, so if one happens, just return
|
||||
// // without changing anything.
|
||||
|
||||
// // repackage the request to include the image
|
||||
// bodyBytes, err := ioutil.ReadAll(req.Body)
|
||||
// if err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
// newBody := &bytes.Buffer{}
|
||||
// writer := multipart.NewWriter(newBody)
|
||||
// _ = writer.WriteField("operations", string(bodyBytes))
|
||||
|
||||
// if err := writer.WriteField("map", "{ \"0\": [\"variables.input.image\"] }"); err != nil {
|
||||
// return
|
||||
// }
|
||||
// part, _ := writer.CreateFormFile("0", "draft")
|
||||
// if _, err := io.Copy(part, image); err != nil {
|
||||
// return
|
||||
// }
|
||||
|
||||
// writer.Close()
|
||||
|
||||
// // now set the request body to this new body
|
||||
// req.Body = io.NopCloser(newBody)
|
||||
// req.ContentLength = int64(newBody.Len())
|
||||
// req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
// }
|
||||
// }
|
||||
1791
pkg/stashbox/graphql/generated_client.go
Normal file
1791
pkg/stashbox/graphql/generated_client.go
Normal file
File diff suppressed because it is too large
Load Diff
2685
pkg/stashbox/graphql/generated_models.go
Normal file
2685
pkg/stashbox/graphql/generated_models.go
Normal file
File diff suppressed because it is too large
Load Diff
20
pkg/stashbox/graphql/override.go
Normal file
20
pkg/stashbox/graphql/override.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package graphql
|
||||
|
||||
import "github.com/99designs/gqlgen/graphql"
|
||||
|
||||
// Override for generated struct due to mistaken omitempty
|
||||
// https://github.com/Yamashou/gqlgenc/issues/77
|
||||
type SceneDraftInput struct {
|
||||
ID *string `json:"id,omitempty"`
|
||||
Title *string `json:"title,omitempty"`
|
||||
Code *string `json:"code,omitempty"`
|
||||
Details *string `json:"details,omitempty"`
|
||||
Director *string `json:"director,omitempty"`
|
||||
URL *string `json:"url,omitempty"`
|
||||
Date *string `json:"date,omitempty"`
|
||||
Studio *DraftEntityInput `json:"studio,omitempty"`
|
||||
Performers []*DraftEntityInput `json:"performers"`
|
||||
Tags []*DraftEntityInput `json:"tags,omitempty"`
|
||||
Image *graphql.Upload `json:"image,omitempty"`
|
||||
Fingerprints []*FingerprintInput `json:"fingerprints"`
|
||||
}
|
||||
431
pkg/stashbox/performer.go
Normal file
431
pkg/stashbox/performer.go
Normal file
@@ -0,0 +1,431 @@
|
||||
package stashbox
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/scraper"
|
||||
"github.com/stashapp/stash/pkg/sliceutil"
|
||||
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
|
||||
"github.com/stashapp/stash/pkg/stashbox/graphql"
|
||||
"github.com/stashapp/stash/pkg/utils"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
)
|
||||
|
||||
// QueryPerformer queries stash-box for performers using a query string.
|
||||
func (c Client) QueryPerformer(ctx context.Context, queryStr string) ([]*models.ScrapedPerformer, error) {
|
||||
performers, err := c.queryPerformer(ctx, queryStr)
|
||||
|
||||
// set the deprecated image field
|
||||
for _, p := range performers {
|
||||
if len(p.Images) > 0 {
|
||||
p.Image = &p.Images[0]
|
||||
}
|
||||
}
|
||||
|
||||
return performers, err
|
||||
}
|
||||
|
||||
func (c Client) queryPerformer(ctx context.Context, queryStr string) ([]*models.ScrapedPerformer, error) {
|
||||
performers, err := c.client.SearchPerformer(ctx, queryStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
performerFragments := performers.SearchPerformer
|
||||
|
||||
var ret []*models.ScrapedPerformer
|
||||
var ignoredTags []string
|
||||
for _, fragment := range performerFragments {
|
||||
performer := performerFragmentToScrapedPerformer(*fragment)
|
||||
|
||||
// exclude tags that match the excludeTagRE
|
||||
var thisIgnoredTags []string
|
||||
performer.Tags, thisIgnoredTags = scraper.FilterTags(c.excludeTagRE, performer.Tags)
|
||||
ignoredTags = sliceutil.AppendUniques(ignoredTags, thisIgnoredTags)
|
||||
|
||||
ret = append(ret, performer)
|
||||
}
|
||||
|
||||
scraper.LogIgnoredTags(ignoredTags)
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// QueryPerformers queries stash-box for performers using a list of names.
|
||||
func (c Client) QueryPerformers(ctx context.Context, names []string) ([][]*models.ScrapedPerformer, error) {
|
||||
ret := make([][]*models.ScrapedPerformer, len(names))
|
||||
for i, name := range names {
|
||||
if name != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
var err error
|
||||
ret[i], err = c.queryPerformer(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func findURL(urls []*graphql.URLFragment, urlType string) *string {
|
||||
for _, u := range urls {
|
||||
if u.Type == urlType {
|
||||
ret := u.URL
|
||||
return &ret
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func enumToStringPtr(e fmt.Stringer, titleCase bool) *string {
|
||||
if e != nil {
|
||||
ret := strings.ReplaceAll(e.String(), "_", " ")
|
||||
if titleCase {
|
||||
c := cases.Title(language.Und)
|
||||
ret = c.String(strings.ToLower(ret))
|
||||
}
|
||||
return &ret
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func translateGender(gender *graphql.GenderEnum) *string {
|
||||
var res models.GenderEnum
|
||||
switch *gender {
|
||||
case graphql.GenderEnumMale:
|
||||
res = models.GenderEnumMale
|
||||
case graphql.GenderEnumFemale:
|
||||
res = models.GenderEnumFemale
|
||||
case graphql.GenderEnumIntersex:
|
||||
res = models.GenderEnumIntersex
|
||||
case graphql.GenderEnumTransgenderFemale:
|
||||
res = models.GenderEnumTransgenderFemale
|
||||
case graphql.GenderEnumTransgenderMale:
|
||||
res = models.GenderEnumTransgenderMale
|
||||
case graphql.GenderEnumNonBinary:
|
||||
res = models.GenderEnumNonBinary
|
||||
}
|
||||
|
||||
if res != "" {
|
||||
strVal := res.String()
|
||||
return &strVal
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func formatMeasurements(m graphql.MeasurementsFragment) *string {
|
||||
if m.BandSize != nil && m.CupSize != nil && m.Hip != nil && m.Waist != nil {
|
||||
ret := fmt.Sprintf("%d%s-%d-%d", *m.BandSize, *m.CupSize, *m.Waist, *m.Hip)
|
||||
return &ret
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func formatCareerLength(start, end *int) *string {
|
||||
if start == nil && end == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var ret string
|
||||
switch {
|
||||
case end == nil:
|
||||
ret = fmt.Sprintf("%d -", *start)
|
||||
case start == nil:
|
||||
ret = fmt.Sprintf("- %d", *end)
|
||||
default:
|
||||
ret = fmt.Sprintf("%d - %d", *start, *end)
|
||||
}
|
||||
|
||||
return &ret
|
||||
}
|
||||
|
||||
func formatBodyModifications(m []*graphql.BodyModificationFragment) *string {
|
||||
if len(m) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var retSlice []string
|
||||
for _, f := range m {
|
||||
if f.Description == nil {
|
||||
retSlice = append(retSlice, f.Location)
|
||||
} else {
|
||||
retSlice = append(retSlice, fmt.Sprintf("%s, %s", f.Location, *f.Description))
|
||||
}
|
||||
}
|
||||
|
||||
ret := strings.Join(retSlice, "; ")
|
||||
return &ret
|
||||
}
|
||||
|
||||
func fetchImage(ctx context.Context, client *http.Client, url string) (*string, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := client.Do(req)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// determine the image type and set the base64 type
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
if contentType == "" {
|
||||
contentType = http.DetectContentType(body)
|
||||
}
|
||||
|
||||
img := "data:" + contentType + ";base64," + utils.GetBase64StringFromData(body)
|
||||
return &img, nil
|
||||
}
|
||||
|
||||
func performerFragmentToScrapedPerformer(p graphql.PerformerFragment) *models.ScrapedPerformer {
|
||||
images := []string{}
|
||||
for _, image := range p.Images {
|
||||
images = append(images, image.URL)
|
||||
}
|
||||
|
||||
sp := &models.ScrapedPerformer{
|
||||
Name: &p.Name,
|
||||
Disambiguation: p.Disambiguation,
|
||||
Country: p.Country,
|
||||
Measurements: formatMeasurements(*p.Measurements),
|
||||
CareerLength: formatCareerLength(p.CareerStartYear, p.CareerEndYear),
|
||||
Tattoos: formatBodyModifications(p.Tattoos),
|
||||
Piercings: formatBodyModifications(p.Piercings),
|
||||
Twitter: findURL(p.Urls, "TWITTER"),
|
||||
RemoteSiteID: &p.ID,
|
||||
RemoteDeleted: p.Deleted,
|
||||
RemoteMergedIntoId: p.MergedIntoID,
|
||||
Images: images,
|
||||
// TODO - tags not currently supported
|
||||
// graphql schema change to accommodate this. Leave off for now.
|
||||
}
|
||||
|
||||
if len(sp.Images) > 0 {
|
||||
sp.Image = &sp.Images[0]
|
||||
}
|
||||
|
||||
if p.Height != nil && *p.Height > 0 {
|
||||
hs := strconv.Itoa(*p.Height)
|
||||
sp.Height = &hs
|
||||
}
|
||||
|
||||
if p.BirthDate != nil {
|
||||
sp.Birthdate = padFuzzyDate(p.BirthDate)
|
||||
}
|
||||
|
||||
if p.DeathDate != nil {
|
||||
sp.DeathDate = padFuzzyDate(p.DeathDate)
|
||||
}
|
||||
|
||||
if p.Gender != nil {
|
||||
sp.Gender = translateGender(p.Gender)
|
||||
}
|
||||
|
||||
if p.Ethnicity != nil {
|
||||
sp.Ethnicity = enumToStringPtr(p.Ethnicity, true)
|
||||
}
|
||||
|
||||
if p.EyeColor != nil {
|
||||
sp.EyeColor = enumToStringPtr(p.EyeColor, true)
|
||||
}
|
||||
|
||||
if p.HairColor != nil {
|
||||
sp.HairColor = enumToStringPtr(p.HairColor, true)
|
||||
}
|
||||
|
||||
if p.BreastType != nil {
|
||||
sp.FakeTits = enumToStringPtr(p.BreastType, true)
|
||||
}
|
||||
|
||||
if len(p.Aliases) > 0 {
|
||||
// #4437 - stash-box may return aliases that are equal to the performer name
|
||||
// filter these out
|
||||
p.Aliases = sliceutil.Filter(p.Aliases, func(s string) bool {
|
||||
return !strings.EqualFold(s, p.Name)
|
||||
})
|
||||
|
||||
// #4596 - stash-box may return duplicate aliases. Filter these out
|
||||
p.Aliases = stringslice.UniqueFold(p.Aliases)
|
||||
|
||||
alias := strings.Join(p.Aliases, ", ")
|
||||
sp.Aliases = &alias
|
||||
}
|
||||
|
||||
for _, u := range p.Urls {
|
||||
sp.URLs = append(sp.URLs, u.URL)
|
||||
}
|
||||
|
||||
return sp
|
||||
}
|
||||
|
||||
func padFuzzyDate(date *string) *string {
|
||||
if date == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
var paddedDate string
|
||||
switch len(*date) {
|
||||
case 10:
|
||||
paddedDate = *date
|
||||
case 7:
|
||||
paddedDate = fmt.Sprintf("%s-01", *date)
|
||||
case 4:
|
||||
paddedDate = fmt.Sprintf("%s-01-01", *date)
|
||||
}
|
||||
return &paddedDate
|
||||
}
|
||||
|
||||
// FindPerformerByID queries stash-box for a performer by ID.
|
||||
func (c Client) FindPerformerByID(ctx context.Context, id string) (*models.ScrapedPerformer, error) {
|
||||
performer, err := c.client.FindPerformerByID(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if performer.FindPerformer == nil {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
ret := performerFragmentToScrapedPerformer(*performer.FindPerformer)
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// FindPerformerByName queries stash-box for a performer by name.
|
||||
// Unlike QueryPerformer, this function will only return a performer if the name matches exactly.
|
||||
func (c Client) FindPerformerByName(ctx context.Context, name string) (*models.ScrapedPerformer, error) {
|
||||
performers, err := c.client.SearchPerformer(ctx, name)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ret *models.ScrapedPerformer
|
||||
for _, performer := range performers.SearchPerformer {
|
||||
if strings.EqualFold(performer.Name, name) {
|
||||
ret = performerFragmentToScrapedPerformer(*performer)
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// SubmitPerformerDraft submits a performer draft to stash-box.
|
||||
// The performer parameter must have aliases, URLs and stash IDs loaded.
|
||||
func (c Client) SubmitPerformerDraft(ctx context.Context, performer *models.Performer, img []byte) (*string, error) {
|
||||
draft := graphql.PerformerDraftInput{}
|
||||
var image io.Reader
|
||||
endpoint := c.box.Endpoint
|
||||
|
||||
if len(img) > 0 {
|
||||
image = bytes.NewReader(img)
|
||||
}
|
||||
|
||||
if performer.Name != "" {
|
||||
draft.Name = performer.Name
|
||||
}
|
||||
if performer.Disambiguation != "" {
|
||||
draft.Disambiguation = &performer.Disambiguation
|
||||
}
|
||||
if performer.Birthdate != nil {
|
||||
d := performer.Birthdate.String()
|
||||
draft.Birthdate = &d
|
||||
}
|
||||
if performer.Country != "" {
|
||||
draft.Country = &performer.Country
|
||||
}
|
||||
if performer.Ethnicity != "" {
|
||||
draft.Ethnicity = &performer.Ethnicity
|
||||
}
|
||||
if performer.EyeColor != "" {
|
||||
draft.EyeColor = &performer.EyeColor
|
||||
}
|
||||
if performer.FakeTits != "" {
|
||||
draft.BreastType = &performer.FakeTits
|
||||
}
|
||||
if performer.Gender != nil && performer.Gender.IsValid() {
|
||||
v := performer.Gender.String()
|
||||
draft.Gender = &v
|
||||
}
|
||||
if performer.HairColor != "" {
|
||||
draft.HairColor = &performer.HairColor
|
||||
}
|
||||
if performer.Height != nil {
|
||||
v := strconv.Itoa(*performer.Height)
|
||||
draft.Height = &v
|
||||
}
|
||||
if performer.Measurements != "" {
|
||||
draft.Measurements = &performer.Measurements
|
||||
}
|
||||
if performer.Piercings != "" {
|
||||
draft.Piercings = &performer.Piercings
|
||||
}
|
||||
if performer.Tattoos != "" {
|
||||
draft.Tattoos = &performer.Tattoos
|
||||
}
|
||||
if len(performer.Aliases.List()) > 0 {
|
||||
aliases := strings.Join(performer.Aliases.List(), ",")
|
||||
draft.Aliases = &aliases
|
||||
}
|
||||
if performer.CareerLength != "" {
|
||||
var career = strings.Split(performer.CareerLength, "-")
|
||||
if i, err := strconv.Atoi(strings.TrimSpace(career[0])); err == nil {
|
||||
draft.CareerStartYear = &i
|
||||
}
|
||||
if len(career) == 2 {
|
||||
if y, err := strconv.Atoi(strings.TrimSpace(career[1])); err == nil {
|
||||
draft.CareerEndYear = &y
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(performer.URLs.List()) > 0 {
|
||||
draft.Urls = performer.URLs.List()
|
||||
}
|
||||
|
||||
var stashID *string
|
||||
for _, v := range performer.StashIDs.List() {
|
||||
c := v
|
||||
if v.Endpoint == endpoint {
|
||||
stashID = &c.StashID
|
||||
break
|
||||
}
|
||||
}
|
||||
draft.ID = stashID
|
||||
|
||||
var id *string
|
||||
var ret graphql.SubmitPerformerDraft
|
||||
err := c.submitDraft(ctx, graphql.SubmitPerformerDraftDocument, draft, image, &ret)
|
||||
id = ret.SubmitPerformerDraft.ID
|
||||
|
||||
return id, err
|
||||
|
||||
// ret, err := c.client.SubmitPerformerDraft(ctx, draft, uploadImage(image))
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// id := ret.SubmitPerformerDraft.ID
|
||||
// return id, nil
|
||||
}
|
||||
468
pkg/stashbox/scene.go
Normal file
468
pkg/stashbox/scene.go
Normal file
@@ -0,0 +1,468 @@
|
||||
package stashbox
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/scraper"
|
||||
"github.com/stashapp/stash/pkg/sliceutil"
|
||||
"github.com/stashapp/stash/pkg/stashbox/graphql"
|
||||
"github.com/stashapp/stash/pkg/utils"
|
||||
)
|
||||
|
||||
// QueryScene queries stash-box for scenes using a query string.
|
||||
func (c Client) QueryScene(ctx context.Context, queryStr string) ([]*models.ScrapedScene, error) {
|
||||
scenes, err := c.client.SearchScene(ctx, queryStr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sceneFragments := scenes.SearchScene
|
||||
|
||||
var ret []*models.ScrapedScene
|
||||
var ignoredTags []string
|
||||
for _, s := range sceneFragments {
|
||||
ss, err := c.sceneFragmentToScrapedScene(ctx, s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var thisIgnoredTags []string
|
||||
ss.Tags, thisIgnoredTags = scraper.FilterTags(c.excludeTagRE, ss.Tags)
|
||||
ignoredTags = sliceutil.AppendUniques(ignoredTags, thisIgnoredTags)
|
||||
|
||||
ret = append(ret, ss)
|
||||
}
|
||||
|
||||
scraper.LogIgnoredTags(ignoredTags)
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// FindStashBoxScenesByFingerprints queries stash-box for a scene using the
|
||||
// scene's MD5/OSHASH checksum, or PHash.
|
||||
func (c Client) FindSceneByFingerprints(ctx context.Context, fps models.Fingerprints) ([]*models.ScrapedScene, error) {
|
||||
res, err := c.FindScenesByFingerprints(ctx, []models.Fingerprints{fps})
|
||||
if len(res) > 0 {
|
||||
return res[0], err
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// FindScenesByFingerprints queries stash-box for scenes using every
|
||||
// scene's MD5/OSHASH checksum, or PHash, and returns results in the same order
|
||||
// as the input slice.
|
||||
func (c Client) FindScenesByFingerprints(ctx context.Context, fps []models.Fingerprints) ([][]*models.ScrapedScene, error) {
|
||||
var fingerprints [][]*graphql.FingerprintQueryInput
|
||||
|
||||
for _, fp := range fps {
|
||||
fingerprints = append(fingerprints, convertFingerprints(fp))
|
||||
}
|
||||
|
||||
return c.findScenesByFingerprints(ctx, fingerprints)
|
||||
}
|
||||
|
||||
func convertFingerprints(fps models.Fingerprints) []*graphql.FingerprintQueryInput {
|
||||
var ret []*graphql.FingerprintQueryInput
|
||||
|
||||
for _, f := range fps {
|
||||
var i = &graphql.FingerprintQueryInput{}
|
||||
switch f.Type {
|
||||
case models.FingerprintTypeMD5:
|
||||
i.Algorithm = graphql.FingerprintAlgorithmMd5
|
||||
i.Hash = f.String()
|
||||
case models.FingerprintTypeOshash:
|
||||
i.Algorithm = graphql.FingerprintAlgorithmOshash
|
||||
i.Hash = f.String()
|
||||
case models.FingerprintTypePhash:
|
||||
i.Algorithm = graphql.FingerprintAlgorithmPhash
|
||||
i.Hash = utils.PhashToString(f.Int64())
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if !i.Algorithm.IsValid() {
|
||||
continue
|
||||
}
|
||||
|
||||
ret = append(ret, i)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c Client) findScenesByFingerprints(ctx context.Context, scenes [][]*graphql.FingerprintQueryInput) ([][]*models.ScrapedScene, error) {
|
||||
var results [][]*models.ScrapedScene
|
||||
|
||||
// filter out nils
|
||||
var validScenes [][]*graphql.FingerprintQueryInput
|
||||
for _, s := range scenes {
|
||||
if len(s) > 0 {
|
||||
validScenes = append(validScenes, s)
|
||||
}
|
||||
}
|
||||
|
||||
var ignoredTags []string
|
||||
|
||||
for i := 0; i < len(validScenes); i += 40 {
|
||||
end := i + 40
|
||||
if end > len(validScenes) {
|
||||
end = len(validScenes)
|
||||
}
|
||||
scenes, err := c.client.FindScenesBySceneFingerprints(ctx, validScenes[i:end])
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, sceneFragments := range scenes.FindScenesBySceneFingerprints {
|
||||
var sceneResults []*models.ScrapedScene
|
||||
for _, scene := range sceneFragments {
|
||||
ss, err := c.sceneFragmentToScrapedScene(ctx, scene)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var thisIgnoredTags []string
|
||||
ss.Tags, thisIgnoredTags = scraper.FilterTags(c.excludeTagRE, ss.Tags)
|
||||
ignoredTags = sliceutil.AppendUniques(ignoredTags, thisIgnoredTags)
|
||||
|
||||
sceneResults = append(sceneResults, ss)
|
||||
}
|
||||
results = append(results, sceneResults)
|
||||
}
|
||||
}
|
||||
|
||||
scraper.LogIgnoredTags(ignoredTags)
|
||||
|
||||
// repopulate the results to be the same order as the input
|
||||
ret := make([][]*models.ScrapedScene, len(scenes))
|
||||
upTo := 0
|
||||
|
||||
for i, v := range scenes {
|
||||
if len(v) > 0 {
|
||||
ret[i] = results[upTo]
|
||||
upTo++
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (c Client) sceneFragmentToScrapedScene(ctx context.Context, s *graphql.SceneFragment) (*models.ScrapedScene, error) {
|
||||
stashID := s.ID
|
||||
|
||||
ss := &models.ScrapedScene{
|
||||
Title: s.Title,
|
||||
Code: s.Code,
|
||||
Date: s.Date,
|
||||
Details: s.Details,
|
||||
Director: s.Director,
|
||||
URL: findURL(s.Urls, "STUDIO"),
|
||||
Duration: s.Duration,
|
||||
RemoteSiteID: &stashID,
|
||||
Fingerprints: getFingerprints(s),
|
||||
// Image
|
||||
// stash_id
|
||||
}
|
||||
|
||||
for _, u := range s.Urls {
|
||||
ss.URLs = append(ss.URLs, u.URL)
|
||||
}
|
||||
|
||||
if len(ss.URLs) > 0 {
|
||||
ss.URL = &ss.URLs[0]
|
||||
}
|
||||
|
||||
if len(s.Images) > 0 {
|
||||
// TODO - #454 code sorts images by aspect ratio according to a wanted
|
||||
// orientation. I'm just grabbing the first for now
|
||||
ss.Image = getFirstImage(ctx, c.getHTTPClient(), s.Images)
|
||||
}
|
||||
|
||||
ss.URLs = make([]string, len(s.Urls))
|
||||
for i, u := range s.Urls {
|
||||
ss.URLs[i] = u.URL
|
||||
}
|
||||
|
||||
if s.Studio != nil {
|
||||
var err error
|
||||
ss.Studio, err = c.resolveStudio(ctx, s.Studio)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, p := range s.Performers {
|
||||
sp := performerFragmentToScrapedPerformer(*p.Performer)
|
||||
ss.Performers = append(ss.Performers, sp)
|
||||
}
|
||||
|
||||
for _, t := range s.Tags {
|
||||
st := &models.ScrapedTag{
|
||||
Name: t.Name,
|
||||
}
|
||||
ss.Tags = append(ss.Tags, st)
|
||||
}
|
||||
|
||||
return ss, nil
|
||||
}
|
||||
|
||||
func getFirstImage(ctx context.Context, client *http.Client, images []*graphql.ImageFragment) *string {
|
||||
ret, err := fetchImage(ctx, client, images[0].URL)
|
||||
if err != nil && !errors.Is(err, context.Canceled) {
|
||||
logger.Warnf("Error fetching image %s: %s", images[0].URL, err.Error())
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func getFingerprints(scene *graphql.SceneFragment) []*models.StashBoxFingerprint {
|
||||
fingerprints := []*models.StashBoxFingerprint{}
|
||||
for _, fp := range scene.Fingerprints {
|
||||
fingerprint := models.StashBoxFingerprint{
|
||||
Algorithm: fp.Algorithm.String(),
|
||||
Hash: fp.Hash,
|
||||
Duration: fp.Duration,
|
||||
}
|
||||
fingerprints = append(fingerprints, &fingerprint)
|
||||
}
|
||||
return fingerprints
|
||||
}
|
||||
|
||||
type SceneDraft struct {
|
||||
// Files, URLs, StashIDs must be loaded
|
||||
Scene *models.Scene
|
||||
// StashIDs must be loaded
|
||||
Performers []*models.Performer
|
||||
// StashIDs must be loaded
|
||||
Studio *models.Studio
|
||||
Tags []*models.Tag
|
||||
Cover []byte
|
||||
}
|
||||
|
||||
func (c Client) SubmitSceneDraft(ctx context.Context, d SceneDraft) (*string, error) {
|
||||
draft := newSceneDraftInput(d, c.box.Endpoint)
|
||||
var image io.Reader
|
||||
|
||||
if len(d.Cover) > 0 {
|
||||
image = bytes.NewReader(d.Cover)
|
||||
}
|
||||
|
||||
var id *string
|
||||
var ret graphql.SubmitSceneDraft
|
||||
err := c.submitDraft(ctx, graphql.SubmitSceneDraftDocument, draft, image, &ret)
|
||||
id = ret.SubmitSceneDraft.ID
|
||||
|
||||
return id, err
|
||||
|
||||
// ret, err := c.client.SubmitSceneDraft(ctx, draft, uploadImage(image))
|
||||
// if err != nil {
|
||||
// return nil, err
|
||||
// }
|
||||
|
||||
// id := ret.SubmitSceneDraft.ID
|
||||
// return id, nil
|
||||
}
|
||||
|
||||
func newSceneDraftInput(d SceneDraft, endpoint string) graphql.SceneDraftInput {
|
||||
scene := d.Scene
|
||||
|
||||
draft := graphql.SceneDraftInput{}
|
||||
|
||||
if scene.Title != "" {
|
||||
draft.Title = &scene.Title
|
||||
}
|
||||
if scene.Code != "" {
|
||||
draft.Code = &scene.Code
|
||||
}
|
||||
if scene.Details != "" {
|
||||
draft.Details = &scene.Details
|
||||
}
|
||||
if scene.Director != "" {
|
||||
draft.Director = &scene.Director
|
||||
}
|
||||
// TODO - draft does not accept multiple URLs. Use single URL for now.
|
||||
if len(scene.URLs.List()) > 0 {
|
||||
url := strings.TrimSpace(scene.URLs.List()[0])
|
||||
draft.URL = &url
|
||||
}
|
||||
if scene.Date != nil {
|
||||
v := scene.Date.String()
|
||||
draft.Date = &v
|
||||
}
|
||||
|
||||
if d.Studio != nil {
|
||||
studio := d.Studio
|
||||
|
||||
studioDraft := graphql.DraftEntityInput{
|
||||
Name: studio.Name,
|
||||
}
|
||||
|
||||
stashIDs := studio.StashIDs.List()
|
||||
for _, stashID := range stashIDs {
|
||||
c := stashID
|
||||
if stashID.Endpoint == endpoint {
|
||||
studioDraft.ID = &c.StashID
|
||||
break
|
||||
}
|
||||
}
|
||||
draft.Studio = &studioDraft
|
||||
}
|
||||
|
||||
fingerprints := []*graphql.FingerprintInput{}
|
||||
|
||||
for _, f := range scene.Files.List() {
|
||||
duration := f.Duration
|
||||
|
||||
if duration != 0 {
|
||||
fingerprints = appendFingerprintsUnique(fingerprints, fileFingerprintsToInputGraphQL(f.Fingerprints, int(duration))...)
|
||||
}
|
||||
}
|
||||
draft.Fingerprints = fingerprints
|
||||
|
||||
scenePerformers := d.Performers
|
||||
|
||||
inputPerformers := []*graphql.DraftEntityInput{}
|
||||
for _, p := range scenePerformers {
|
||||
performerDraft := graphql.DraftEntityInput{
|
||||
Name: p.Name,
|
||||
}
|
||||
|
||||
stashIDs := p.StashIDs.List()
|
||||
for _, stashID := range stashIDs {
|
||||
c := stashID
|
||||
if stashID.Endpoint == endpoint {
|
||||
performerDraft.ID = &c.StashID
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
inputPerformers = append(inputPerformers, &performerDraft)
|
||||
}
|
||||
draft.Performers = inputPerformers
|
||||
|
||||
var tags []*graphql.DraftEntityInput
|
||||
sceneTags := d.Tags
|
||||
for _, tag := range sceneTags {
|
||||
tags = append(tags, &graphql.DraftEntityInput{Name: tag.Name})
|
||||
}
|
||||
draft.Tags = tags
|
||||
|
||||
stashIDs := scene.StashIDs.List()
|
||||
var stashID *string
|
||||
for _, v := range stashIDs {
|
||||
if v.Endpoint == endpoint {
|
||||
vv := v.StashID
|
||||
stashID = &vv
|
||||
break
|
||||
}
|
||||
}
|
||||
draft.ID = stashID
|
||||
|
||||
return draft
|
||||
}
|
||||
|
||||
func fileFingerprintsToInputGraphQL(fps models.Fingerprints, duration int) []*graphql.FingerprintInput {
|
||||
var ret []*graphql.FingerprintInput
|
||||
|
||||
for _, f := range fps {
|
||||
var i = &graphql.FingerprintInput{
|
||||
Duration: duration,
|
||||
}
|
||||
switch f.Type {
|
||||
case models.FingerprintTypeMD5:
|
||||
i.Algorithm = graphql.FingerprintAlgorithmMd5
|
||||
i.Hash = f.String()
|
||||
case models.FingerprintTypeOshash:
|
||||
i.Algorithm = graphql.FingerprintAlgorithmOshash
|
||||
i.Hash = f.String()
|
||||
case models.FingerprintTypePhash:
|
||||
i.Algorithm = graphql.FingerprintAlgorithmPhash
|
||||
i.Hash = utils.PhashToString(f.Int64())
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if !i.Algorithm.IsValid() {
|
||||
continue
|
||||
}
|
||||
|
||||
ret = appendFingerprintUnique(ret, i)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (c Client) SubmitFingerprints(ctx context.Context, scenes []*models.Scene) (bool, error) {
|
||||
endpoint := c.box.Endpoint
|
||||
|
||||
var fingerprints []graphql.FingerprintSubmission
|
||||
|
||||
for _, scene := range scenes {
|
||||
stashIDs := scene.StashIDs.List()
|
||||
sceneStashID := ""
|
||||
for _, stashID := range stashIDs {
|
||||
if stashID.Endpoint == endpoint {
|
||||
sceneStashID = stashID.StashID
|
||||
}
|
||||
}
|
||||
|
||||
if sceneStashID == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
for _, f := range scene.Files.List() {
|
||||
duration := f.Duration
|
||||
|
||||
if duration == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
fps := fileFingerprintsToInputGraphQL(f.Fingerprints, int(duration))
|
||||
for _, fp := range fps {
|
||||
fingerprints = append(fingerprints, graphql.FingerprintSubmission{
|
||||
SceneID: sceneStashID,
|
||||
Fingerprint: fp,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return c.submitFingerprints(ctx, fingerprints)
|
||||
}
|
||||
|
||||
func (c Client) submitFingerprints(ctx context.Context, fingerprints []graphql.FingerprintSubmission) (bool, error) {
|
||||
for _, fingerprint := range fingerprints {
|
||||
_, err := c.client.SubmitFingerprint(ctx, fingerprint)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func appendFingerprintUnique(v []*graphql.FingerprintInput, toAdd *graphql.FingerprintInput) []*graphql.FingerprintInput {
|
||||
for _, vv := range v {
|
||||
if vv.Algorithm == toAdd.Algorithm && vv.Hash == toAdd.Hash {
|
||||
return v
|
||||
}
|
||||
}
|
||||
|
||||
return append(v, toAdd)
|
||||
}
|
||||
|
||||
func appendFingerprintsUnique(v []*graphql.FingerprintInput, toAdd ...*graphql.FingerprintInput) []*graphql.FingerprintInput {
|
||||
for _, a := range toAdd {
|
||||
v = appendFingerprintUnique(v, a)
|
||||
}
|
||||
|
||||
return v
|
||||
}
|
||||
78
pkg/stashbox/studio.go
Normal file
78
pkg/stashbox/studio.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package stashbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stashapp/stash/pkg/models"
|
||||
"github.com/stashapp/stash/pkg/stashbox/graphql"
|
||||
)
|
||||
|
||||
func (c Client) resolveStudio(ctx context.Context, s *graphql.StudioFragment) (*models.ScrapedStudio, error) {
|
||||
scraped := studioFragmentToScrapedStudio(*s)
|
||||
|
||||
if s.Parent != nil {
|
||||
parentStudio, err := c.client.FindStudio(ctx, &s.Parent.ID, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if parentStudio.FindStudio == nil {
|
||||
return scraped, nil
|
||||
}
|
||||
|
||||
scraped.Parent, err = c.resolveStudio(ctx, parentStudio.FindStudio)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return scraped, nil
|
||||
}
|
||||
|
||||
func (c Client) FindStudio(ctx context.Context, query string) (*models.ScrapedStudio, error) {
|
||||
var studio *graphql.FindStudio
|
||||
|
||||
_, err := uuid.Parse(query)
|
||||
if err == nil {
|
||||
// Confirmed the user passed in a Stash ID
|
||||
studio, err = c.client.FindStudio(ctx, &query, nil)
|
||||
} else {
|
||||
// Otherwise assume they're searching on a name
|
||||
studio, err = c.client.FindStudio(ctx, nil, &query)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var ret *models.ScrapedStudio
|
||||
if studio.FindStudio != nil {
|
||||
ret, err = c.resolveStudio(ctx, studio.FindStudio)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func studioFragmentToScrapedStudio(s graphql.StudioFragment) *models.ScrapedStudio {
|
||||
images := []string{}
|
||||
for _, image := range s.Images {
|
||||
images = append(images, image.URL)
|
||||
}
|
||||
|
||||
st := &models.ScrapedStudio{
|
||||
Name: s.Name,
|
||||
URL: findURL(s.Urls, "HOME"),
|
||||
Images: images,
|
||||
RemoteSiteID: &s.ID,
|
||||
}
|
||||
|
||||
if len(st.Images) > 0 {
|
||||
st.Image = &st.Images[0]
|
||||
}
|
||||
|
||||
return st
|
||||
}
|
||||
Reference in New Issue
Block a user