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:
Óscar M. Lage 2024-12-17 14:26:32 +01:00
parent bd4b1c3059
commit 80b872d6e9
5 changed files with 111 additions and 10 deletions

View File

@ -1,6 +1,7 @@
package movies package movies
import ( import (
"errors"
"fmt" "fmt"
"hugo-medialog/utils" "hugo-medialog/utils"
"os" "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 // 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 { if err != nil {
fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", movie.Title, err) fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", movie.Title, err)
continue 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 { imageTypes := []struct {
URL string URL string

View File

@ -18,6 +18,7 @@ type Movie struct {
Background string `yaml:"background-image"` Background string `yaml:"background-image"`
Date string `yaml:"date"` Date string `yaml:"date"`
New bool `yaml:"new"` New bool `yaml:"new"`
Query string `yaml:"query"`
Tags []string Tags []string
} }

View File

@ -1,6 +1,7 @@
package series package series
import ( import (
"errors"
"fmt" "fmt"
"hugo-medialog/utils" "hugo-medialog/utils"
"os" "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 // 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 { if err != nil {
fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", serie.Title, err) fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", serie.Title, err)
continue 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 { imageTypes := []struct {
URL string URL string

View File

@ -20,6 +20,7 @@ type Serie struct {
Background string `yaml:"background-image"` Background string `yaml:"background-image"`
Date string `yaml:"date"` Date string `yaml:"date"`
New bool `yaml:"new"` New bool `yaml:"new"`
Query string `yaml:"query"`
Tags []string Tags []string
} }

View File

@ -3,12 +3,15 @@ package utils
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"image" "image"
"image/jpeg" "image/jpeg"
_ "image/jpeg" _ "image/jpeg"
_ "image/png" _ "image/png"
"io/ioutil" "io/ioutil"
"net/http"
"net/url"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "regexp"
@ -129,3 +132,41 @@ func GenerateThumbnails(originalPath, slug, outputDir string, resolutions []uint
return nil 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)
}