package series import ( "fmt" "hugo-medialog/utils" "os" "path/filepath" "gopkg.in/yaml.v3" ) func LoadSeries() ([]Serie, error) { seriesFile := os.Getenv("OBSIDIAN_SERIES_FILE") fileData, err := os.ReadFile(seriesFile) if err != nil { return nil, err } var series []Serie err = yaml.Unmarshal(fileData, &series) if err != nil { return nil, err } return series, nil } func ProcessSeries(series []Serie, update bool) error { fmt.Printf(" S E R I E S\n") utils.Sep() for _, serie := range series { // Debug print 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 || !serie.New { 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) if err != nil { fmt.Printf(" ! Error searching serie by title %s: %s\n", serie.Title, err) continue } } // Now we need to get the images from fanart posterURL, backgroundURL, logoURL, err := FetchImagesFromFanart(serie.IDs.TVDB) if err != nil { fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", serie.Title, err) continue } imageTypes := []struct { URL string ImageType string SetField func(imagePath string) }{ { URL: posterURL, ImageType: "poster", SetField: func(imagePath string) { serie.Poster = imagePath }, }, { URL: backgroundURL, ImageType: "background", SetField: func(imagePath string) { serie.Background = imagePath }, }, { URL: logoURL, ImageType: "logo", SetField: func(imagePath string) { serie.Image = imagePath }, }, } for _, image := range imageTypes { if image.URL != "" { err := DownloadImage(image.URL, serie.IDs.Slug, image.ImageType) if err != nil { fmt.Printf(" ! Error downloading %s for %s: %s\n", image.ImageType, serie.Title, err) } else { image.SetField(fmt.Sprintf("%s-%s.jpg", serie.IDs.Slug, image.ImageType)) } } } err = generateSerieMarkdown(serie) if err != nil { return err } utils.Sep() } return nil } func generateSerieMarkdown(serie Serie) error { templatePath := filepath.Join(os.Getenv("TEMPLATES_DIR"), "serie.md.tpl") outputDir := os.Getenv("MARKDOWN_OUTPUT_SERIES_DIR") if err := utils.CreateDirIfNotExists(outputDir); err != nil { return err } outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", serie.IDs.Slug)) data := map[string]interface{}{ "Title": serie.Title, "Link": serie.Link, "Subtitle": serie.Year, "Year": serie.Year, "Rate": serie.Rate, "Progress": serie.Progress, "Episode": serie.Episode, "Image": serie.Image, "Poster": serie.Poster, "Background": serie.Background, "Date": serie.Date, "Tags": "watching", } 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 }