stash/pkg/scraper/query_url.go

104 lines
2.3 KiB
Go

package scraper
import (
"path/filepath"
"strings"
"github.com/stashapp/stash/pkg/models"
)
type queryURLReplacements map[string]mappedRegexConfigs
type queryURLParameters map[string]string
func queryURLParametersFromScene(scene *models.Scene) queryURLParameters {
ret := make(queryURLParameters)
ret["checksum"] = scene.Checksum
ret["oshash"] = scene.OSHash
ret["filename"] = filepath.Base(scene.Path)
if scene.Title != "" {
ret["title"] = scene.Title
}
if len(scene.URLs.List()) > 0 {
ret["url"] = scene.URLs.List()[0]
}
return ret
}
func queryURLParametersFromScrapedScene(scene ScrapedSceneInput) queryURLParameters {
ret := make(queryURLParameters)
setField := func(field string, value *string) {
if value != nil {
ret[field] = *value
}
}
setField("title", scene.Title)
setField("code", scene.Code)
if len(scene.URLs) > 0 {
setField("url", &scene.URLs[0])
} else {
setField("url", scene.URL)
}
setField("date", scene.Date)
setField("details", scene.Details)
setField("director", scene.Director)
setField("remote_site_id", scene.RemoteSiteID)
return ret
}
func queryURLParameterFromURL(url string) queryURLParameters {
ret := make(queryURLParameters)
ret["url"] = url
return ret
}
func queryURLParametersFromGallery(gallery *models.Gallery) queryURLParameters {
ret := make(queryURLParameters)
ret["checksum"] = gallery.PrimaryChecksum()
if gallery.Path != "" {
ret["filename"] = filepath.Base(gallery.Path)
}
if gallery.Title != "" {
ret["title"] = gallery.Title
}
if len(gallery.URLs.List()) > 0 {
ret["url"] = gallery.URLs.List()[0]
}
return ret
}
func (p queryURLParameters) applyReplacements(r queryURLReplacements) {
for k, v := range p {
rpl, found := r[k]
if found {
p[k] = rpl.apply(v)
}
}
}
func (p queryURLParameters) constructURL(url string) string {
ret := url
for k, v := range p {
ret = strings.ReplaceAll(ret, "{"+k+"}", v)
}
return ret
}
// replaceURL does a partial URL Replace ( only url parameter is used)
func replaceURL(url string, scraperConfig scraperTypeConfig) string {
u := url
queryURL := queryURLParameterFromURL(u)
if scraperConfig.QueryURLReplacements != nil {
queryURL.applyReplacements(scraperConfig.QueryURLReplacements)
u = queryURL.constructURL(scraperConfig.QueryURL)
}
return u
}