From 80b872d6e9d7a17d8a992242e2337eb37aa9b553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20M=2E=20Lage?= Date: Tue, 17 Dec 2024 14:26:32 +0100 Subject: [PATCH] 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. --- internal/movies/controller.go | 39 ++++++++++++++++++++++++++++----- internal/movies/model.go | 1 + internal/series/controller.go | 39 ++++++++++++++++++++++++++++----- internal/series/model.go | 1 + utils/utils.go | 41 +++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 10 deletions(-) diff --git a/internal/movies/controller.go b/internal/movies/controller.go index f3faa41..904d28e 100644 --- a/internal/movies/controller.go +++ b/internal/movies/controller.go @@ -1,6 +1,7 @@ package movies import ( + "errors" "fmt" "hugo-medialog/utils" "os" @@ -62,11 +63,39 @@ func ProcessMovies(movies []Movie, update bool) error { } } - // Now we need to get the images from fanart - 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 + 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) + 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 { diff --git a/internal/movies/model.go b/internal/movies/model.go index 4888fb5..df43535 100644 --- a/internal/movies/model.go +++ b/internal/movies/model.go @@ -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 } diff --git a/internal/series/controller.go b/internal/series/controller.go index 536f47a..ef41066 100644 --- a/internal/series/controller.go +++ b/internal/series/controller.go @@ -1,6 +1,7 @@ package series import ( + "errors" "fmt" "hugo-medialog/utils" "os" @@ -62,11 +63,39 @@ func ProcessSeries(series []Serie, update bool) error { } } - // Now we need to get the images from fanart - 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 + 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) + 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 { diff --git a/internal/series/model.go b/internal/series/model.go index ab6e514..948c10d 100644 --- a/internal/series/model.go +++ b/internal/series/model.go @@ -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 } diff --git a/utils/utils.go b/utils/utils.go index 5ca2caa..fd6c879 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -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) +}