diff --git a/internal/movies/controller.go b/internal/movies/controller.go index 8b25fa3..cf005f4 100644 --- a/internal/movies/controller.go +++ b/internal/movies/controller.go @@ -25,7 +25,7 @@ func LoadMovies() ([]Movie, error) { return movies, nil } -func ProcessMovies(movies []Movie) error { +func ProcessMovies(movies []Movie, update bool) error { fmt.Printf(" M O V I E S\n") utils.Sep() for _, movie := range movies { @@ -33,6 +33,25 @@ func ProcessMovies(movies []Movie) error { fmt.Printf("Título: %s, Puntuación: %.1f, Fecha: %s\n", movie.Title, movie.Rate, movie.Date) + // If we're updating, the process is a bit different + 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 + } + // If we dont have IDs, search movie by Title and get the IDs if movie.IDs.Trakt == 0 { err := SearchMovieByTitle(movie.Title, &movie) @@ -115,3 +134,10 @@ func generateMovieMarkdown(movie Movie) error { 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, movie Movie) utils.FrontMatter { + frontmatter.Date = movie.Date + frontmatter.Rate = movie.Rate + return frontmatter +} diff --git a/main.go b/main.go index b8b5eb1..be05b94 100644 --- a/main.go +++ b/main.go @@ -55,7 +55,7 @@ func processMovies(update bool) { fmt.Printf("Error reading movies file: %v\n", err) return } - err = movies.ProcessMovies(moviesList) + err = movies.ProcessMovies(moviesList, update) if err != nil { fmt.Printf("Error processing movies: %v\n", err) }