mirror of
https://github.com/stashapp/stash.git
synced 2025-12-10 02:15:30 +01:00
* Add Cookies directly to the request Rather than maintaining a cookie jar on a one-shot HTTP client, maintain the jar ourselves: make a new jar, then use it to select the right cookies. The cookies are set on the request rather than on the client. This will retain the current behavior as we are always throwing the client away after each use. This patch enables the lifting of the http client as well over time. * Introduce a cached scraper HTTP client The scraper cache is augmented with an *http.Client. These are safe for concurrent use, so the pointer can safely be passed around. Push this into scraper configurations where applicable, next to the txnManagers. When we issue a loadUrl request, do so on the cached *http.Client, which will reuse existing idle connections in the client if any are present. * Set MaxIdleConnsPerHost. Closes #1850 We allow for up to 8 idle connections to a single host. This should make concurrent operation toward the same host reuse connections, even for sizeable concurrency. The number isn't bumped excessively high. We should probably limit concurrency toward a single site anyway, since we'll be able to overrun a site with queries quite easily if we have many concurrent goroutines issuing requests at the same time. * Reinstate driverOptions / useCDP check Use DeMorgan's laws to invert the logic and exit early. Fixes tests breaking. * Documentation fixup. * Use the scraper http.Client when fetching images Fold image fetchers onto the cached scraper http.Client as well. This makes the scraper have a single http.Client cache for all its operations. Thread the client upwards to the relevant attachment points: either the cache, or a stash_box instance, which is extended to include a pointer to the client. Style roughly follows that of txnManagers. * Use the same http Client as the GraphQL client use Rather than using http.DefaultClient, use the same client as the GraphQL client use in the stash_box subsystem. This localizes the client used in the subsystem into the constructing New.. call. * Hoist HTTP client construction Create a function for initializaing the HTTP Client we use. While here hoist magic numbers into constants. Introduce a proper static redirect error and use it in the client code as well. * Reinstate printCookies This is a debugging function, and it might still come in handy in the future at some point. * Nitpick comment. * Minor tidy Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com>
313 lines
7.7 KiB
Go
313 lines
7.7 KiB
Go
package scraper
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"net/url"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/antchfx/htmlquery"
|
|
|
|
"golang.org/x/net/html"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
"github.com/stashapp/stash/pkg/models"
|
|
)
|
|
|
|
type xpathScraper struct {
|
|
scraper scraperTypeConfig
|
|
config config
|
|
globalConfig GlobalConfig
|
|
client *http.Client
|
|
txnManager models.TransactionManager
|
|
}
|
|
|
|
func newXpathScraper(scraper scraperTypeConfig, client *http.Client, txnManager models.TransactionManager, config config, globalConfig GlobalConfig) *xpathScraper {
|
|
return &xpathScraper{
|
|
scraper: scraper,
|
|
config: config,
|
|
globalConfig: globalConfig,
|
|
client: client,
|
|
txnManager: txnManager,
|
|
}
|
|
}
|
|
|
|
func (s *xpathScraper) getXpathScraper() *mappedScraper {
|
|
return s.config.XPathScrapers[s.scraper.Scraper]
|
|
}
|
|
|
|
func (s *xpathScraper) scrapeURL(url string) (*html.Node, *mappedScraper, error) {
|
|
scraper := s.getXpathScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, 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, nil, err
|
|
}
|
|
|
|
return doc, scraper, nil
|
|
}
|
|
|
|
func (s *xpathScraper) 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(u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getXPathQuery(doc)
|
|
return scraper.scrapePerformer(q)
|
|
}
|
|
|
|
func (s *xpathScraper) 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(u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getXPathQuery(doc)
|
|
return scraper.scrapeScene(q)
|
|
}
|
|
|
|
func (s *xpathScraper) 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(u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getXPathQuery(doc)
|
|
return scraper.scrapeGallery(q)
|
|
}
|
|
|
|
func (s *xpathScraper) 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(u)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getXPathQuery(doc)
|
|
return scraper.scrapeMovie(q)
|
|
}
|
|
|
|
func (s *xpathScraper) scrapePerformersByName(name string) ([]*models.ScrapedPerformer, error) {
|
|
scraper := s.getXpathScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, errors.New("xpath 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.ReplaceAll(url, placeholder, escapedName)
|
|
|
|
doc, err := s.loadURL(context.TODO(), url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getXPathQuery(doc)
|
|
return scraper.scrapePerformers(q)
|
|
}
|
|
|
|
func (s *xpathScraper) scrapePerformerByFragment(scrapedPerformer models.ScrapedPerformerInput) (*models.ScrapedPerformer, error) {
|
|
return nil, errors.New("scrapePerformerByFragment not supported for xpath scraper")
|
|
}
|
|
|
|
func (s *xpathScraper) scrapeScenesByName(name string) ([]*models.ScrapedScene, error) {
|
|
scraper := s.getXpathScraper()
|
|
|
|
if scraper == nil {
|
|
return nil, errors.New("xpath 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.ReplaceAll(url, placeholder, escapedName)
|
|
|
|
doc, err := s.loadURL(context.TODO(), url)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
q := s.getXPathQuery(doc)
|
|
return scraper.scrapeScenes(q)
|
|
}
|
|
|
|
func (s *xpathScraper) 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.getXpathScraper()
|
|
|
|
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.getXPathQuery(doc)
|
|
return scraper.scrapeScene(q)
|
|
}
|
|
|
|
func (s *xpathScraper) 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.getXpathScraper()
|
|
|
|
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.getXPathQuery(doc)
|
|
return scraper.scrapeScene(q)
|
|
}
|
|
|
|
func (s *xpathScraper) 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.getXpathScraper()
|
|
|
|
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.getXPathQuery(doc)
|
|
return scraper.scrapeGallery(q)
|
|
}
|
|
|
|
func (s *xpathScraper) scrapeGalleryByFragment(gallery models.ScrapedGalleryInput) (*models.ScrapedGallery, error) {
|
|
return nil, errors.New("scrapeGalleryByFragment not supported for xpath scraper")
|
|
}
|
|
|
|
func (s *xpathScraper) loadURL(ctx context.Context, url string) (*html.Node, error) {
|
|
r, err := loadURL(ctx, url, s.client, s.config, s.globalConfig)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ret, err := html.Parse(r)
|
|
|
|
if err == nil && s.config.DebugOptions != nil && s.config.DebugOptions.PrintHTML {
|
|
var b bytes.Buffer
|
|
if err := html.Render(&b, ret); err != nil {
|
|
logger.Warnf("could not render HTML: %v", err)
|
|
}
|
|
logger.Infof("loadURL (%s) response: \n%s", url, b.String())
|
|
}
|
|
|
|
return ret, err
|
|
}
|
|
|
|
func (s *xpathScraper) getXPathQuery(doc *html.Node) *xpathQuery {
|
|
return &xpathQuery{
|
|
doc: doc,
|
|
scraper: s,
|
|
}
|
|
}
|
|
|
|
type xpathQuery struct {
|
|
doc *html.Node
|
|
scraper *xpathScraper
|
|
}
|
|
|
|
func (q *xpathQuery) runQuery(selector string) []string {
|
|
found, err := htmlquery.QueryAll(q.doc, selector)
|
|
if err != nil {
|
|
logger.Warnf("Error parsing xpath expression '%s': %s", selector, err.Error())
|
|
return nil
|
|
}
|
|
|
|
var ret []string
|
|
for _, n := range found {
|
|
// don't add empty strings
|
|
nodeText := q.nodeText(n)
|
|
if nodeText != "" {
|
|
ret = append(ret, q.nodeText(n))
|
|
}
|
|
}
|
|
|
|
return ret
|
|
}
|
|
|
|
func (q *xpathQuery) nodeText(n *html.Node) string {
|
|
var ret string
|
|
if n != nil && n.Type == html.CommentNode {
|
|
ret = htmlquery.OutputHTML(n, true)
|
|
} else {
|
|
ret = htmlquery.InnerText(n)
|
|
}
|
|
|
|
// trim all leading and trailing whitespace
|
|
ret = strings.TrimSpace(ret)
|
|
|
|
// remove multiple whitespace
|
|
re := regexp.MustCompile(" +")
|
|
ret = re.ReplaceAllString(ret, " ")
|
|
|
|
// TODO - make this optional
|
|
re = regexp.MustCompile("\n")
|
|
ret = re.ReplaceAllString(ret, "")
|
|
|
|
return ret
|
|
}
|
|
|
|
func (q *xpathQuery) 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.getXPathQuery(doc)
|
|
}
|