mirror of
https://github.com/stashapp/stash.git
synced 2025-12-07 00:43:12 +01:00
* Add sub-scraper functionality * Add scraping of performer image * Add scene cover image scraping * Port UI changes to v2.5 * Fix v2.5 dialog suggest color * Don't convert eol of UI to support pretty
84 lines
1.7 KiB
Go
84 lines
1.7 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) error {
|
|
if p == nil || p.Image == nil || !strings.HasPrefix(*p.Image, "http") {
|
|
// nothing to do
|
|
return nil
|
|
}
|
|
|
|
img, err := getImage(*p.Image)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
p.Image = img
|
|
|
|
return nil
|
|
}
|
|
|
|
func setSceneImage(s *models.ScrapedScene) 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)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
s.Image = img
|
|
|
|
return nil
|
|
}
|
|
|
|
func getImage(url string) (*string, error) {
|
|
client := &http.Client{
|
|
Timeout: imageGetTimeout,
|
|
}
|
|
|
|
// assume is a URL for now
|
|
resp, err := client.Get(url)
|
|
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) (*string, error) {
|
|
return getImage(stashURL + "/performer/" + performerID + "/image")
|
|
}
|
|
|
|
func getStashSceneImage(stashURL string, sceneID string) (*string, error) {
|
|
return getImage(stashURL + "/scene/" + sceneID + "/screenshot")
|
|
}
|