mirror of
https://github.com/stashapp/stash.git
synced 2025-12-12 03:12:24 +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.
251 lines
6.1 KiB
Go
251 lines
6.1 KiB
Go
package manager
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
|
|
"github.com/stashapp/stash/pkg/identify"
|
|
"github.com/stashapp/stash/pkg/job"
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
"github.com/stashapp/stash/pkg/models"
|
|
"github.com/stashapp/stash/pkg/scene"
|
|
"github.com/stashapp/stash/pkg/scraper"
|
|
"github.com/stashapp/stash/pkg/scraper/stashbox"
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
)
|
|
|
|
var ErrInput = errors.New("invalid request input")
|
|
|
|
type IdentifyJob struct {
|
|
txnManager models.TransactionManager
|
|
postHookExecutor identify.SceneUpdatePostHookExecutor
|
|
input models.IdentifyMetadataInput
|
|
|
|
stashBoxes models.StashBoxes
|
|
progress *job.Progress
|
|
}
|
|
|
|
func CreateIdentifyJob(input models.IdentifyMetadataInput) *IdentifyJob {
|
|
return &IdentifyJob{
|
|
txnManager: instance.TxnManager,
|
|
postHookExecutor: instance.PluginCache,
|
|
input: input,
|
|
stashBoxes: instance.Config.GetStashBoxes(),
|
|
}
|
|
}
|
|
|
|
func (j *IdentifyJob) Execute(ctx context.Context, progress *job.Progress) {
|
|
j.progress = progress
|
|
|
|
// if no sources provided - just return
|
|
if len(j.input.Sources) == 0 {
|
|
return
|
|
}
|
|
|
|
sources, err := j.getSources()
|
|
if err != nil {
|
|
logger.Error(err)
|
|
return
|
|
}
|
|
|
|
// if scene ids provided, use those
|
|
// otherwise, batch query for all scenes - ordering by path
|
|
if err := j.txnManager.WithReadTxn(ctx, func(r models.ReaderRepository) error {
|
|
if len(j.input.SceneIDs) == 0 {
|
|
return j.identifyAllScenes(ctx, r, sources)
|
|
}
|
|
|
|
sceneIDs, err := utils.StringSliceToIntSlice(j.input.SceneIDs)
|
|
if err != nil {
|
|
return fmt.Errorf("invalid scene IDs: %w", err)
|
|
}
|
|
|
|
progress.SetTotal(len(sceneIDs))
|
|
for _, id := range sceneIDs {
|
|
if job.IsCancelled(ctx) {
|
|
break
|
|
}
|
|
|
|
// find the scene
|
|
var err error
|
|
scene, err := r.Scene().Find(id)
|
|
if err != nil {
|
|
return fmt.Errorf("error finding scene with id %d: %w", id, err)
|
|
}
|
|
|
|
if scene == nil {
|
|
return fmt.Errorf("%w: scene with id %d", models.ErrNotFound, id)
|
|
}
|
|
|
|
j.identifyScene(ctx, scene, sources)
|
|
}
|
|
|
|
return nil
|
|
}); err != nil {
|
|
logger.Errorf("Error encountered while identifying scenes: %v", err)
|
|
}
|
|
}
|
|
|
|
func (j *IdentifyJob) identifyAllScenes(ctx context.Context, r models.ReaderRepository, sources []identify.ScraperSource) error {
|
|
// exclude organised
|
|
organised := false
|
|
sceneFilter := scene.FilterFromPaths(j.input.Paths)
|
|
sceneFilter.Organized = &organised
|
|
|
|
sort := "path"
|
|
findFilter := &models.FindFilterType{
|
|
Sort: &sort,
|
|
}
|
|
|
|
// get the count
|
|
pp := 0
|
|
findFilter.PerPage = &pp
|
|
countResult, err := r.Scene().Query(models.SceneQueryOptions{
|
|
QueryOptions: models.QueryOptions{
|
|
FindFilter: findFilter,
|
|
Count: true,
|
|
},
|
|
SceneFilter: sceneFilter,
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("error getting scene count: %w", err)
|
|
}
|
|
|
|
j.progress.SetTotal(countResult.Count)
|
|
|
|
return scene.BatchProcess(ctx, r.Scene(), sceneFilter, findFilter, func(scene *models.Scene) error {
|
|
if job.IsCancelled(ctx) {
|
|
return nil
|
|
}
|
|
|
|
j.identifyScene(ctx, scene, sources)
|
|
return nil
|
|
})
|
|
}
|
|
|
|
func (j *IdentifyJob) identifyScene(ctx context.Context, s *models.Scene, sources []identify.ScraperSource) {
|
|
if job.IsCancelled(ctx) {
|
|
return
|
|
}
|
|
|
|
var taskError error
|
|
j.progress.ExecuteTask("Identifying "+s.Path, func() {
|
|
task := identify.SceneIdentifier{
|
|
DefaultOptions: j.input.Options,
|
|
Sources: sources,
|
|
ScreenshotSetter: &scene.PathsScreenshotSetter{
|
|
Paths: instance.Paths,
|
|
FileNamingAlgorithm: instance.Config.GetVideoFileNamingAlgorithm(),
|
|
},
|
|
SceneUpdatePostHookExecutor: j.postHookExecutor,
|
|
}
|
|
|
|
taskError = task.Identify(ctx, j.txnManager, s)
|
|
})
|
|
|
|
if taskError != nil {
|
|
logger.Errorf("Error encountered identifying %s: %v", s.Path, taskError)
|
|
}
|
|
|
|
j.progress.Increment()
|
|
}
|
|
|
|
func (j *IdentifyJob) getSources() ([]identify.ScraperSource, error) {
|
|
var ret []identify.ScraperSource
|
|
for _, source := range j.input.Sources {
|
|
// get scraper source
|
|
stashBox, err := j.getStashBox(source.Source)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var src identify.ScraperSource
|
|
if stashBox != nil {
|
|
src = identify.ScraperSource{
|
|
Name: "stash-box: " + stashBox.Endpoint,
|
|
Scraper: stashboxSource{
|
|
stashbox.NewClient(*stashBox, j.txnManager),
|
|
stashBox.Endpoint,
|
|
},
|
|
RemoteSite: stashBox.Endpoint,
|
|
}
|
|
} else {
|
|
scraperID := *source.Source.ScraperID
|
|
s := instance.ScraperCache.GetScraper(scraperID)
|
|
if s == nil {
|
|
return nil, fmt.Errorf("%w: scraper with id %q", models.ErrNotFound, scraperID)
|
|
}
|
|
src = identify.ScraperSource{
|
|
Name: s.Name,
|
|
Scraper: scraperSource{
|
|
cache: instance.ScraperCache,
|
|
scraperID: scraperID,
|
|
},
|
|
}
|
|
}
|
|
|
|
src.Options = source.Options
|
|
ret = append(ret, src)
|
|
}
|
|
|
|
return ret, nil
|
|
}
|
|
|
|
func (j *IdentifyJob) getStashBox(src *models.ScraperSourceInput) (*models.StashBox, error) {
|
|
if src.ScraperID != nil {
|
|
return nil, nil
|
|
}
|
|
|
|
// must be stash-box
|
|
if src.StashBoxIndex == nil && src.StashBoxEndpoint == nil {
|
|
return nil, fmt.Errorf("%w: stash_box_index or stash_box_endpoint or scraper_id must be set", ErrInput)
|
|
}
|
|
|
|
return j.stashBoxes.ResolveStashBox(*src)
|
|
}
|
|
|
|
type stashboxSource struct {
|
|
*stashbox.Client
|
|
endpoint string
|
|
}
|
|
|
|
func (s stashboxSource) ScrapeScene(ctx context.Context, sceneID int) (*models.ScrapedScene, error) {
|
|
results, err := s.FindStashBoxScenesByFingerprintsFlat(ctx, []string{strconv.Itoa(sceneID)})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("error querying stash-box using scene ID %d: %w", sceneID, err)
|
|
}
|
|
|
|
if len(results) > 0 {
|
|
return results[0], nil
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func (s stashboxSource) String() string {
|
|
return fmt.Sprintf("stash-box %s", s.endpoint)
|
|
}
|
|
|
|
type scraperSource struct {
|
|
cache *scraper.Cache
|
|
scraperID string
|
|
}
|
|
|
|
func (s scraperSource) ScrapeScene(ctx context.Context, sceneID int) (*models.ScrapedScene, error) {
|
|
content, err := s.cache.ScrapeID(ctx, s.scraperID, sceneID, models.ScrapeContentTypeScene)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if scene, ok := content.(*models.ScrapedScene); ok {
|
|
return scene, nil
|
|
}
|
|
|
|
return nil, errors.New("could not convert content to scene")
|
|
}
|
|
|
|
func (s scraperSource) String() string {
|
|
return fmt.Sprintf("scraper %s", s.scraperID)
|
|
}
|