mirror of
https://github.com/adulau/mastodon-markdown-archive.git
synced 2024-12-22 08:36:03 +00:00
Write posts and replies to markdown files
This commit is contained in:
parent
582a6a9435
commit
0cf835b6e3
5 changed files with 121 additions and 8 deletions
|
@ -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) {
|
||||
|
|
91
files/files.go
Normal file
91
files/files.go
Normal file
|
@ -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)
|
||||
}
|
||||
}
|
1
go.mod
1
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
|
||||
|
|
10
main.go
10
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)
|
||||
}
|
||||
}
|
||||
|
|
13
templates/post.tmpl
Normal file
13
templates/post.tmpl
Normal file
|
@ -0,0 +1,13 @@
|
|||
---
|
||||
date: {{ .Post.CreatedAt }}
|
||||
post_url: {{ .Post.URI }}
|
||||
post_ids:
|
||||
- {{ .Post.Id }}
|
||||
{{- range .Replies }}
|
||||
- {{ .Id }}
|
||||
{{- end }}
|
||||
---
|
||||
{{ .Content }}
|
||||
{{ range .Replies }}
|
||||
{{ .Content }}
|
||||
{{ end }}
|
Loading…
Reference in a new issue