hugo-medialog/internal/books/controller.go

114 lines
3.0 KiB
Go
Raw Normal View History

2024-10-10 12:13:40 +00:00
package books
2024-10-09 09:12:44 +00:00
2024-10-10 12:13:40 +00:00
import (
"fmt"
"hugo-medialog/utils"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
func LoadBooks() ([]Book, error) {
booksFile := os.Getenv("OBSIDIAN_BOOKS_FILE")
fileData, err := os.ReadFile(booksFile)
if err != nil {
return nil, err
}
var books []Book
err = yaml.Unmarshal(fileData, &books)
if err != nil {
return nil, err
}
return books, nil
}
func ProcessBooks(books []Book, update bool) error {
2024-10-11 18:20:01 +00:00
fmt.Printf(" B O O K S\n")
2024-10-10 12:13:40 +00:00
utils.Sep()
for _, book := range books {
fmt.Printf("Title: %s, Author: %s, ID: %s\n", book.Title, book.Author, book.ID)
// If we're updating, the process is a bit different
if update {
outputDir := os.Getenv("MARKDOWN_OUTPUT_BOOKS_DIR")
mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(book.Title)))
frontmatter, content, err := utils.LoadMarkdown(mdFilePath)
if err != nil {
fmt.Printf(" ! Error loading markdown frontmatter for book %s: %v\n", book.Title, err)
continue
}
updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, book)
err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content)
if err != nil {
fmt.Printf(" ! Error saving updated markdown for book %s: %v\n", book.Title, err)
continue
}
}
2024-10-10 12:13:40 +00:00
// If we dont have ID, search movie by Title and get the ID
if book.ID == "" {
err := SearchBookByTitle(book.Title, &book)
if err != nil {
2024-10-11 19:24:46 +00:00
fmt.Printf(" ! Error searching book by title %s: %s\n", book.Title, err)
2024-10-10 12:13:40 +00:00
continue
}
}
book.Slug = utils.Sluggify(book.Title)
// Now we need to get the image
imageURL := getBookCoverByTitle(book.Title)
if imageURL != "" {
err := DownloadImage(imageURL, book.Slug)
if err != nil {
2024-10-11 19:24:46 +00:00
fmt.Printf(" ! Error downloading %s: %s\n", imageURL, err)
2024-10-10 12:13:40 +00:00
}
}
book.Image = fmt.Sprintf("%s.jpg", book.Slug)
err := generateBookMarkdown(book)
if err != nil {
return err
}
utils.Sep()
}
return nil
}
func generateBookMarkdown(book Book) error {
templatePath := filepath.Join(os.Getenv("TEMPLATES_DIR"), "book.md.tpl")
outputDir := os.Getenv("MARKDOWN_OUTPUT_BOOKS_DIR")
if err := utils.CreateDirIfNotExists(outputDir); err != nil {
return err
}
outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", book.Slug))
data := map[string]interface{}{
"Title": book.Title,
"Author": book.Author,
"Link": book.Link,
"Subtitle": book.Year,
"Year": book.Year,
"Rate": book.Rate,
"Pages": book.Pages,
"Progress": book.Progress,
"Image": book.Image,
"Date": book.Date,
"Tags": "reading",
}
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, book Book) utils.FrontMatter {
// Update fields (adapt based on your actual Book struct)
frontmatter.Progress = book.Progress
frontmatter.Date = book.Date
frontmatter.Rate = book.Rate
return frontmatter
}