scripts/tmdb-export/markdown.go

86 lines
1.6 KiB
Go
Raw Normal View History

2024-05-23 10:59:13 +02:00
package main
import (
"fmt"
"os"
"strings"
"text/template"
2024-05-23 11:56:45 +02:00
"time"
2024-05-23 10:59:13 +02:00
)
const (
markdownTemplate = `---
tmdb: {{ .TMDBID }}
emdb: {{ .IMDBID }}
englishTitle: {{ .EnglishTitle }}
title: {{ .Title }}
year: {{ .Year }}
2024-05-23 11:56:45 +02:00
runtime: {{ .Runtime }}
2024-05-23 10:59:13 +02:00
directors: {{ .DirectorsYAML }}
inCollection: no
watchedOn:
rating:
---
# {{ .EnglishTitle }} ({{ .Year }})
Director(s): {{ .Directors }}
{{ .Summary }}
## Comment
`
)
func Export(movie Movie) error {
tpl, err := template.New("page").Parse(markdownTemplate)
if err != nil {
return err
}
filename := fmt.Sprintf("%s (%d).md", movie.EnglishTitle, movie.Year)
f, err := os.Create(filename)
if err != nil {
return err
}
2024-05-23 11:56:45 +02:00
runtime := time.Duration(movie.RunTime) * time.Minute
runtimeStr, _, ok := strings.Cut(runtime.String(), "m")
if !ok {
return fmt.Errorf("could not parse runtime format %s", runtime)
}
runtimeStr = fmt.Sprintf("%sm", runtimeStr)
2024-05-23 10:59:13 +02:00
data := struct {
TMDBID string
IMDBID string
EnglishTitle string
Title string
Year int
2024-05-23 11:56:45 +02:00
Runtime string
2024-05-23 10:59:13 +02:00
DirectorsYAML string
Directors string
Summary string
}{
TMDBID: movie.TMDBID,
IMDBID: movie.IMDBID,
EnglishTitle: movie.EnglishTitle,
Title: movie.Title,
Year: movie.Year,
2024-05-23 11:56:45 +02:00
Runtime: runtimeStr,
2024-05-23 10:59:13 +02:00
DirectorsYAML: strings.Join(movie.Directors, ", "),
Directors: fmt.Sprintf("[[%s]]", strings.Join(movie.Directors, "]], [[")),
Summary: movie.Summary,
}
if err := tpl.Execute(f, data); err != nil {
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}