2024-10-09 21:56:40 +02:00
|
|
|
package series
|
2024-10-09 11:12:44 +02:00
|
|
|
|
2024-10-09 21:56:40 +02:00
|
|
|
import (
|
2024-12-17 14:26:32 +01:00
|
|
|
"errors"
|
2024-10-09 21:56:40 +02:00
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2024-10-14 13:44:24 +02:00
|
|
|
func ProcessSeries(series []Serie, update bool) error {
|
2024-10-24 17:16:17 +02:00
|
|
|
utils.Sep()
|
2024-10-11 20:20:01 +02:00
|
|
|
fmt.Printf(" S E R I E S\n")
|
2024-10-09 21:56:40 +02:00
|
|
|
utils.Sep()
|
|
|
|
for _, serie := range series {
|
|
|
|
// Debug print
|
2024-10-24 17:16:17 +02:00
|
|
|
fmt.Printf("Título: %s\n", serie.Title)
|
2024-10-09 21:56:40 +02:00
|
|
|
|
2024-10-14 13:44:24 +02:00
|
|
|
// If we're updating, the process is a bit different
|
2024-10-14 16:52:15 +02:00
|
|
|
if update || !serie.New {
|
2024-10-14 13:44:24 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-10-09 21:56:40 +02:00
|
|
|
// 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 {
|
2024-10-11 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error searching serie by title %s: %s\n", serie.Title, err)
|
2024-10-09 21:56:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-12-17 14:26:32 +01:00
|
|
|
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)
|
2024-10-09 21:56:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-10-11 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error downloading %s for %s: %s\n", image.ImageType, serie.Title, err)
|
2024-10-09 21:56:40 +02:00
|
|
|
} 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,
|
2024-10-11 20:20:16 +02:00
|
|
|
"Episode": serie.Episode,
|
2024-10-09 21:56:40 +02:00
|
|
|
"Image": serie.Image,
|
|
|
|
"Poster": serie.Poster,
|
|
|
|
"Background": serie.Background,
|
|
|
|
"Date": serie.Date,
|
|
|
|
"Tags": "watching",
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.GenerateMarkdown(templatePath, outputPath, data)
|
|
|
|
}
|
2024-10-14 13:44:24 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|