Use flags for query parameters

This commit is contained in:
Gabriel Garrido 2024-04-21 19:15:36 +02:00
parent be79b0333a
commit 1868600b98
4 changed files with 47 additions and 8 deletions

1
.gitignore vendored
View file

@ -21,3 +21,4 @@
# Go workspace file
go.work
example

View file

@ -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
- [x] Show embedded images
- [x] Support filtering posts using Mastodon's [query parameters](https://docs.joinmastodon.org/methods/accounts/#query-parameters)

View file

@ -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 {

18
main.go
View file

@ -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
}
}
}