mastodon-markdown-archive/client/client.go

151 lines
2.9 KiB
Go
Raw Normal View History

2024-04-21 13:08:09 +00:00
package client
import (
"encoding/json"
"fmt"
"io"
"log"
2024-04-21 13:08:09 +00:00
"net/http"
"net/url"
2024-04-21 17:15:36 +00:00
"strconv"
2024-04-21 13:08:09 +00:00
"strings"
"time"
)
type Client struct {
handle string
baseURL string
}
type Account struct {
Id string `json:"id"`
}
type MediaAttachment struct {
Type string `json:"type"`
Url string `json:"url"`
Description string `json:"description"`
}
type Post struct {
CreatedAt time.Time `json:"created_at"`
Id string `json:"id"`
Visibility string `json:"visibility"`
InReplyToId string `json:"in_reply_to_id"`
URI string `json:"uri"`
Content string `json:"content"`
MediaAttachments []MediaAttachment `json:"media_attachments"`
2024-04-21 13:08:09 +00:00
}
2024-04-21 17:15:36 +00:00
type PostsFilter struct {
ExcludeReplies bool
ExcludeReblogs bool
Limit int
SinceId string
2024-04-21 18:10:25 +00:00
MinId string
MaxId string
2024-04-21 17:15:36 +00:00
}
2024-04-21 13:08:09 +00:00
func New(userURL string) (Client, error) {
var client Client
parsedURL, err := url.Parse(userURL)
if err != nil {
return client, fmt.Errorf("error parsing user url: %w", err)
}
baseURL := fmt.Sprintf("%s://%s", parsedURL.Scheme, parsedURL.Host)
acc := strings.TrimPrefix(parsedURL.Path, "/")
handle := strings.TrimPrefix(acc, "@")
return Client{
baseURL: baseURL,
handle: handle,
}, nil
}
2024-04-21 17:15:36 +00:00
func (c Client) GetPosts(filter PostsFilter) ([]Post, error) {
2024-04-21 13:08:09 +00:00
var posts []Post
account, err := c.getAccount()
if err != nil {
return posts, err
}
2024-04-21 17:15:36 +00:00
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)
}
2024-04-21 18:10:25 +00:00
if filter.MaxId != "" {
queryValues.Add("max_id", filter.MaxId)
}
if filter.MinId != "" {
queryValues.Add("min_id", filter.MinId)
}
2024-04-21 17:15:36 +00:00
queryValues.Add("limit", strconv.Itoa(filter.Limit))
query := fmt.Sprintf("?%s", queryValues.Encode())
2024-04-21 13:08:09 +00:00
postsUrl := fmt.Sprintf(
"%s/api/v1/accounts/%s/statuses/%s",
c.baseURL,
account.Id,
2024-04-21 17:15:36 +00:00
query,
2024-04-21 13:08:09 +00:00
)
log.Println(fmt.Sprintf("Fetching posts from %s", postsUrl))
2024-04-21 13:08:09 +00:00
if err := get(postsUrl, &posts); err != nil {
return posts, err
}
return posts, nil
}
func (c Client) getAccount() (Account, error) {
var account Account
lookupUrl := fmt.Sprintf(
"%s/api/v1/accounts/lookup?acct=%s",
c.baseURL,
c.handle,
)
err := get(lookupUrl, &account)
if err != nil {
return account, err
}
return account, nil
}
func get(requestUrl string, variable interface{}) error {
res, err := http.Get(requestUrl)
if err != nil {
return err
}
defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err := json.Unmarshal(body, variable); err != nil {
return err
}
return nil
}