Added --update param to series

This commit is contained in:
Óscar M. Lage 2024-10-14 13:44:24 +02:00
parent 5ce0c35a11
commit d0f44bdc0b
2 changed files with 31 additions and 2 deletions

View File

@ -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
}

View File

@ -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)
}