package movies import ( "errors" "fmt" "hugo-medialog/utils" "os" "path/filepath" "gopkg.in/yaml.v3" ) func LoadMovies() ([]Movie, error) { moviesFile := os.Getenv("OBSIDIAN_MOVIES_FILE") fileData, err := os.ReadFile(moviesFile) if err != nil { return nil, err } var movies []Movie err = yaml.Unmarshal(fileData, &movies) if err != nil { return nil, err } return movies, nil } func ProcessMovies(movies []Movie, update bool) error { utils.Sep() fmt.Printf(" M O V I E S\n") utils.Sep() for _, movie := range movies { // Debug print fmt.Printf("Título: %s\n", movie.Title) // If we're updating, the process is a bit different if update || !movie.New { 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) if err != nil { fmt.Printf(" ! Error searching movie by title %s: %s\n", movie.Title, err) continue } } posterURL, backgroundURL, logoURL, err := "", "", "", errors.New("") if movie.Query != "google" { // Now we need to get the images from fanart posterURL, backgroundURL, logoURL, err = FetchImagesFromFanart(movie.IDs.TMDB) if err != nil { fmt.Printf(" ! Error fetching images from Fanart.tv for %s: %s\n", movie.Title, err) continue } } else { // Images from Google (as backup method) searchQuery := fmt.Sprintf("%s+movie+poster", movie.Title) posterURL, err = utils.FetchImageFromGoogle(searchQuery) if err != nil { fmt.Printf(" ! Error fetching images from Google for %s: %s\n", movie.Title, err) posterURL = "default-cover.jpg" continue } searchQuery = fmt.Sprintf("%s+movie+background", movie.Title) backgroundURL, err = utils.FetchImageFromGoogle(searchQuery) if err != nil { fmt.Printf(" ! Error fetching images from Google for %s: %s\n", movie.Title, err) posterURL = "default-cover.jpg" continue } searchQuery = fmt.Sprintf("%s+movie+logo", movie.Title) logoURL, err = utils.FetchImageFromGoogle(searchQuery) if err != nil { fmt.Printf(" ! Error fetching images from Google for %s: %s\n", movie.Title, err) posterURL = "default-cover.jpg" continue } fmt.Printf("posterURL: %s\n, backgroundURL: %s\n, logoURL: %s\n", posterURL, backgroundURL, logoURL) } imageTypes := []struct { URL string ImageType string SetField func(imagePath string) }{ { URL: posterURL, ImageType: "poster", SetField: func(imagePath string) { movie.Poster = imagePath }, }, { URL: backgroundURL, ImageType: "background", SetField: func(imagePath string) { movie.Background = imagePath }, }, { URL: logoURL, ImageType: "logo", SetField: func(imagePath string) { movie.Image = imagePath }, }, } for _, image := range imageTypes { if image.URL != "" { err := DownloadImage(image.URL, movie.IDs.Slug, image.ImageType) if err != nil { fmt.Printf(" ! Error downloading %s for %s: %s\n", image.ImageType, movie.Title, err) } else { image.SetField(fmt.Sprintf("%s-%s.jpg", movie.IDs.Slug, image.ImageType)) } } } // utils.Debug(movie) err = generateMovieMarkdown(movie) if err != nil { return err } utils.Sep() } return nil } func generateMovieMarkdown(movie Movie) error { templatePath := filepath.Join(os.Getenv("TEMPLATES_DIR"), "movie.md.tpl") outputDir := os.Getenv("MARKDOWN_OUTPUT_MOVIES_DIR") if err := utils.CreateDirIfNotExists(outputDir); err != nil { return err } outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", movie.IDs.Slug)) data := map[string]interface{}{ "Title": movie.Title, "Link": movie.Link, "Subtitle": movie.Year, "Year": movie.Year, "Rate": movie.Rate, "Progress": movie.Progress, "Image": movie.Image, "Poster": movie.Poster, "Background": movie.Background, "Date": movie.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, movie Movie) utils.FrontMatter { frontmatter.Progress = movie.Progress frontmatter.Date = movie.Date frontmatter.Rate = movie.Rate return frontmatter }