From 0cf835b6e36a515aeaa959e325bf69497e72ca81 Mon Sep 17 00:00:00 2001 From: Gabriel Garrido Date: Sun, 21 Apr 2024 18:12:13 +0200 Subject: [PATCH] Write posts and replies to markdown files --- client/client.go | 14 +++---- files/files.go | 91 +++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + main.go | 10 ++++- templates/post.tmpl | 13 +++++++ 5 files changed, 121 insertions(+), 8 deletions(-) create mode 100644 files/files.go create mode 100644 templates/post.tmpl diff --git a/client/client.go b/client/client.go index cd83d37..ed86bcd 100644 --- a/client/client.go +++ b/client/client.go @@ -26,13 +26,13 @@ type MediaAttachment struct { } type Post struct { - CreatedAt time.Time `json:"created_at"` - Id string `json:"id"` - Visibility string `json:"visibility"` - InReplyToAccountId string `json:"in_reply_to_account_id"` - URI string `json:"uri"` - Content string `json:"content"` - MediaAttachments []MediaAttachment `json:"media_attachments"` + CreatedAt time.Time `json:"created_at"` + Id string `json:"id"` + Visibility string `json:"visibility"` + InReplyToId string `json:"in_reply_to_id"` + URI string `json:"uri"` + Content string `json:"content"` + MediaAttachments []MediaAttachment `json:"media_attachments"` } func New(userURL string) (Client, error) { diff --git a/files/files.go b/files/files.go new file mode 100644 index 0000000..4136892 --- /dev/null +++ b/files/files.go @@ -0,0 +1,91 @@ +package files + +import ( + "fmt" + "git.hq.ggpsv.com/gabriel/mastodon-pesos/client" + "os" + "path/filepath" + "text/template" +) + +type FileWriter struct { + dir string + repies map[string]client.Post +} + +type TemplateContext struct { + Post client.Post + Content string + Replies []client.Post +} + +func New(dir string) (FileWriter, error) { + var fileWriter FileWriter + _, err := os.Stat(dir) + + if os.IsNotExist(err) { + os.Mkdir(dir, os.ModePerm) + } + + absDir, err := filepath.Abs(dir) + + if err != nil { + return fileWriter, err + } + + return FileWriter{ + dir: absDir, + repies: make(map[string]client.Post), + }, nil +} + +func (f FileWriter) Write(post client.Post) { + tpmlFilename := "templates/post.tmpl" + tmplFile, err := filepath.Abs(tpmlFilename) + + if err != nil { + panic(err) + } + + if post.InReplyToId != "" { + f.repies[post.InReplyToId] = post + return + } + + var descendants []client.Post + f.getReplies(post.Id, &descendants) + + tmpl, err := template.New(filepath.Base(tpmlFilename)).ParseFiles(tmplFile) + + if err != nil { + panic(err) + } + + name := fmt.Sprintf("%s.md", post.Id) + filename := filepath.Join(f.dir, name) + file, err := os.Create(filename) + + if err != nil { + panic(err) + } + + defer file.Close() + + context := TemplateContext{ + Post: post, + Content: post.Content, + Replies: descendants, + } + err = tmpl.Execute(file, context) + + if err != nil { + panic(err) + } +} + +func (f FileWriter) getReplies(postId string, replies *[]client.Post) { + if reply, ok := f.repies[postId]; ok { + *replies = append(*replies, reply) + f.getReplies(reply.Id, replies) + } +} diff --git a/go.mod b/go.mod index 238e96a..7a070b0 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,4 @@ module git.hq.ggpsv.com/gabriel/mastodon-pesos go 1.21.6 replace git.hq.ggpsv.com/gabriel/mastodon-pesos/client => ./client +replace git.hq.ggpsv.com/gabriel/mastodon-pesos/files => ./files diff --git a/main.go b/main.go index 31eb110..fecb02c 100644 --- a/main.go +++ b/main.go @@ -4,10 +4,12 @@ import ( "flag" "fmt" "git.hq.ggpsv.com/gabriel/mastodon-pesos/client" + "git.hq.ggpsv.com/gabriel/mastodon-pesos/files" "log" ) func main() { + dist := flag.String("dist", "", "Path to directory where files will be written") user := flag.String("user", "", "URL of User's Mastodon account whose toots will be fetched") flag.Parse() @@ -24,7 +26,13 @@ func main() { log.Panicln(err) } + fileWriter, err := files.New(*dist) + + if err != nil { + log.Panicln(err) + } + for _, post := range posts { - log.Println(post.Id) + fileWriter.Write(post) } } diff --git a/templates/post.tmpl b/templates/post.tmpl new file mode 100644 index 0000000..6d9f28e --- /dev/null +++ b/templates/post.tmpl @@ -0,0 +1,13 @@ +--- +date: {{ .Post.CreatedAt }} +post_url: {{ .Post.URI }} +post_ids: +- {{ .Post.Id }} +{{- range .Replies }} +- {{ .Id }} +{{- end }} +--- +{{ .Content }} +{{ range .Replies }} +{{ .Content }} +{{ end }}