diff --git a/internal/series/controller.go b/internal/series/controller.go index baacfbd..4fc88ec 100644 --- a/internal/series/controller.go +++ b/internal/series/controller.go @@ -25,7 +25,7 @@ func LoadSeries() ([]Serie, error) { return series, nil } -func ProcessSeries(series []Serie) error { +func ProcessSeries(series []Serie, update bool) error { fmt.Printf(" S E R I E S\n") utils.Sep() for _, serie := range series { @@ -33,6 +33,26 @@ func ProcessSeries(series []Serie) error { fmt.Printf("Título: %s, Puntuación: %.1f, Fecha: %s\n", serie.Title, serie.Rate, serie.Date) + // If we're updating, the process is a bit different + if update { + 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) @@ -117,3 +137,12 @@ func generateSerieMarkdown(serie Serie) 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, serie Serie) utils.FrontMatter { + frontmatter.Progress = serie.Progress + frontmatter.Episode = serie.Episode + frontmatter.Date = serie.Date + frontmatter.Rate = serie.Rate + return frontmatter +} diff --git a/main.go b/main.go index 0722163..d90b068 100644 --- a/main.go +++ b/main.go @@ -67,7 +67,7 @@ func processSeries(update bool) { fmt.Printf("Error reading series file: %v\n", err) return } - err = series.ProcessSeries(seriesList) + err = series.ProcessSeries(seriesList, update) if err != nil { fmt.Printf("Error processing series: %v\n", err) }