mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 16:34:02 +01:00
* Use the request context
The code uses context.Background() in a flow where there is a
http.Request. Use the requests context instead.
* Use a true context in the plugin example
Let AddTag/RemoveTag take a context and use that context throughout
the example.
* Avoid the use of context.Background
Prefer context.TODO over context.Background deep in the call chain.
This marks the site as something which we need to context-handle
later, and also makes it clear to the reader that the context is
sort-of temporary in the code base.
While here, be consistent in handling the `act` variable in each
branch of the if .. { .. } .. check.
* Prefer context.TODO over context.Background
For the different scraping operations here, there is a context
higher up the call chain, which we ought to use. Mark the call-sites
as TODO for now, so we can come back later on a sweep of which parts
can be context-lifted.
* Thread context upwards
Initialization requires context for transactions. Thread the context
upward the call chain.
At the intialization call, add a context.TODO since we can't break this
yet. The singleton assumption prevents us from pulling it up into main for
now.
* make tasks context-aware
Change the task interface to understand contexts.
Pass the context down in some of the branches where it is needed.
* Make QueryStashBoxScene context-aware
This call naturally sits inside the request-context. Use it.
* Introduce a context in the JS plugin code
This allows us to use a context for HTTP calls inside the system.
Mark the context with a TODO at top level for now.
* Nitpick error formatting
Use %v rather than %s for error interfaces.
Do not begin an error strong with a capital letter.
* Avoid the use of http.Get in FFMPEG download chain
Since http.Get has no context, it isn't possible to break out or have
policy induced. The call will block until the GET completes. Rewrite
to use a http Request and provide a context.
Thread the context through the call chain for now. provide
context.TODO() at the top level of the initialization chain.
* Make getRemoteCDPWSAddress aware of contexts
Eliminate a call to http.Get and replace it with a context-aware
variant.
Push the context upwards in the call chain, but plug it before the
scraper interface so we don't have to rewrite said interface yet.
Plugged with context.TODO()
* Scraper: make the getImage function context-aware
Use a context, and pass it upwards. Plug it with context.TODO()
up the chain before the rewrite gets too much out of hand for now.
Minor tweaks along the way, remove a call to context.Background()
deep in the call chain.
* Make NOTIFY request context-aware
The call sits inside a Request-handler. So it's natural to use the
requests context as the context for the outgoing HTTP request.
* Use a context in the url scraper code
We are sitting in code which has a context, so utilize it for the
request as well.
* Use a context when checking versions
When we check the version of stash on Github, use a context. Thread
the context up to the initialization routine of the HTTP/GraphQL
server and plug it with a context.TODO() for now.
This paves the way for providing a context to the HTTP server code in a
future patch.
* Make utils func ReadImage context-aware
In almost all of the cases, there is a context in the call chain which
is a natural use. This is true for all the GraphQL mutations.
The exception is in task_stash_box_tag, so plug that task with
context.TODO() for now.
* Make stash-box get context-aware
Thread a context through the call chain until we hit the Client API.
Plug it with context.TODO() there for now.
* Enable the noctx linter
The code is now free of any uncontexted HTTP request. This means we
pass the noctx linter, and we can enable it in the code base.
290 lines
7.1 KiB
Go
290 lines
7.1 KiB
Go
package scraper
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
"github.com/stashapp/stash/pkg/models"
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
type jsonScraper struct {
|
|
scraper scraperTypeConfig
|
|
config config
|
|
globalConfig GlobalConfig
|
|
txnManager models.TransactionManager
|
|
}
|
|
|
|
func newJsonScraper(scraper scraperTypeConfig, txnManager models.TransactionManager, config config, globalConfig GlobalConfig) *jsonScraper {
|
|
return &jsonScraper{
|
|
scraper: scraper,
|
|
config: config,
|
|
globalConfig: globalConfig,
|
|
txnManager: txnManager,
|
|
}
|
|
}
|
|
|
|
func (s *jsonScraper) getJsonScraper() *mappedScraper {
|
|
return s.config.JsonScrapers[s.scraper.Scraper]
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeURL(ctx context.Context, url string) (string, *mappedScraper, error) {
|
|
scraper := s.getJsonScraper()
|
|
|
|
if scraper == nil {
|
|
return "", nil, errors.New("json scraper with name " + s.scraper.Scraper + " not found in config")
|
|
}
|
|
|
|
doc, err := s.loadURL(ctx, url)
|
|
|
|
if err != nil {
|
|
return "", nil, err
|
|
}
|
|
|
|
return doc, scraper, nil
|
|
}
|
|
|
|
func (s *jsonScraper) loadURL(ctx context.Context, url string) (string, error) {
|
|
r, err := loadURL(ctx, url, s.config, s.globalConfig)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
logger.Infof("loadURL (%s)\n", url)
|
|
doc, err := io.ReadAll(r)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
docStr := string(doc)
|
|
if !gjson.Valid(docStr) {
|
|
return "", errors.New("not valid json")
|
|
}
|
|
|
|
if err == nil && s.config.DebugOptions != nil && s.config.DebugOptions.PrintHTML {
|
|
logger.Infof("loadURL (%s) response: \n%s", url, docStr)
|
|
}
|
|
|
|
return docStr, err
|
|
}
|
|
|
|
func (s *jsonScraper) scrapePerformerByURL(url string) (*models.ScrapedPerformer, error) {
|
|
u := replaceURL(url, s.scraper) // allow a URL Replace for performer by URL queries
|
|
doc, scraper, err := s.scrapeURL(context.TODO(), u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapePerformer(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeSceneByURL(url string) (*models.ScrapedScene, error) {
|
|
u := replaceURL(url, s.scraper) // allow a URL Replace for scene by URL queries
|
|
doc, scraper, err := s.scrapeURL(context.TODO(), u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapeScene(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeGalleryByURL(url string) (*models.ScrapedGallery, error) {
|
|
u := replaceURL(url, s.scraper) // allow a URL Replace for gallery by URL queries
|
|
doc, scraper, err := s.scrapeURL(context.TODO(), u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapeGallery(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeMovieByURL(url string) (*models.ScrapedMovie, error) {
|
|
u := replaceURL(url, s.scraper) // allow a URL Replace for movie by URL queries
|
|
doc, scraper, err := s.scrapeURL(context.TODO(), u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapeMovie(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapePerformersByName(name string) ([]*models.ScrapedPerformer, error) {
|
|
scraper := s.getJsonScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, errors.New("json scraper with name " + s.scraper.Scraper + " not found in config")
|
|
}
|
|
|
|
const placeholder = "{}"
|
|
|
|
// replace the placeholder string with the URL-escaped name
|
|
escapedName := url.QueryEscape(name)
|
|
|
|
url := s.scraper.QueryURL
|
|
url = strings.Replace(url, placeholder, escapedName, -1)
|
|
|
|
doc, err := s.loadURL(context.TODO(), url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapePerformers(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapePerformerByFragment(scrapedPerformer models.ScrapedPerformerInput) (*models.ScrapedPerformer, error) {
|
|
return nil, errors.New("scrapePerformerByFragment not supported for json scraper")
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeScenesByName(name string) ([]*models.ScrapedScene, error) {
|
|
scraper := s.getJsonScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, errors.New("json scraper with name " + s.scraper.Scraper + " not found in config")
|
|
}
|
|
|
|
const placeholder = "{}"
|
|
|
|
// replace the placeholder string with the URL-escaped name
|
|
escapedName := url.QueryEscape(name)
|
|
|
|
url := s.scraper.QueryURL
|
|
url = strings.Replace(url, placeholder, escapedName, -1)
|
|
|
|
doc, err := s.loadURL(context.TODO(), url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapeScenes(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeSceneByScene(scene *models.Scene) (*models.ScrapedScene, error) {
|
|
// construct the URL
|
|
queryURL := queryURLParametersFromScene(scene)
|
|
if s.scraper.QueryURLReplacements != nil {
|
|
queryURL.applyReplacements(s.scraper.QueryURLReplacements)
|
|
}
|
|
url := queryURL.constructURL(s.scraper.QueryURL)
|
|
|
|
scraper := s.getJsonScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, errors.New("json scraper with name " + s.scraper.Scraper + " not found in config")
|
|
}
|
|
|
|
doc, err := s.loadURL(context.TODO(), url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapeScene(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeSceneByFragment(scene models.ScrapedSceneInput) (*models.ScrapedScene, error) {
|
|
// construct the URL
|
|
queryURL := queryURLParametersFromScrapedScene(scene)
|
|
if s.scraper.QueryURLReplacements != nil {
|
|
queryURL.applyReplacements(s.scraper.QueryURLReplacements)
|
|
}
|
|
url := queryURL.constructURL(s.scraper.QueryURL)
|
|
|
|
scraper := s.getJsonScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, errors.New("xpath scraper with name " + s.scraper.Scraper + " not found in config")
|
|
}
|
|
|
|
doc, err := s.loadURL(context.TODO(), url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapeScene(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeGalleryByGallery(gallery *models.Gallery) (*models.ScrapedGallery, error) {
|
|
// construct the URL
|
|
queryURL := queryURLParametersFromGallery(gallery)
|
|
if s.scraper.QueryURLReplacements != nil {
|
|
queryURL.applyReplacements(s.scraper.QueryURLReplacements)
|
|
}
|
|
url := queryURL.constructURL(s.scraper.QueryURL)
|
|
|
|
scraper := s.getJsonScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, errors.New("json scraper with name " + s.scraper.Scraper + " not found in config")
|
|
}
|
|
|
|
doc, err := s.loadURL(context.TODO(), url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getJsonQuery(doc)
|
|
return scraper.scrapeGallery(q)
|
|
}
|
|
|
|
func (s *jsonScraper) scrapeGalleryByFragment(gallery models.ScrapedGalleryInput) (*models.ScrapedGallery, error) {
|
|
return nil, errors.New("scrapeGalleryByFragment not supported for json scraper")
|
|
}
|
|
|
|
func (s *jsonScraper) getJsonQuery(doc string) *jsonQuery {
|
|
return &jsonQuery{
|
|
doc: doc,
|
|
scraper: s,
|
|
}
|
|
}
|
|
|
|
type jsonQuery struct {
|
|
doc string
|
|
scraper *jsonScraper
|
|
}
|
|
|
|
func (q *jsonQuery) runQuery(selector string) []string {
|
|
value := gjson.Get(q.doc, selector)
|
|
|
|
if !value.Exists() {
|
|
logger.Warnf("Could not find json path '%s' in json object", selector)
|
|
return nil
|
|
}
|
|
|
|
var ret []string
|
|
if value.IsArray() {
|
|
value.ForEach(func(k, v gjson.Result) bool {
|
|
ret = append(ret, v.String())
|
|
return true
|
|
})
|
|
} else {
|
|
ret = append(ret, value.String())
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func (q *jsonQuery) subScrape(value string) mappedQuery {
|
|
doc, err := q.scraper.loadURL(context.TODO(), value)
|
|
|
|
if err != nil {
|
|
logger.Warnf("Error getting URL '%s' for sub-scraper: %s", value, err.Error())
|
|
return nil
|
|
}
|
|
|
|
return q.scraper.getJsonQuery(doc)
|
|
}
|