hugo-medialog/internal/music/controller.go

114 lines
3.0 KiB
Go
Raw Normal View History

2024-10-11 13:05:46 +02:00
package music
2024-10-09 11:12:44 +02:00
2024-10-11 13:05:46 +02:00
import (
"fmt"
"hugo-medialog/utils"
"os"
"path/filepath"
"gopkg.in/yaml.v2"
)
func LoadAlbums() ([]Album, error) {
albumsFile := os.Getenv("OBSIDIAN_MUSIC_FILE")
fileData, err := os.ReadFile(albumsFile)
if err != nil {
return nil, err
}
var albums []Album
err = yaml.Unmarshal(fileData, &albums)
if err != nil {
return nil, err
}
return albums, nil
}
2024-10-14 13:48:49 +02:00
func ProcessAlbum(albumList []Album, update bool) error {
2024-10-24 17:16:17 +02:00
utils.Sep()
2024-10-11 20:20:01 +02:00
fmt.Printf(" M U S I C\n")
utils.Sep()
2024-10-11 13:05:46 +02:00
for _, album := range albumList {
fmt.Printf("Title: %s\n", album.Title)
2024-10-14 13:48:49 +02:00
// If we're updating, the process is a bit different
if update || !album.New {
2024-10-14 13:48:49 +02:00
outputDir := os.Getenv("MARKDOWN_OUTPUT_MUSIC_DIR")
mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(album.Title)))
frontmatter, content, err := utils.LoadMarkdown(mdFilePath)
if err != nil {
fmt.Printf(" ! Error loading markdown frontmatter for album %s: %v\n", album.Title, err)
continue
}
updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, album)
err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content)
if err != nil {
fmt.Printf(" ! Error saving updated markdown for album %s: %v\n", album.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
}
2024-10-11 13:05:46 +02:00
// If we dont have ID, search album by Title and get the ID
if album.ID == "" {
err := SearchAlbumByTitle(album.Title, &album)
if err != nil {
2024-10-11 21:24:46 +02:00
fmt.Printf(" ! Error searching album by title %s: %s\n", album.Title, err)
2024-10-11 13:05:46 +02:00
continue
}
}
album.Slug = utils.Sluggify(album.Title)
// Now we need to get the image
if album.Image != "" {
err := DownloadImage(album.Image, album.Slug)
if err != nil {
2024-10-11 21:24:46 +02:00
fmt.Printf(" ! Error downloading %s: %s\n", album.Image, err)
2024-10-11 13:05:46 +02:00
}
}
album.Image = fmt.Sprintf("%s.jpg", album.Slug)
err := generateAlbumMarkdown(album)
if err != nil {
return err
}
utils.Sep()
}
return nil
}
func generateAlbumMarkdown(album Album) error {
templatePath := filepath.Join(os.Getenv("TEMPLATES_DIR"), "music.md.tpl")
outputDir := os.Getenv("MARKDOWN_OUTPUT_MUSIC_DIR")
if err := utils.CreateDirIfNotExists(outputDir); err != nil {
return err
}
outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", album.Slug))
data := map[string]interface{}{
"Title": album.Title,
"Artist": album.Artist,
"Link": album.Link,
"Subtitle": album.Year,
"Year": album.Year,
"Rate": album.Rate,
"Tracks": album.Tracks,
"Image": album.Image,
"Date": album.Date,
"Tags": "listening",
}
return utils.GenerateMarkdown(templatePath, outputPath, data)
}
2024-10-14 13:48:49 +02:00
// Helper function to update only YAML fields that exist in both the frontmatter and YAML data
func updateFrontmatterWithYAML(frontmatter utils.FrontMatter, album Album) utils.FrontMatter {
frontmatter.Date = album.Date
frontmatter.Rate = album.Rate
return frontmatter
}