2024-10-10 14:13:40 +02:00
|
|
|
package books
|
2024-10-09 11:12:44 +02:00
|
|
|
|
2024-10-10 14:13:40 +02: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
|
|
|
|
}
|
|
|
|
|
2024-10-11 22:48:21 +02:00
|
|
|
func ProcessBooks(books []Book, update bool) error {
|
2024-10-11 20:20:01 +02:00
|
|
|
fmt.Printf(" B O O K S\n")
|
2024-10-10 14:13:40 +02:00
|
|
|
utils.Sep()
|
|
|
|
for _, book := range books {
|
2024-10-14 16:52:23 +02:00
|
|
|
fmt.Printf("Title: %s\n", book.Title)
|
2024-10-10 14:13:40 +02:00
|
|
|
|
2024-10-11 22:48:21 +02:00
|
|
|
// If we're updating, the process is a bit different
|
2024-10-14 16:52:15 +02:00
|
|
|
if update || !book.New {
|
2024-10-11 22:48:21 +02:00
|
|
|
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-14 13:44:07 +02:00
|
|
|
// 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
|
2024-10-11 22:48:21 +02:00
|
|
|
}
|
|
|
|
|
2024-10-10 14:13:40 +02: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 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error searching book by title %s: %s\n", book.Title, err)
|
2024-10-10 14:13:40 +02: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 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error downloading %s: %s\n", imageURL, err)
|
2024-10-10 14:13:40 +02: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)
|
|
|
|
}
|
2024-10-11 22:48:21 +02:00
|
|
|
|
|
|
|
// 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 {
|
|
|
|
frontmatter.Progress = book.Progress
|
|
|
|
frontmatter.Date = book.Date
|
|
|
|
frontmatter.Rate = book.Rate
|
|
|
|
return frontmatter
|
|
|
|
}
|