2020-03-11 00:41:55 +00:00
|
|
|
package scraper
|
|
|
|
|
|
|
|
import (
|
2021-03-01 00:47:39 +00:00
|
|
|
"crypto/tls"
|
2021-03-23 21:12:11 +00:00
|
|
|
"fmt"
|
2020-03-11 00:41:55 +00:00
|
|
|
"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
|
|
|
|
|
2020-07-21 04:06:25 +00:00
|
|
|
func setPerformerImage(p *models.ScrapedPerformer, globalConfig GlobalConfig) error {
|
2020-03-11 00:41:55 +00:00
|
|
|
if p == nil || p.Image == nil || !strings.HasPrefix(*p.Image, "http") {
|
|
|
|
// nothing to do
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 04:06:25 +00:00
|
|
|
img, err := getImage(*p.Image, globalConfig)
|
2020-03-11 00:41:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Image = img
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-21 04:06:25 +00:00
|
|
|
func setSceneImage(s *models.ScrapedScene, globalConfig GlobalConfig) error {
|
2020-03-11 00:41:55 +00:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2020-07-21 04:06:25 +00:00
|
|
|
img, err := getImage(*s.Image, globalConfig)
|
2020-03-11 00:41:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Image = img
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-10 05:34:15 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-21 04:06:25 +00:00
|
|
|
func getImage(url string, globalConfig GlobalConfig) (*string, error) {
|
2020-03-11 00:41:55 +00:00
|
|
|
client := &http.Client{
|
2021-03-01 00:47:39 +00:00
|
|
|
Transport: &http.Transport{ // ignore insecure certificates
|
2021-04-11 23:31:33 +00:00
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: !globalConfig.GetScraperCertCheck()}},
|
2020-03-11 00:41:55 +00:00
|
|
|
Timeout: imageGetTimeout,
|
|
|
|
}
|
|
|
|
|
2020-03-20 21:55:15 +00:00
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-04-11 23:31:33 +00:00
|
|
|
userAgent := globalConfig.GetScraperUserAgent()
|
2020-03-20 21:55:15 +00:00
|
|
|
if userAgent != "" {
|
|
|
|
req.Header.Set("User-Agent", userAgent)
|
|
|
|
}
|
|
|
|
|
2020-03-11 00:41:55 +00:00
|
|
|
// assume is a URL for now
|
2020-07-09 22:42:06 +00:00
|
|
|
|
|
|
|
// set the host of the URL as the referer
|
|
|
|
if req.URL.Scheme != "" {
|
2020-11-29 23:50:43 +00:00
|
|
|
req.Header.Set("Referer", req.URL.Scheme+"://"+req.Host+"/")
|
2020-07-09 22:42:06 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 21:55:15 +00:00
|
|
|
resp, err := client.Do(req)
|
2020-07-09 22:42:06 +00:00
|
|
|
|
2020-03-11 00:41:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-23 21:12:11 +00:00
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return nil, fmt.Errorf("http error %d", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
2020-03-11 00:41:55 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-07-21 04:06:25 +00:00
|
|
|
func getStashPerformerImage(stashURL string, performerID string, globalConfig GlobalConfig) (*string, error) {
|
|
|
|
return getImage(stashURL+"/performer/"+performerID+"/image", globalConfig)
|
2020-03-11 00:41:55 +00:00
|
|
|
}
|
|
|
|
|
2020-07-21 04:06:25 +00:00
|
|
|
func getStashSceneImage(stashURL string, sceneID string, globalConfig GlobalConfig) (*string, error) {
|
|
|
|
return getImage(stashURL+"/scene/"+sceneID+"/screenshot", globalConfig)
|
2020-03-11 00:41:55 +00:00
|
|
|
}
|