From 5af659241f1cf8545040ccd0b3421efc6741d0b2 Mon Sep 17 00:00:00 2001 From: Gabriel Garrido Date: Sun, 12 May 2024 11:46:31 +0200 Subject: [PATCH] Updates to threading, structs, and template * Extend Mastodon structs * Make threading optional * Include tags in default template * Add function to get all tags * Update spacing in template * Update default template front-matter --- client/client.go | 79 ++++++++++++++++++++++++++++++++++----- files/files.go | 8 ++-- files/templates/post.tmpl | 32 ++++++++++------ main.go | 3 +- 4 files changed, 98 insertions(+), 24 deletions(-) diff --git a/client/client.go b/client/client.go index bd3c665..5f7c7c7 100644 --- a/client/client.go +++ b/client/client.go @@ -18,25 +18,70 @@ type Client struct { } type Account struct { - Id string `json:"id"` + Id string `json:"id"` + Username string `json:"username"` + Acct string `json:"acct"` + DisplayName string `json:"display_name"` + Locked bool `json:"locked"` + Bot bool `json:"bot"` + Discoverable bool `json:"discoverable"` + Group bool `json:"group"` + CreatedAt time.Time `json:"created_at"` + Note string `json:"note"` + URL string `json:"url"` + URI string `json:"uri"` + Avatar string `json:"avatar"` + AvatarStatic string `json:"avatar_static"` + Header string `json:"header"` + HeaderStatic string `json:"header_static"` + FollowersCount int `json:"followers_count"` + FollowingCount int `json:"following_count"` + StatusesCount int `json:"statuses_count"` + LastStatusAt string `json:"last_status_at"` } type MediaAttachment struct { Type string `json:"type"` - Url string `json:"url"` + URL string `json:"url"` Description string `json:"description"` Id string `json:"id"` Path string } +type Application struct { + Name string `json:"name"` + Website string `json:"website"` +} + +type Tag struct { + Name string `json:"name"` + URL string `json:"url"` +} + type Post struct { - 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"` + CreatedAt time.Time `json:"created_at"` + Id string `json:"id"` + Visibility string `json:"visibility"` + InReplyToId string `json:"in_reply_to_id"` + InReplyToAccountId string `json:"in_reply_to_account_id"` + Sensitive bool `json:"sensitive"` + SpoilerText string `json:"spoiler_text"` + Language string `json:"language"` + URI string `json:"uri"` + URL string `json:"url"` + Application Application `json:"application"` + Content string `json:"content"` + MediaAttachments []MediaAttachment `json:"media_attachments"` + RepliesCount int `json:"replies_count"` + ReblogsCount int `json:"reblogs_count"` + FavoritesCount int `json:"favourites_count"` + Pinned bool `json:"pinned"` + Tags []Tag `json:"tags"` + Favourited bool `json:"favourited"` + Reblogged bool `json:"reblogged"` + Muted bool `json:"muted"` + Bookmarked bool `json:"bookmarked"` + Account Account `json:"account"` } type PostsFilter struct { @@ -133,6 +178,22 @@ func (c Client) getAccount() (Account, error) { return account, nil } +func TagsForPost(post Post, descendants []Post) []Tag { + var tags []Tag + + for _, tag := range post.Tags { + tags = append(tags, tag) + } + + for _, descendant := range descendants { + for _, tag := range descendant.Tags { + tags = append(tags, tag) + } + } + + return tags +} + func get(requestUrl string, variable interface{}) error { res, err := http.Get(requestUrl) diff --git a/files/files.go b/files/files.go index 55d77e1..9b61434 100644 --- a/files/files.go +++ b/files/files.go @@ -25,6 +25,7 @@ type FileWriter struct { type TemplateContext struct { Post client.Post Descendants []client.Post + Tags []client.Tag } func New(dir string) (FileWriter, error) { @@ -47,8 +48,8 @@ func New(dir string) (FileWriter, error) { }, nil } -func (f FileWriter) Write(post client.Post, templateFile string) error { - if post.InReplyToId != "" { +func (f FileWriter) Write(post client.Post, threaded bool, templateFile string) error { + if threaded && post.InReplyToId != "" { f.repies[post.InReplyToId] = post return nil } @@ -73,7 +74,7 @@ func (f FileWriter) Write(post client.Post, templateFile string) error { continue } - imageFilename, err := downloadAttachment(dir, media.Id, media.Url) + imageFilename, err := downloadAttachment(dir, media.Id, media.URL) if err != nil { return err @@ -96,6 +97,7 @@ func (f FileWriter) Write(post client.Post, templateFile string) error { context := TemplateContext{ Post: post, Descendants: descendants, + Tags: client.TagsForPost(post, descendants), } err = tmpl.Execute(file, context) diff --git a/files/templates/post.tmpl b/files/templates/post.tmpl index 042b7c3..15f01ab 100644 --- a/files/templates/post.tmpl +++ b/files/templates/post.tmpl @@ -1,26 +1,36 @@ --- date: {{ .Post.CreatedAt }} -post_url: {{ .Post.URI }} -post_ids: -- {{ .Post.Id }} +{{- if .Post.InReplyToId }} +in_reply_to: {{ .Post.InReplyToId }} +{{- end }} +post_uri: {{ .Post.URI }} +post_id: {{ .Post.Id }} +{{- if len .Tags }} +tags: +{{- range .Tags }} +- {{ .Name }} +{{- end }} +{{- end }} +{{- if len .Descendants }} +descendants: {{- range .Descendants }} - {{ .Id }} {{- end }} +{{- end }} --- - {{ .Post.Content | tomd }} -{{ range .Post.MediaAttachments }} - {{- if eq .Type "image" -}} +{{- range .Post.MediaAttachments }} +{{ if eq .Type "image" }} ![{{ .Description }}]({{ .Path }}) - {{ end }} +{{ end }} {{- end -}} {{- range .Descendants }} {{ .Content | tomd -}} - {{ range .MediaAttachments }} - {{- if eq .Type "image" -}} +{{- range .MediaAttachments }} +{{ if eq .Type "image" }} ![{{ .Description }}]({{ .Path }}) - {{ end }} - {{- end -}} +{{ end }} +{{- end -}} {{- end -}} diff --git a/main.go b/main.go index dda55fa..34b9358 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ func main() { persistFirst := flag.String("persist-first", "", "Location to persist the post id of the first post returned") persistLast := flag.String("persist-last", "", "Location to persist the post id of the last post returned") templateFile := flag.String("template", "", "Template to use for post rendering, if passed") + threaded := flag.Bool("threaded", true, "Thread replies for a post in a single file") flag.Parse() @@ -58,7 +59,7 @@ func main() { continue } - if err := fileWriter.Write(post, *templateFile); err != nil { + if err := fileWriter.Write(post, *threaded, *templateFile); err != nil { log.Panicln("error writing post to file: %w", err) break }