Add FetchImageFromGoogle function for the images download
From now on we can use both: fanart and google for the images download. By default it will be using fanart but if we want to use google because images in fanart are not appropriate (or it didn't find anything because the content is Spanish or reasons), we can add the following to the .md: ```yml query: google ``` And it should use google for image searching instead of fanart.
This commit is contained in:
parent
bd4b1c3059
commit
80b872d6e9
@ -1,6 +1,7 @@
|
||||
package movies
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"hugo-medialog/utils"
|
||||
"os"
|
||||
@ -62,12 +63,40 @@ func ProcessMovies(movies []Movie, update bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
posterURL, backgroundURL, logoURL, err := "", "", "", errors.New("")
|
||||
|
||||
if movie.Query != "google" {
|
||||
// Now we need to get the images from fanart
|
||||
posterURL, backgroundURL, logoURL, err := FetchImagesFromFanart(movie.IDs.TMDB)
|
||||
posterURL, backgroundURL, logoURL, err = FetchImagesFromFanart(movie.IDs.TMDB)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", movie.Title, err)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
// Images from Google (as backup method)
|
||||
searchQuery := fmt.Sprintf("%s+movie+poster", movie.Title)
|
||||
posterURL, err = utils.FetchImageFromGoogle(searchQuery)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Google for %s: %s\n", movie.Title, err)
|
||||
posterURL = "default-cover.jpg"
|
||||
continue
|
||||
}
|
||||
searchQuery = fmt.Sprintf("%s+movie+background", movie.Title)
|
||||
backgroundURL, err = utils.FetchImageFromGoogle(searchQuery)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Google for %s: %s\n", movie.Title, err)
|
||||
posterURL = "default-cover.jpg"
|
||||
continue
|
||||
}
|
||||
searchQuery = fmt.Sprintf("%s+movie+logo", movie.Title)
|
||||
logoURL, err = utils.FetchImageFromGoogle(searchQuery)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Google for %s: %s\n", movie.Title, err)
|
||||
posterURL = "default-cover.jpg"
|
||||
continue
|
||||
}
|
||||
fmt.Printf("posterURL: %s\n, backgroundURL: %s\n, logoURL: %s\n", posterURL, backgroundURL, logoURL)
|
||||
}
|
||||
|
||||
imageTypes := []struct {
|
||||
URL string
|
||||
|
@ -18,6 +18,7 @@ type Movie struct {
|
||||
Background string `yaml:"background-image"`
|
||||
Date string `yaml:"date"`
|
||||
New bool `yaml:"new"`
|
||||
Query string `yaml:"query"`
|
||||
Tags []string
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,7 @@
|
||||
package series
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"hugo-medialog/utils"
|
||||
"os"
|
||||
@ -62,12 +63,40 @@ func ProcessSeries(series []Serie, update bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
posterURL, backgroundURL, logoURL, err := "", "", "", errors.New("")
|
||||
|
||||
if serie.Query != "google" {
|
||||
// Now we need to get the images from fanart
|
||||
posterURL, backgroundURL, logoURL, err := FetchImagesFromFanart(serie.IDs.TVDB)
|
||||
posterURL, backgroundURL, logoURL, err = FetchImagesFromFanart(serie.IDs.TVDB)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", serie.Title, err)
|
||||
continue
|
||||
}
|
||||
} else {
|
||||
// Images from Google (as backup method)
|
||||
searchQuery := fmt.Sprintf("%s+serie+poster", serie.Title)
|
||||
posterURL, err = utils.FetchImageFromGoogle(searchQuery)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Google for %s: %s\n", serie.Title, err)
|
||||
posterURL = "default-cover.jpg"
|
||||
continue
|
||||
}
|
||||
searchQuery = fmt.Sprintf("%s+serie+background", serie.Title)
|
||||
backgroundURL, err = utils.FetchImageFromGoogle(searchQuery)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Google for %s: %s\n", serie.Title, err)
|
||||
posterURL = "default-cover.jpg"
|
||||
continue
|
||||
}
|
||||
searchQuery = fmt.Sprintf("%s+serie+logo", serie.Title)
|
||||
logoURL, err = utils.FetchImageFromGoogle(searchQuery)
|
||||
if err != nil {
|
||||
fmt.Printf(" ! Error fetching images from Google for %s: %s\n", serie.Title, err)
|
||||
posterURL = "default-cover.jpg"
|
||||
continue
|
||||
}
|
||||
fmt.Printf("posterURL: %s\n, backgroundURL: %s\n, logoURL: %s\n", posterURL, backgroundURL, logoURL)
|
||||
}
|
||||
|
||||
imageTypes := []struct {
|
||||
URL string
|
||||
|
@ -20,6 +20,7 @@ type Serie struct {
|
||||
Background string `yaml:"background-image"`
|
||||
Date string `yaml:"date"`
|
||||
New bool `yaml:"new"`
|
||||
Query string `yaml:"query"`
|
||||
Tags []string
|
||||
}
|
||||
|
||||
|
@ -3,12 +3,15 @@ package utils
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
@ -129,3 +132,41 @@ func GenerateThumbnails(originalPath, slug, outputDir string, resolutions []uint
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func FetchImageFromGoogle(query string) (string, error) {
|
||||
apiKey := os.Getenv("GOOGLE_CUSTOM_SEARCH_ENGINE_API_KEY")
|
||||
cx := os.Getenv("GOOGLE_CX")
|
||||
|
||||
searchURL := fmt.Sprintf("https://www.googleapis.com/customsearch/v1?q=%s&key=%s&searchType=image&num=1&cx=%s&gl=es&lr=lang_es", url.QueryEscape(query), apiKey, cx)
|
||||
|
||||
resp, err := http.Get(searchURL)
|
||||
if err != nil {
|
||||
error := fmt.Sprintf("Error fetching cover from Google Images API: %s\n", err)
|
||||
fmt.Printf(error)
|
||||
return "", errors.New(error)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
error := fmt.Sprintf("Error fetching cover, status code: %d\n", resp.StatusCode)
|
||||
fmt.Printf(error)
|
||||
return "", errors.New(error)
|
||||
}
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
||||
error := fmt.Sprintf("Error decoding JSON response: %s\n", err)
|
||||
fmt.Printf(error)
|
||||
return "", errors.New(error)
|
||||
}
|
||||
|
||||
if items, ok := result["items"].([]interface{}); ok && len(items) > 0 {
|
||||
if link, ok := items[0].(map[string]interface{})["link"].(string); ok {
|
||||
return link, nil
|
||||
}
|
||||
}
|
||||
|
||||
error := fmt.Sprintf("Other error has happened")
|
||||
fmt.Printf(error)
|
||||
return "", errors.New(error)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user