Changes in movies code

This commit is contained in:
Óscar M. Lage 2024-10-09 21:05:11 +02:00
parent 43af80014f
commit b2e4deefdd
2 changed files with 65 additions and 5 deletions

View File

@ -27,14 +27,67 @@ func LoadMovies() ([]Movie, error) {
}
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)
// API Calls
err := generateMovieMarkdown(movie)
// 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
}
@ -45,11 +98,11 @@ func generateMovieMarkdown(movie Movie) error {
if err := utils.CreateDirIfNotExists(outputDir); err != nil {
return err
}
outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", movie.Title))
outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", movie.IDs.Slug))
data := map[string]interface{}{
"Title": movie.Title,
"Link": "https://imdb.com/enlace",
"Link": movie.Link,
"Subtitle": movie.Year,
"Year": movie.Year,
"Rate": movie.Rate,
@ -57,6 +110,7 @@ func generateMovieMarkdown(movie Movie) error {
"Poster": movie.Poster,
"Background": movie.Background,
"Date": movie.Date,
"Tags": "watching",
}
return utils.GenerateMarkdown(templatePath, outputPath, data)

View File

@ -2,7 +2,13 @@
package movies
type Movie struct {
Title string `yaml:"title"`
Title string `yaml:"title"`
IDs struct {
Slug string `json:"slug"`
IMDB string `json:"imdb"`
TMDB int `json:"tmdb"`
Trakt int `json:"trakt"`
} `json:"ids"`
Subtitle string `yaml:"subtitle"`
Link string `yaml:"link"`
Year int `yaml:"year"`