118 lines
2.8 KiB
Go
118 lines
2.8 KiB
Go
// internal/movies/controller.go
|
|
package movies
|
|
|
|
import (
|
|
"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) error {
|
|
utils.Sep()
|
|
for _, movie := range movies {
|
|
// Debug print
|
|
fmt.Printf("Título: %s, Puntuación: %.1f, Fecha: %s\n",
|
|
movie.Title, movie.Rate, movie.Date)
|
|
|
|
// 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
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
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_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,
|
|
"Image": movie.Image,
|
|
"Poster": movie.Poster,
|
|
"Background": movie.Background,
|
|
"Date": movie.Date,
|
|
"Tags": "watching",
|
|
}
|
|
|
|
return utils.GenerateMarkdown(templatePath, outputPath, data)
|
|
}
|