mirror of
https://github.com/adulau/mastodon-markdown-archive.git
synced 2024-11-24 02:47:08 +00:00
Use flags for query parameters
This commit is contained in:
parent
be79b0333a
commit
1868600b98
4 changed files with 47 additions and 8 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -21,3 +21,4 @@
|
|||
# Go workspace file
|
||||
go.work
|
||||
|
||||
example
|
|
@ -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)
|
||||
|
|
|
@ -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
18
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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue