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.
49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
package scraper
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
)
|
|
|
|
type scraperAction string
|
|
|
|
const (
|
|
scraperActionScript scraperAction = "script"
|
|
scraperActionStash scraperAction = "stash"
|
|
scraperActionXPath scraperAction = "scrapeXPath"
|
|
scraperActionJson scraperAction = "scrapeJson"
|
|
)
|
|
|
|
func (e scraperAction) IsValid() bool {
|
|
switch e {
|
|
case scraperActionScript, scraperActionStash, scraperActionXPath, scraperActionJson:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
type scraperActionImpl interface {
|
|
scrapeByURL(ctx context.Context, url string, ty models.ScrapeContentType) (models.ScrapedContent, error)
|
|
scrapeByName(ctx context.Context, name string, ty models.ScrapeContentType) ([]models.ScrapedContent, error)
|
|
scrapeByFragment(ctx context.Context, input Input) (models.ScrapedContent, error)
|
|
|
|
scrapeSceneByScene(ctx context.Context, scene *models.Scene) (*models.ScrapedScene, error)
|
|
scrapeGalleryByGallery(ctx context.Context, gallery *models.Gallery) (*models.ScrapedGallery, error)
|
|
}
|
|
|
|
func (c config) getScraper(scraper scraperTypeConfig, client *http.Client, txnManager models.TransactionManager, globalConfig GlobalConfig) scraperActionImpl {
|
|
switch scraper.Action {
|
|
case scraperActionScript:
|
|
return newScriptScraper(scraper, c, globalConfig)
|
|
case scraperActionStash:
|
|
return newStashScraper(scraper, client, txnManager, c, globalConfig)
|
|
case scraperActionXPath:
|
|
return newXpathScraper(scraper, client, txnManager, c, globalConfig)
|
|
case scraperActionJson:
|
|
return newJsonScraper(scraper, client, txnManager, c, globalConfig)
|
|
}
|
|
|
|
panic("unknown scraper action: " + scraper.Action)
|
|
}
|