mirror of
https://github.com/stashapp/stash.git
synced 2025-12-10 02:15:30 +01:00
* api/urlbuilders/movie: Auto format. * graphql+pkg+ui: Implement scraping movies by URL. This patch implements the missing required boilerplate for scraping movies by URL, using performers and scenes as a reference. Although this patch contains a big chunck of ground work for enabling scraping movies by fragment, the feature would require additional changes to be completely implemented and was not tested. * graphql+pkg+ui: Scrape movie studio. Extends and corrects the movie model for the ability to store and dereference studio IDs with received studio string from the scraper. This was done with Scenes as a reference. For simplicity the duplication of having `ScrapedMovieStudio` and `ScrapedSceneStudio` was kept, which should probably be refactored to be the same type in the model in the future. * ui/movies: Add movie scrape dialog. Adds possibility to update existing movie entries with the URL scraper. For this the MovieScrapeDialog.tsx was implemented with Performers and Scenes as a reference. In addition DurationUtils needs to be called one time for converting seconds from the model to the string that is displayed in the component. This seemed the least intrusive to me as it kept a ScrapeResult<string> type compatible with ScrapedInputGroupRow.
135 lines
3 KiB
Go
135 lines
3 KiB
Go
package scraper
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
)
|
|
|
|
// Timeout to get the image. Includes transfer time. May want to make this
|
|
// configurable at some point.
|
|
const imageGetTimeout = time.Second * 30
|
|
|
|
func setPerformerImage(p *models.ScrapedPerformer, globalConfig GlobalConfig) error {
|
|
if p == nil || p.Image == nil || !strings.HasPrefix(*p.Image, "http") {
|
|
// nothing to do
|
|
return nil
|
|
}
|
|
|
|
img, err := getImage(*p.Image, globalConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.Image = img
|
|
|
|
return nil
|
|
}
|
|
|
|
func setSceneImage(s *models.ScrapedScene, globalConfig GlobalConfig) error {
|
|
// don't try to get the image if it doesn't appear to be a URL
|
|
if s == nil || s.Image == nil || !strings.HasPrefix(*s.Image, "http") {
|
|
// nothing to do
|
|
return nil
|
|
}
|
|
|
|
img, err := getImage(*s.Image, globalConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.Image = img
|
|
|
|
return nil
|
|
}
|
|
|
|
func setMovieFrontImage(m *models.ScrapedMovie, globalConfig GlobalConfig) error {
|
|
// don't try to get the image if it doesn't appear to be a URL
|
|
if m == nil || m.FrontImage == nil || !strings.HasPrefix(*m.FrontImage, "http") {
|
|
// nothing to do
|
|
return nil
|
|
}
|
|
|
|
img, err := getImage(*m.FrontImage, globalConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m.FrontImage = img
|
|
|
|
return nil
|
|
}
|
|
|
|
func setMovieBackImage(m *models.ScrapedMovie, globalConfig GlobalConfig) error {
|
|
// don't try to get the image if it doesn't appear to be a URL
|
|
if m == nil || m.BackImage == nil || !strings.HasPrefix(*m.BackImage, "http") {
|
|
// nothing to do
|
|
return nil
|
|
}
|
|
|
|
img, err := getImage(*m.BackImage, globalConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
m.BackImage = img
|
|
|
|
return nil
|
|
}
|
|
|
|
func getImage(url string, globalConfig GlobalConfig) (*string, error) {
|
|
client := &http.Client{
|
|
Timeout: imageGetTimeout,
|
|
}
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
userAgent := globalConfig.UserAgent
|
|
if userAgent != "" {
|
|
req.Header.Set("User-Agent", userAgent)
|
|
}
|
|
|
|
// assume is a URL for now
|
|
|
|
// set the host of the URL as the referer
|
|
if req.URL.Scheme != "" {
|
|
req.Header.Set("Referer", req.URL.Scheme+"://"+req.Host)
|
|
}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// determine the image type and set the base64 type
|
|
contentType := resp.Header.Get("Content-Type")
|
|
if contentType == "" {
|
|
contentType = http.DetectContentType(body)
|
|
}
|
|
|
|
img := "data:" + contentType + ";base64," + utils.GetBase64StringFromData(body)
|
|
return &img, nil
|
|
}
|
|
|
|
func getStashPerformerImage(stashURL string, performerID string, globalConfig GlobalConfig) (*string, error) {
|
|
return getImage(stashURL+"/performer/"+performerID+"/image", globalConfig)
|
|
}
|
|
|
|
func getStashSceneImage(stashURL string, sceneID string, globalConfig GlobalConfig) (*string, error) {
|
|
return getImage(stashURL+"/scene/"+sceneID+"/screenshot", globalConfig)
|
|
}
|