From 1868600b985be637ff88ab04067df52d05a0d285 Mon Sep 17 00:00:00 2001 From: Gabriel Garrido Date: Sun, 21 Apr 2024 19:15:36 +0200 Subject: [PATCH] Use flags for query parameters --- .gitignore | 1 + README.md | 6 +++--- client/client.go | 30 ++++++++++++++++++++++++++++-- main.go | 18 +++++++++++++++--- 4 files changed, 47 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index adf8f72..df69af2 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ # Go workspace file go.work +example \ No newline at end of file diff --git a/README.md b/README.md index 9c188e4..954ce12 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,13 @@ # Mastodon PESOS -Fetch a Mastodon account's posts and save them as Markdown files. For the time being this works with [Hugo](https://gohugo.io). +Fetch a Mastodon account's posts and save them as Markdown files. For the time being this generates the files formatted as [Hugo](https://gohugo.io) markdown files. Use this to syndicate Mastodon posts to your own site. See IndieWeb's guide on [PESOS](https://indieweb.org/PESOS) for more information. ## To do - [x] Fetch Mastodon account id - [x] Fetch Mastodon posts -- [ ] Support filtering posts using Mastodon's [query parameters](https://docs.joinmastodon.org/methods/accounts/#query-parameters) - [x] Save posts to files - [x] Transform HTML to Markdown -- [x] Show embedded images \ No newline at end of file +- [x] Show embedded images +- [x] Support filtering posts using Mastodon's [query parameters](https://docs.joinmastodon.org/methods/accounts/#query-parameters) diff --git a/client/client.go b/client/client.go index ed86bcd..06c1881 100644 --- a/client/client.go +++ b/client/client.go @@ -6,6 +6,7 @@ import ( "io" "net/http" "net/url" + "strconv" "strings" "time" ) @@ -35,6 +36,13 @@ type Post struct { MediaAttachments []MediaAttachment `json:"media_attachments"` } +type PostsFilter struct { + ExcludeReplies bool + ExcludeReblogs bool + Limit int + SinceId string +} + func New(userURL string) (Client, error) { var client Client parsedURL, err := url.Parse(userURL) @@ -53,7 +61,7 @@ func New(userURL string) (Client, error) { }, nil } -func (c Client) GetPosts(params string) ([]Post, error) { +func (c Client) GetPosts(filter PostsFilter) ([]Post, error) { var posts []Post account, err := c.getAccount() @@ -61,11 +69,29 @@ func (c Client) GetPosts(params string) ([]Post, error) { return posts, err } + queryValues := url.Values{} + + if filter.ExcludeReplies { + queryValues.Add("exclude_replies", strconv.Itoa(1)) + } + + if filter.ExcludeReblogs { + queryValues.Add("exclude_reblogs", strconv.Itoa(1)) + } + + if filter.SinceId != "" { + queryValues.Add("since_id", filter.SinceId) + } + + queryValues.Add("limit", strconv.Itoa(filter.Limit)) + + query := fmt.Sprintf("?%s", queryValues.Encode()) + postsUrl := fmt.Sprintf( "%s/api/v1/accounts/%s/statuses/%s", c.baseURL, account.Id, - params, + query, ) if err := get(postsUrl, &posts); err != nil { diff --git a/main.go b/main.go index fecb02c..63e280b 100644 --- a/main.go +++ b/main.go @@ -11,16 +11,25 @@ import ( 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") + 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") + sinceId := flag.String("since-id", "", "Fetch only posts made since passed post id") flag.Parse() - client, err := client.New(*user) + c, err := client.New(*user) if err != nil { log.Panicln(fmt.Errorf("error instantiating client: %w", err)) } - posts, err := client.GetPosts("?exclude_replies=1&exclude_reblogs=1&limit=10") + posts, err := c.GetPosts(client.PostsFilter{ + ExcludeReplies: *excludeReplies, + ExcludeReblogs: *excludeReblogs, + Limit: *limit, + SinceId: *sinceId, + }) if err != nil { log.Panicln(err) @@ -33,6 +42,9 @@ func main() { } for _, post := range posts { - fileWriter.Write(post) + if err := fileWriter.Write(post); err != nil { + log.Panicln("error writing post to file: %w", err) + break + } } }