mirror of
https://github.com/stashapp/stash.git
synced 2025-12-06 16:34:02 +01:00
* Push scrapeByURL into scrapers Replace ScrapePerfomerByURL, ScrapeMovie..., ... with ScrapeByURL in the scraperActionImpl interface. This allows us to delete a lot of repeated code in the scrapers and replace the central part with a switch on the scraper type. * Fold name scraping into one call Follow up on scraper refactoring. Name scrapers use the same code path. This allows us to restructure some code and kill some functions, adding variance to the name scraping code. It allows us to remove some code repetition as well. * Do not export loop refs. * Simplify fragment scraping Generalize fragment scrapers into ScrapeByFragment. This simplifies fragment code flows into a simpler pathing which should be easier to handle in the future. * Eliminate more context.TODO() In a number of cases, we have a context now. Use the context rather than TODO() for those cases in order to make those operations cancellable. * Pass the context for the stashbox scraper This removes all context.TODO() in the path of the stashbox scraper, and replaces it with the context that's present on each of the paths. * Pass the context into subscrapers Mostly a mechanical update, where we pass in the context for subscraping. This removes the final context.TODO() in the scraper code. * Warn on unknown fields from scripts A common mistake for new script writers are that they return fields not known to stash. For instance the name "description" is used rather than "details". Decode disallowing unknown fields. If this fails, use a tee-reader to fall back to the old behavior, but print a warning for the user in this case. Thus, we retain the old behavior, but print warnings for scripts which fails the more strict unknown-fields detection. * Nil-check before running the postprocessing chain Fixes panics when scraping returns nil values. * Lift nil-ness in post-postprocessing If the struct we are trying to post-process is nil, we shouldn't enter the postprocessing flow at all. Pass the struct as a value rather than a pointer, eliminating nil-checks as we go. Use the top-level postProcess call to make the nil-check and then abort there if the object we are looking at is nil. * Allow conversion routines to handle values If we have a non-pointer type in the interface, we should also convert those into ScrapedContent. Otherwise we get errors on deprecated functions.
141 lines
3.9 KiB
Go
141 lines
3.9 KiB
Go
package scraper
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
)
|
|
|
|
type group struct {
|
|
config config
|
|
|
|
txnManager models.TransactionManager
|
|
globalConf GlobalConfig
|
|
}
|
|
|
|
func newGroupScraper(c config, txnManager models.TransactionManager, globalConfig GlobalConfig) scraper {
|
|
return group{
|
|
config: c,
|
|
txnManager: txnManager,
|
|
globalConf: globalConfig,
|
|
}
|
|
}
|
|
|
|
func (g group) spec() models.Scraper {
|
|
return g.config.spec()
|
|
}
|
|
|
|
// fragmentScraper finds an appropriate fragment scraper based on input.
|
|
func (g group) fragmentScraper(input Input) *scraperTypeConfig {
|
|
switch {
|
|
case input.Performer != nil:
|
|
return g.config.PerformerByFragment
|
|
case input.Gallery != nil:
|
|
// TODO - this should be galleryByQueryFragment
|
|
return g.config.GalleryByFragment
|
|
case input.Scene != nil:
|
|
return g.config.SceneByQueryFragment
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (g group) viaFragment(ctx context.Context, client *http.Client, input Input) (models.ScrapedContent, error) {
|
|
stc := g.fragmentScraper(input)
|
|
if stc == nil {
|
|
// If there's no performer fragment scraper in the group, we try to use
|
|
// the URL scraper. Check if there's an URL in the input, and then shift
|
|
// to an URL scrape if it's present.
|
|
if input.Performer != nil && input.Performer.URL != nil && *input.Performer.URL != "" {
|
|
return g.viaURL(ctx, client, *input.Performer.URL, models.ScrapeContentTypePerformer)
|
|
}
|
|
|
|
return nil, ErrNotSupported
|
|
}
|
|
|
|
s := g.config.getScraper(*stc, client, g.txnManager, g.globalConf)
|
|
return s.scrapeByFragment(ctx, input)
|
|
}
|
|
|
|
func (g group) viaScene(ctx context.Context, client *http.Client, scene *models.Scene) (*models.ScrapedScene, error) {
|
|
if g.config.SceneByFragment == nil {
|
|
return nil, ErrNotSupported
|
|
}
|
|
|
|
s := g.config.getScraper(*g.config.SceneByFragment, client, g.txnManager, g.globalConf)
|
|
return s.scrapeSceneByScene(ctx, scene)
|
|
}
|
|
|
|
func (g group) viaGallery(ctx context.Context, client *http.Client, gallery *models.Gallery) (*models.ScrapedGallery, error) {
|
|
if g.config.GalleryByFragment == nil {
|
|
return nil, ErrNotSupported
|
|
}
|
|
|
|
s := g.config.getScraper(*g.config.GalleryByFragment, client, g.txnManager, g.globalConf)
|
|
return s.scrapeGalleryByGallery(ctx, gallery)
|
|
}
|
|
|
|
func loadUrlCandidates(c config, ty models.ScrapeContentType) []*scrapeByURLConfig {
|
|
switch ty {
|
|
case models.ScrapeContentTypePerformer:
|
|
return c.PerformerByURL
|
|
case models.ScrapeContentTypeScene:
|
|
return c.SceneByURL
|
|
case models.ScrapeContentTypeMovie:
|
|
return c.MovieByURL
|
|
case models.ScrapeContentTypeGallery:
|
|
return c.GalleryByURL
|
|
}
|
|
|
|
panic("loadUrlCandidates: unreachable")
|
|
}
|
|
|
|
func (g group) viaURL(ctx context.Context, client *http.Client, url string, ty models.ScrapeContentType) (models.ScrapedContent, error) {
|
|
candidates := loadUrlCandidates(g.config, ty)
|
|
for _, scraper := range candidates {
|
|
if scraper.matchesURL(url) {
|
|
s := g.config.getScraper(scraper.scraperTypeConfig, client, g.txnManager, g.globalConf)
|
|
ret, err := s.scrapeByURL(ctx, url, ty)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if ret != nil {
|
|
return ret, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (g group) viaName(ctx context.Context, client *http.Client, name string, ty models.ScrapeContentType) ([]models.ScrapedContent, error) {
|
|
switch ty {
|
|
case models.ScrapeContentTypePerformer:
|
|
if g.config.PerformerByName == nil {
|
|
break
|
|
}
|
|
|
|
s := g.config.getScraper(*g.config.PerformerByName, client, g.txnManager, g.globalConf)
|
|
return s.scrapeByName(ctx, name, ty)
|
|
case models.ScrapeContentTypeScene:
|
|
if g.config.SceneByName == nil {
|
|
break
|
|
}
|
|
|
|
s := g.config.getScraper(*g.config.SceneByName, client, g.txnManager, g.globalConf)
|
|
return s.scrapeByName(ctx, name, ty)
|
|
}
|
|
|
|
return nil, fmt.Errorf("%w: cannot load %v by name", ErrNotSupported, ty)
|
|
}
|
|
|
|
func (g group) supports(ty models.ScrapeContentType) bool {
|
|
return g.config.supports(ty)
|
|
}
|
|
|
|
func (g group) supportsURL(url string, ty models.ScrapeContentType) bool {
|
|
return g.config.matchesURL(url, ty)
|
|
}
|