Implement --persist behavior and add logs

This commit is contained in:
Gabriel Garrido 2024-04-21 19:51:55 +02:00
parent 1868600b98
commit b97d050c93
2 changed files with 14 additions and 0 deletions

View file

@ -4,6 +4,7 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -94,6 +95,8 @@ func (c Client) GetPosts(filter PostsFilter) ([]Post, error) {
query, query,
) )
log.Println(fmt.Sprintf("Fetching posts from %s", postsUrl))
if err := get(postsUrl, &posts); err != nil { if err := get(postsUrl, &posts); err != nil {
return posts, err return posts, err
} }

11
main.go
View file

@ -6,6 +6,7 @@ import (
"git.hq.ggpsv.com/gabriel/mastodon-pesos/client" "git.hq.ggpsv.com/gabriel/mastodon-pesos/client"
"git.hq.ggpsv.com/gabriel/mastodon-pesos/files" "git.hq.ggpsv.com/gabriel/mastodon-pesos/files"
"log" "log"
"os"
) )
func main() { func main() {
@ -15,6 +16,7 @@ func main() {
excludeReblogs := flag.Bool("exclude-reblogs", false, "Whether or not to exclude reblogs") excludeReblogs := flag.Bool("exclude-reblogs", false, "Whether or not to exclude reblogs")
limit := flag.Int("limit", 40, "Maximum number of posts to fetch") limit := flag.Int("limit", 40, "Maximum number of posts to fetch")
sinceId := flag.String("since-id", "", "Fetch only posts made since passed post id") sinceId := flag.String("since-id", "", "Fetch only posts made since passed post id")
persist := flag.Bool("persist", false, "Persist most recent post id to /tmp/mastodon-pesos-fid")
flag.Parse() flag.Parse()
@ -41,10 +43,19 @@ func main() {
log.Panicln(err) log.Panicln(err)
} }
log.Println(fmt.Sprintf("Fetched %d posts", len(posts)))
for _, post := range posts { for _, post := range posts {
if err := fileWriter.Write(post); err != nil { if err := fileWriter.Write(post); err != nil {
log.Panicln("error writing post to file: %w", err) log.Panicln("error writing post to file: %w", err)
break break
} }
} }
if *persist && len(posts) > 0 {
lastPost := posts[0]
fid := []byte(lastPost.Id)
os.WriteFile("/tmp/mastodon-pesos-fid", fid, 0644)
}
} }