Óscar M. Lage
80b872d6e9
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.
177 lines
5.1 KiB
Go
177 lines
5.1 KiB
Go
package series
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"hugo-medialog/utils"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func LoadSeries() ([]Serie, error) {
|
|
seriesFile := os.Getenv("OBSIDIAN_SERIES_FILE")
|
|
fileData, err := os.ReadFile(seriesFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var series []Serie
|
|
err = yaml.Unmarshal(fileData, &series)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return series, nil
|
|
}
|
|
|
|
func ProcessSeries(series []Serie, update bool) error {
|
|
utils.Sep()
|
|
fmt.Printf(" S E R I E S\n")
|
|
utils.Sep()
|
|
for _, serie := range series {
|
|
// Debug print
|
|
fmt.Printf("Título: %s\n", serie.Title)
|
|
|
|
// If we're updating, the process is a bit different
|
|
if update || !serie.New {
|
|
outputDir := os.Getenv("MARKDOWN_OUTPUT_SERIES_DIR")
|
|
mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(serie.Title)))
|
|
frontmatter, content, err := utils.LoadMarkdown(mdFilePath)
|
|
if err != nil {
|
|
fmt.Printf(" ! Error loading markdown frontmatter for serie %s: %v\n", serie.Title, err)
|
|
continue
|
|
}
|
|
updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, serie)
|
|
err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content)
|
|
if err != nil {
|
|
fmt.Printf(" ! Error saving updated markdown for serie %s: %v\n", serie.Title, err)
|
|
continue
|
|
}
|
|
// We want to continue the loop here, in update's cases we don't
|
|
// want to do any api calls for info nor for images
|
|
continue
|
|
}
|
|
|
|
// If we dont have IDs, search serie by Title and get the IDs
|
|
if serie.IDs.Trakt == 0 {
|
|
err := SearchSerieByTitle(serie.Title, &serie)
|
|
if err != nil {
|
|
fmt.Printf(" ! Error searching serie by title %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 {
|
|
URL string
|
|
ImageType string
|
|
SetField func(imagePath string)
|
|
}{
|
|
{
|
|
URL: posterURL,
|
|
ImageType: "poster",
|
|
SetField: func(imagePath string) { serie.Poster = imagePath },
|
|
},
|
|
{
|
|
URL: backgroundURL,
|
|
ImageType: "background",
|
|
SetField: func(imagePath string) { serie.Background = imagePath },
|
|
},
|
|
{
|
|
URL: logoURL,
|
|
ImageType: "logo",
|
|
SetField: func(imagePath string) { serie.Image = imagePath },
|
|
},
|
|
}
|
|
|
|
for _, image := range imageTypes {
|
|
if image.URL != "" {
|
|
err := DownloadImage(image.URL, serie.IDs.Slug, image.ImageType)
|
|
if err != nil {
|
|
fmt.Printf(" ! Error downloading %s for %s: %s\n", image.ImageType, serie.Title, err)
|
|
} else {
|
|
image.SetField(fmt.Sprintf("%s-%s.jpg", serie.IDs.Slug, image.ImageType))
|
|
}
|
|
}
|
|
}
|
|
|
|
err = generateSerieMarkdown(serie)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
utils.Sep()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func generateSerieMarkdown(serie Serie) error {
|
|
templatePath := filepath.Join(os.Getenv("TEMPLATES_DIR"), "serie.md.tpl")
|
|
outputDir := os.Getenv("MARKDOWN_OUTPUT_SERIES_DIR")
|
|
if err := utils.CreateDirIfNotExists(outputDir); err != nil {
|
|
return err
|
|
}
|
|
outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", serie.IDs.Slug))
|
|
|
|
data := map[string]interface{}{
|
|
"Title": serie.Title,
|
|
"Link": serie.Link,
|
|
"Subtitle": serie.Year,
|
|
"Year": serie.Year,
|
|
"Rate": serie.Rate,
|
|
"Progress": serie.Progress,
|
|
"Episode": serie.Episode,
|
|
"Image": serie.Image,
|
|
"Poster": serie.Poster,
|
|
"Background": serie.Background,
|
|
"Date": serie.Date,
|
|
"Tags": "watching",
|
|
}
|
|
|
|
return utils.GenerateMarkdown(templatePath, outputPath, data)
|
|
}
|
|
|
|
// Helper function to update only YAML fields that exist in both the frontmatter and YAML data
|
|
func updateFrontmatterWithYAML(frontmatter utils.FrontMatter, serie Serie) utils.FrontMatter {
|
|
frontmatter.Progress = serie.Progress
|
|
frontmatter.Episode = serie.Episode
|
|
frontmatter.Date = serie.Date
|
|
frontmatter.Rate = serie.Rate
|
|
return frontmatter
|
|
}
|