2024-04-21 13:08:09 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2024-04-21 17:51:55 +00:00
|
|
|
"os"
|
2024-05-11 16:03:35 +00:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"git.garrido.io/gabriel/mastodon-markdown-archive/client"
|
|
|
|
"git.garrido.io/gabriel/mastodon-markdown-archive/files"
|
2024-04-21 13:08:09 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-04-21 18:10:25 +00:00
|
|
|
dist := flag.String("dist", "./posts", "Path to directory where files will be written")
|
2024-04-21 13:08:09 +00:00
|
|
|
user := flag.String("user", "", "URL of User's Mastodon account whose toots will be fetched")
|
2024-04-21 17:15:36 +00:00
|
|
|
excludeReplies := flag.Bool("exclude-replies", false, "Whether or not exclude replies to other users")
|
|
|
|
excludeReblogs := flag.Bool("exclude-reblogs", false, "Whether or not to exclude reblogs")
|
|
|
|
limit := flag.Int("limit", 40, "Maximum number of posts to fetch")
|
2024-04-21 18:10:25 +00:00
|
|
|
sinceId := flag.String("since-id", "", "Fetch posts greater than this id")
|
|
|
|
maxId := flag.String("max-id", "", "Fetch posts lesser than this id")
|
|
|
|
minId := flag.String("min-id", "", "Fetch posts immediately newer than this id")
|
2024-05-11 16:03:35 +00:00
|
|
|
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")
|
2024-05-11 17:34:57 +00:00
|
|
|
templateFile := flag.String("template", "", "Template to use for post rendering, if passed")
|
2024-05-12 09:46:31 +00:00
|
|
|
threaded := flag.Bool("threaded", true, "Thread replies for a post in a single file")
|
2024-04-21 13:08:09 +00:00
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
2024-05-18 13:22:23 +00:00
|
|
|
c, err := client.New(*user, client.PostsFilter{
|
2024-04-21 17:15:36 +00:00
|
|
|
ExcludeReplies: *excludeReplies,
|
|
|
|
ExcludeReblogs: *excludeReblogs,
|
|
|
|
Limit: *limit,
|
|
|
|
SinceId: *sinceId,
|
2024-04-21 18:10:25 +00:00
|
|
|
MaxId: *maxId,
|
|
|
|
MinId: *minId,
|
2024-05-18 13:22:23 +00:00
|
|
|
}, *threaded)
|
2024-04-21 13:08:09 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
|
2024-04-21 16:12:13 +00:00
|
|
|
fileWriter, err := files.New(*dist)
|
2024-05-18 13:22:23 +00:00
|
|
|
posts := c.Posts()
|
|
|
|
postsCount := len(posts)
|
2024-04-21 16:12:13 +00:00
|
|
|
|
2024-05-18 13:22:23 +00:00
|
|
|
log.Println(fmt.Sprintf("Fetched %d posts", postsCount))
|
2024-04-21 17:51:55 +00:00
|
|
|
|
2024-04-21 13:08:09 +00:00
|
|
|
for _, post := range posts {
|
2024-05-18 13:22:23 +00:00
|
|
|
if post.ShouldSkip() {
|
2024-04-22 08:11:53 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-05-18 13:22:23 +00:00
|
|
|
if err := fileWriter.Write(post, *templateFile); err != nil {
|
2024-04-21 17:15:36 +00:00
|
|
|
log.Panicln("error writing post to file: %w", err)
|
|
|
|
break
|
|
|
|
}
|
2024-04-21 13:08:09 +00:00
|
|
|
}
|
2024-04-21 17:51:55 +00:00
|
|
|
|
2024-05-11 16:03:35 +00:00
|
|
|
if postsCount > 0 {
|
|
|
|
if *persistFirst != "" {
|
|
|
|
firstPost := posts[0]
|
|
|
|
err := persistId(firstPost.Id, *persistFirst)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if *persistLast != "" {
|
|
|
|
lastPost := posts[postsCount-1]
|
|
|
|
err := persistId(lastPost.Id, *persistLast)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func persistId(postId string, path string) error {
|
|
|
|
persistPath, err := filepath.Abs(path)
|
2024-04-21 17:51:55 +00:00
|
|
|
|
2024-05-11 16:03:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(persistPath, []byte(postId), 0644); err != nil {
|
|
|
|
return err
|
2024-04-21 17:51:55 +00:00
|
|
|
}
|
2024-05-11 16:03:35 +00:00
|
|
|
|
|
|
|
return nil
|
2024-04-21 13:08:09 +00:00
|
|
|
}
|