2024-10-09 11:12:44 +02:00
|
|
|
package movies
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hugo-medialog/utils"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LoadMovies() ([]Movie, error) {
|
|
|
|
moviesFile := os.Getenv("OBSIDIAN_MOVIES_FILE")
|
|
|
|
fileData, err := os.ReadFile(moviesFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var movies []Movie
|
|
|
|
err = yaml.Unmarshal(fileData, &movies)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return movies, nil
|
|
|
|
}
|
|
|
|
|
2024-10-14 16:49:56 +02:00
|
|
|
func ProcessMovies(movies []Movie, update bool) error {
|
2024-10-11 20:20:01 +02:00
|
|
|
fmt.Printf(" M O V I E S\n")
|
2024-10-09 21:05:11 +02:00
|
|
|
utils.Sep()
|
2024-10-09 11:12:44 +02:00
|
|
|
for _, movie := range movies {
|
2024-10-09 21:05:11 +02:00
|
|
|
// Debug print
|
2024-10-09 11:12:44 +02:00
|
|
|
fmt.Printf("Título: %s, Puntuación: %.1f, Fecha: %s\n",
|
|
|
|
movie.Title, movie.Rate, movie.Date)
|
2024-10-09 21:05:11 +02:00
|
|
|
|
2024-10-14 16:49:56 +02:00
|
|
|
// If we're updating, the process is a bit different
|
2024-10-14 16:52:15 +02:00
|
|
|
if update || !movie.New {
|
2024-10-14 16:49:56 +02:00
|
|
|
outputDir := os.Getenv("MARKDOWN_OUTPUT_MOVIES_DIR")
|
|
|
|
mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(movie.Title)))
|
|
|
|
frontmatter, content, err := utils.LoadMarkdown(mdFilePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" ! Error loading markdown frontmatter for movie %s: %v\n", movie.Title, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, movie)
|
|
|
|
err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" ! Error saving updated markdown for movie %s: %v\n", movie.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:05:11 +02:00
|
|
|
// If we dont have IDs, search movie by Title and get the IDs
|
|
|
|
if movie.IDs.Trakt == 0 {
|
|
|
|
err := SearchMovieByTitle(movie.Title, &movie)
|
|
|
|
if err != nil {
|
2024-10-11 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error searching movie by title %s: %s\n", movie.Title, err)
|
2024-10-09 21:05:11 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now we need to get the images from fanart
|
|
|
|
posterURL, backgroundURL, logoURL, err := FetchImagesFromFanart(movie.IDs.TMDB)
|
|
|
|
if err != nil {
|
2024-10-11 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", movie.Title, err)
|
2024-10-09 21:05:11 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
imageTypes := []struct {
|
|
|
|
URL string
|
|
|
|
ImageType string
|
|
|
|
SetField func(imagePath string)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
URL: posterURL,
|
|
|
|
ImageType: "poster",
|
|
|
|
SetField: func(imagePath string) { movie.Poster = imagePath },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: backgroundURL,
|
|
|
|
ImageType: "background",
|
|
|
|
SetField: func(imagePath string) { movie.Background = imagePath },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
URL: logoURL,
|
|
|
|
ImageType: "logo",
|
|
|
|
SetField: func(imagePath string) { movie.Image = imagePath },
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, image := range imageTypes {
|
|
|
|
if image.URL != "" {
|
|
|
|
err := DownloadImage(image.URL, movie.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, movie.Title, err)
|
2024-10-09 21:05:11 +02:00
|
|
|
} else {
|
|
|
|
image.SetField(fmt.Sprintf("%s-%s.jpg", movie.IDs.Slug, image.ImageType))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-09 21:56:56 +02:00
|
|
|
// utils.Debug(movie)
|
2024-10-09 21:05:11 +02:00
|
|
|
|
|
|
|
err = generateMovieMarkdown(movie)
|
2024-10-09 11:12:44 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-09 21:05:11 +02:00
|
|
|
utils.Sep()
|
2024-10-09 11:12:44 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateMovieMarkdown(movie Movie) error {
|
|
|
|
templatePath := filepath.Join(os.Getenv("TEMPLATES_DIR"), "movie.md.tpl")
|
2024-10-09 21:14:25 +02:00
|
|
|
outputDir := os.Getenv("MARKDOWN_OUTPUT_MOVIES_DIR")
|
2024-10-09 11:12:44 +02:00
|
|
|
if err := utils.CreateDirIfNotExists(outputDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-10-09 21:05:11 +02:00
|
|
|
outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", movie.IDs.Slug))
|
2024-10-09 11:12:44 +02:00
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"Title": movie.Title,
|
2024-10-09 21:05:11 +02:00
|
|
|
"Link": movie.Link,
|
2024-10-09 11:12:44 +02:00
|
|
|
"Subtitle": movie.Year,
|
|
|
|
"Year": movie.Year,
|
|
|
|
"Rate": movie.Rate,
|
|
|
|
"Image": movie.Image,
|
|
|
|
"Poster": movie.Poster,
|
|
|
|
"Background": movie.Background,
|
|
|
|
"Date": movie.Date,
|
2024-10-09 21:05:11 +02:00
|
|
|
"Tags": "watching",
|
2024-10-09 11:12:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return utils.GenerateMarkdown(templatePath, outputPath, data)
|
|
|
|
}
|
2024-10-14 16:49:56 +02:00
|
|
|
|
|
|
|
// Helper function to update only YAML fields that exist in both the frontmatter and YAML data
|
|
|
|
func updateFrontmatterWithYAML(frontmatter utils.FrontMatter, movie Movie) utils.FrontMatter {
|
|
|
|
frontmatter.Date = movie.Date
|
|
|
|
frontmatter.Rate = movie.Rate
|
|
|
|
return frontmatter
|
|
|
|
}
|