forked from stashapp/stash
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stash scraper type (stashapp#269)
* Add stash scraper type * Add graphql client to vendor * Embed stash credentials in URL * Fill URL from scraped scene * Nil IDs returned from remote stash * Nil check
- Loading branch information
1 parent
0870d9b
commit 282dfc5
Showing
21 changed files
with
1,526 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package scraper | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/shurcooL/graphql" | ||
|
||
"github.com/stashapp/stash/pkg/models" | ||
) | ||
|
||
func getStashClient(c scraperTypeConfig) *graphql.Client { | ||
url := c.scraperConfig.StashServer.URL | ||
return graphql.NewClient(url+"/graphql", nil) | ||
} | ||
|
||
type stashFindPerformerNamePerformer struct { | ||
ID string `json:"id" graphql:"id"` | ||
Name string `json:"id" graphql:"name"` | ||
} | ||
|
||
func (p stashFindPerformerNamePerformer) toPerformer() *models.ScrapedPerformer { | ||
return &models.ScrapedPerformer{ | ||
Name: &p.Name, | ||
// put id into the URL field | ||
URL: &p.ID, | ||
} | ||
} | ||
|
||
type stashFindPerformerNamesResultType struct { | ||
Count int `graphql:"count"` | ||
Performers []*stashFindPerformerNamePerformer `graphql:"performers"` | ||
} | ||
|
||
func scrapePerformerNamesStash(c scraperTypeConfig, name string) ([]*models.ScrapedPerformer, error) { | ||
client := getStashClient(c) | ||
|
||
var q struct { | ||
FindPerformers stashFindPerformerNamesResultType `graphql:"findPerformers(filter: $f)"` | ||
} | ||
|
||
page := 1 | ||
perPage := 10 | ||
|
||
vars := map[string]interface{}{ | ||
"f": models.FindFilterType{ | ||
Q: &name, | ||
Page: &page, | ||
PerPage: &perPage, | ||
}, | ||
} | ||
|
||
err := client.Query(context.Background(), &q, vars) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var ret []*models.ScrapedPerformer | ||
for _, p := range q.FindPerformers.Performers { | ||
ret = append(ret, p.toPerformer()) | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
func scrapePerformerFragmentStash(c scraperTypeConfig, scrapedPerformer models.ScrapedPerformerInput) (*models.ScrapedPerformer, error) { | ||
client := getStashClient(c) | ||
|
||
var q struct { | ||
FindPerformer *models.ScrapedPerformer `graphql:"findPerformer(id: $f)"` | ||
} | ||
|
||
// get the id from the URL field | ||
vars := map[string]interface{}{ | ||
"f": *scrapedPerformer.URL, | ||
} | ||
|
||
err := client.Query(context.Background(), &q, vars) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return q.FindPerformer, nil | ||
} | ||
|
||
func scrapeSceneFragmentStash(c scraperTypeConfig, scene models.SceneUpdateInput) (*models.ScrapedScene, error) { | ||
// query by MD5 | ||
// assumes that the scene exists in the database | ||
qb := models.NewSceneQueryBuilder() | ||
id, err := strconv.Atoi(scene.ID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
storedScene, err := qb.Find(id) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var q struct { | ||
FindScene *models.ScrapedScene `graphql:"findScene(checksum: $c)"` | ||
} | ||
|
||
checksum := graphql.String(storedScene.Checksum) | ||
vars := map[string]interface{}{ | ||
"c": &checksum, | ||
} | ||
|
||
client := getStashClient(c) | ||
err = client.Query(context.Background(), &q, vars) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if q.FindScene != nil { | ||
// the ids of the studio, performers and tags must be nilled | ||
if q.FindScene.Studio != nil { | ||
q.FindScene.Studio.ID = nil | ||
} | ||
|
||
for _, p := range q.FindScene.Performers { | ||
p.ID = nil | ||
} | ||
|
||
for _, t := range q.FindScene.Tags { | ||
t.ID = nil | ||
} | ||
} | ||
|
||
return q.FindScene, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.