mastodon-markdown-archive/main.go

70 lines
1.8 KiB
Go
Raw Normal View History

2024-04-21 13:08:09 +00:00
package main
import (
"flag"
"fmt"
2024-04-23 08:31:17 +00:00
"git.garrido.io/gabriel/mastodon-pesos/client"
"git.garrido.io/gabriel/mastodon-pesos/files"
2024-04-21 13:08:09 +00:00
"log"
"os"
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")
persist := flag.Bool("persist", false, "Persist most recent post id to /tmp/mastodon-pesos-fid")
2024-04-21 13:08:09 +00:00
flag.Parse()
2024-04-21 17:15:36 +00:00
c, err := client.New(*user)
2024-04-21 13:08:09 +00:00
if err != nil {
log.Panicln(fmt.Errorf("error instantiating client: %w", err))
}
2024-04-21 17:15:36 +00:00
posts, err := c.GetPosts(client.PostsFilter{
ExcludeReplies: *excludeReplies,
ExcludeReblogs: *excludeReblogs,
Limit: *limit,
SinceId: *sinceId,
2024-04-21 18:10:25 +00:00
MaxId: *maxId,
MinId: *minId,
2024-04-21 17:15:36 +00:00
})
2024-04-21 13:08:09 +00:00
if err != nil {
log.Panicln(err)
}
fileWriter, err := files.New(*dist)
if err != nil {
log.Panicln(err)
}
log.Println(fmt.Sprintf("Fetched %d posts", len(posts)))
2024-04-21 13:08:09 +00:00
for _, post := range posts {
2024-04-22 08:11:53 +00:00
if client.ShouldSkipPost(post) {
continue
}
2024-04-21 17:15:36 +00:00
if err := fileWriter.Write(post); err != nil {
log.Panicln("error writing post to file: %w", err)
break
}
2024-04-21 13:08:09 +00:00
}
if *persist && len(posts) > 0 {
lastPost := posts[0]
fid := []byte(lastPost.Id)
os.WriteFile("/tmp/mastodon-pesos-fid", fid, 0644)
}
2024-04-21 13:08:09 +00:00
}