Added --update param to movies

This commit is contained in:
Óscar M. Lage 2024-10-14 16:49:56 +02:00
parent a3b11e9e5b
commit 9d30151d46
2 changed files with 28 additions and 2 deletions

View File

@ -25,7 +25,7 @@ func LoadMovies() ([]Movie, error) {
return movies, nil return movies, nil
} }
func ProcessMovies(movies []Movie) error { func ProcessMovies(movies []Movie, update bool) error {
fmt.Printf(" M O V I E S\n") fmt.Printf(" M O V I E S\n")
utils.Sep() utils.Sep()
for _, movie := range movies { for _, movie := range movies {
@ -33,6 +33,25 @@ func ProcessMovies(movies []Movie) error {
fmt.Printf("Título: %s, Puntuación: %.1f, Fecha: %s\n", fmt.Printf("Título: %s, Puntuación: %.1f, Fecha: %s\n",
movie.Title, movie.Rate, movie.Date) 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 we dont have IDs, search movie by Title and get the IDs
if movie.IDs.Trakt == 0 { if movie.IDs.Trakt == 0 {
err := SearchMovieByTitle(movie.Title, &movie) err := SearchMovieByTitle(movie.Title, &movie)
@ -115,3 +134,10 @@ func generateMovieMarkdown(movie Movie) error {
return utils.GenerateMarkdown(templatePath, outputPath, data) 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
}

View File

@ -55,7 +55,7 @@ func processMovies(update bool) {
fmt.Printf("Error reading movies file: %v\n", err) fmt.Printf("Error reading movies file: %v\n", err)
return return
} }
err = movies.ProcessMovies(moviesList) err = movies.ProcessMovies(moviesList, update)
if err != nil { if err != nil {
fmt.Printf("Error processing movies: %v\n", err) fmt.Printf("Error processing movies: %v\n", err)
} }