2024-10-10 20:03:49 +02:00
|
|
|
package games
|
2024-10-09 11:12:44 +02:00
|
|
|
|
2024-10-10 20:03:49 +02:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"hugo-medialog/utils"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func LoadGames() ([]Game, error) {
|
|
|
|
gamesFile := os.Getenv("OBSIDIAN_GAMES_FILE")
|
|
|
|
fileData, err := os.ReadFile(gamesFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var games []Game
|
|
|
|
err = yaml.Unmarshal(fileData, &games)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return games, nil
|
|
|
|
}
|
|
|
|
|
2024-10-11 22:54:29 +02:00
|
|
|
func ProcessGames(games []Game, update bool) error {
|
2024-10-11 20:20:01 +02:00
|
|
|
fmt.Printf(" G A M E S\n")
|
2024-10-10 20:03:49 +02:00
|
|
|
utils.Sep()
|
|
|
|
for _, game := range games {
|
|
|
|
fmt.Printf("Title: %s\n", game.Title)
|
|
|
|
|
2024-10-11 22:54:29 +02:00
|
|
|
// If we're updating, the process is a bit different
|
2024-10-14 16:52:15 +02:00
|
|
|
if update || !game.New {
|
2024-10-11 22:54:29 +02:00
|
|
|
outputDir := os.Getenv("MARKDOWN_OUTPUT_GAMES_DIR")
|
|
|
|
mdFilePath := filepath.Join(outputDir, fmt.Sprintf("%s.md", utils.Sluggify(game.Title)))
|
|
|
|
frontmatter, content, err := utils.LoadMarkdown(mdFilePath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" ! Error loading markdown frontmatter for game %s: %v\n", game.Title, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
updatedFrontmatter := updateFrontmatterWithYAML(frontmatter, game)
|
|
|
|
err = utils.SaveUpdatedMarkdown(mdFilePath, updatedFrontmatter, content)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf(" ! Error saving updated markdown for game %s: %v\n", game.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:54:29 +02:00
|
|
|
}
|
|
|
|
|
2024-10-10 20:03:49 +02:00
|
|
|
// If we dont have ID, search movie by Title and get the ID
|
|
|
|
if game.ID == 0 {
|
|
|
|
err := SearchGameByTitle(game.Title, &game)
|
|
|
|
if err != nil {
|
2024-10-11 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error searching game by title %s: %s\n", game.Title, err)
|
2024-10-10 20:03:49 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
game.Slug = utils.Sluggify(game.Title)
|
|
|
|
|
|
|
|
// Download Images
|
|
|
|
if game.Image != "" {
|
|
|
|
suffix := ""
|
|
|
|
err := DownloadImage(game.Image, game.Slug, suffix)
|
|
|
|
if err != nil {
|
2024-10-11 21:24:46 +02:00
|
|
|
fmt.Printf(" ! Error downloading %s: %s\n", game.Image, err)
|
2024-10-10 20:03:49 +02:00
|
|
|
}
|
|
|
|
game.Image = fmt.Sprintf("%s.jpg", game.Slug)
|
|
|
|
}
|
|
|
|
if game.Poster != "" {
|
|
|
|
suffix := "-poster"
|
|
|
|
err := DownloadImage(game.Poster, game.Slug, suffix)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error downloading %s: %s\n", game.Image, err)
|
|
|
|
}
|
|
|
|
game.Poster = fmt.Sprintf("%s%s.jpg", game.Slug, suffix)
|
|
|
|
}
|
|
|
|
if game.Background != "" {
|
|
|
|
suffix := "-background"
|
|
|
|
err := DownloadImage(game.Background, game.Slug, suffix)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error downloading %s: %s\n", game.Background, err)
|
|
|
|
}
|
|
|
|
game.Background = fmt.Sprintf("%s%s.jpg", game.Slug, suffix)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := generateGameMarkdown(game)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
utils.Sep()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateGameMarkdown(game Game) error {
|
|
|
|
templatePath := filepath.Join(os.Getenv("TEMPLATES_DIR"), "game.md.tpl")
|
|
|
|
outputDir := os.Getenv("MARKDOWN_OUTPUT_GAMES_DIR")
|
|
|
|
if err := utils.CreateDirIfNotExists(outputDir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
outputPath := filepath.Join(outputDir, fmt.Sprintf("%s.md", game.Slug))
|
|
|
|
|
|
|
|
data := map[string]interface{}{
|
|
|
|
"Title": game.Title,
|
|
|
|
"Platform": game.Platform,
|
|
|
|
"Link": game.Link,
|
|
|
|
"Subtitle": game.Year,
|
|
|
|
"Year": game.Year,
|
|
|
|
"Rate": game.Rate,
|
|
|
|
"Progress": game.Progress,
|
|
|
|
"Image": game.Image,
|
|
|
|
"Poster": game.Poster,
|
|
|
|
"Background": game.Background,
|
|
|
|
"Date": game.Date,
|
|
|
|
"Tags": "playing",
|
|
|
|
}
|
|
|
|
|
|
|
|
return utils.GenerateMarkdown(templatePath, outputPath, data)
|
|
|
|
}
|
2024-10-11 22:54:29 +02:00
|
|
|
|
|
|
|
// Helper function to update only YAML fields that exist in both the frontmatter and YAML data
|
|
|
|
func updateFrontmatterWithYAML(frontmatter utils.FrontMatter, game Game) utils.FrontMatter {
|
|
|
|
frontmatter.Progress = game.Progress
|
|
|
|
frontmatter.Date = game.Date
|
|
|
|
frontmatter.Rate = game.Rate
|
|
|
|
return frontmatter
|
|
|
|
}
|