2017-04-13 09:47:00 +02:00
|
|
|
package mastodon
|
|
|
|
|
|
|
|
import (
|
2017-04-17 06:54:36 +02:00
|
|
|
"bytes"
|
2017-04-17 04:10:29 +02:00
|
|
|
"context"
|
2017-04-13 09:47:00 +02:00
|
|
|
"encoding/json"
|
2017-04-17 06:54:36 +02:00
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
2017-04-13 09:47:00 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2017-04-17 06:54:36 +02:00
|
|
|
"os"
|
2017-04-13 19:16:52 +02:00
|
|
|
"path"
|
2017-04-17 06:54:36 +02:00
|
|
|
"path/filepath"
|
2017-04-13 09:47:00 +02:00
|
|
|
"strings"
|
2017-04-18 10:11:49 +02:00
|
|
|
"time"
|
2017-04-13 09:47:00 +02:00
|
|
|
)
|
|
|
|
|
2017-04-14 05:21:27 +02:00
|
|
|
// Config is a setting for access mastodon APIs.
|
2017-04-13 09:47:00 +02:00
|
|
|
type Config struct {
|
|
|
|
Server string
|
|
|
|
ClientID string
|
|
|
|
ClientSecret string
|
|
|
|
AccessToken string
|
|
|
|
}
|
|
|
|
|
2017-04-14 05:21:27 +02:00
|
|
|
// Client is a API client for mastodon.
|
|
|
|
type Client struct {
|
2017-04-13 09:47:00 +02:00
|
|
|
http.Client
|
2017-04-18 10:11:49 +02:00
|
|
|
config *Config
|
|
|
|
interval time.Duration
|
2017-04-13 09:47:00 +02:00
|
|
|
}
|
|
|
|
|
2017-04-18 10:08:48 +02:00
|
|
|
type page struct {
|
|
|
|
next string
|
|
|
|
}
|
|
|
|
|
|
|
|
func linkHeader(h http.Header, rel string) []string {
|
|
|
|
var links []string
|
|
|
|
for _, v := range h["Link"] {
|
2017-04-19 10:48:42 +02:00
|
|
|
var p string
|
|
|
|
for len(v) > 0 {
|
|
|
|
i := strings.Index(v, ";")
|
|
|
|
if i < 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
e := i
|
|
|
|
i++
|
|
|
|
for i < len(v) {
|
|
|
|
if v[i] != ' ' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
p = strings.TrimSpace(v[i:])
|
2017-04-18 10:08:48 +02:00
|
|
|
if !strings.HasPrefix(p, "rel=") {
|
2017-04-19 10:48:42 +02:00
|
|
|
break
|
2017-04-18 10:08:48 +02:00
|
|
|
}
|
2017-04-19 10:48:42 +02:00
|
|
|
i += 4
|
2017-04-18 10:08:48 +02:00
|
|
|
pos := strings.Index(p[4:], `,`)
|
|
|
|
if pos > 0 {
|
|
|
|
p = p[4 : 4+pos]
|
2017-04-19 10:48:42 +02:00
|
|
|
i += pos
|
|
|
|
} else {
|
|
|
|
p = p[4:]
|
|
|
|
i = len(v) - 1
|
|
|
|
}
|
|
|
|
if k := strings.Trim(p, `"`); k == rel {
|
|
|
|
links = append(links, strings.Trim(v[:e], "<>"))
|
2017-04-18 10:08:48 +02:00
|
|
|
}
|
2017-04-19 10:48:42 +02:00
|
|
|
i++
|
|
|
|
for i < len(v) {
|
|
|
|
if v[i] != ' ' {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
i++
|
2017-04-18 10:08:48 +02:00
|
|
|
}
|
2017-04-19 10:48:42 +02:00
|
|
|
v = v[i:]
|
2017-04-18 10:08:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return links
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) doAPI(ctx context.Context, method string, uri string, params interface{}, res interface{}, next *bool) error {
|
2017-04-14 21:36:27 +02:00
|
|
|
u, err := url.Parse(c.config.Server)
|
2017-04-14 05:12:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-14 21:36:27 +02:00
|
|
|
u.Path = path.Join(u.Path, uri)
|
2017-04-14 05:12:09 +02:00
|
|
|
|
2017-04-17 06:54:36 +02:00
|
|
|
var req *http.Request
|
|
|
|
ct := "application/x-www-form-urlencoded"
|
|
|
|
if values, ok := params.(url.Values); ok {
|
2017-04-17 18:59:52 +02:00
|
|
|
var body io.Reader
|
|
|
|
if method == http.MethodGet {
|
|
|
|
u.RawQuery = values.Encode()
|
|
|
|
} else {
|
|
|
|
body = strings.NewReader(values.Encode())
|
|
|
|
}
|
|
|
|
req, err = http.NewRequest(method, u.String(), body)
|
2017-04-17 06:54:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if file, ok := params.(string); ok {
|
|
|
|
f, err := os.Open(file)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
mw := multipart.NewWriter(&buf)
|
|
|
|
part, err := mw.CreateFormFile("file", filepath.Base(file))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = io.Copy(part, f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = mw.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req, err = http.NewRequest(method, u.String(), &buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ct = mw.FormDataContentType()
|
|
|
|
} else {
|
|
|
|
req, err = http.NewRequest(method, u.String(), nil)
|
2017-04-20 14:29:10 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-14 05:12:09 +02:00
|
|
|
}
|
2017-04-19 11:26:12 +02:00
|
|
|
req = req.WithContext(ctx)
|
2017-04-14 05:12:09 +02:00
|
|
|
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
|
2017-04-15 17:47:23 +02:00
|
|
|
if params != nil {
|
2017-04-17 06:54:36 +02:00
|
|
|
req.Header.Set("Content-Type", ct)
|
2017-04-15 17:47:23 +02:00
|
|
|
}
|
2017-04-14 10:37:50 +02:00
|
|
|
|
2017-04-17 05:25:20 +02:00
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2017-04-14 10:37:50 +02:00
|
|
|
|
2017-04-18 10:08:48 +02:00
|
|
|
if next != nil && params != nil {
|
|
|
|
nl := linkHeader(resp.Header, "next")
|
|
|
|
*next = false
|
|
|
|
if len(nl) > 0 {
|
|
|
|
u, err = url.Parse(nl[0])
|
|
|
|
if err == nil {
|
|
|
|
for k, v := range u.Query() {
|
|
|
|
params.(url.Values)[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*next = true
|
|
|
|
}
|
|
|
|
}
|
2017-04-17 05:25:20 +02:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-04-19 07:32:53 +02:00
|
|
|
return parseAPIError("bad request", resp)
|
2017-04-17 05:25:20 +02:00
|
|
|
} else if res == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return json.NewDecoder(resp.Body).Decode(&res)
|
2017-04-14 05:12:09 +02:00
|
|
|
}
|
|
|
|
|
2017-04-14 05:21:27 +02:00
|
|
|
// NewClient return new mastodon API client.
|
|
|
|
func NewClient(config *Config) *Client {
|
|
|
|
return &Client{
|
2017-04-18 10:11:49 +02:00
|
|
|
Client: *http.DefaultClient,
|
|
|
|
config: config,
|
|
|
|
interval: 10 * time.Second,
|
2017-04-13 09:47:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 05:21:27 +02:00
|
|
|
// Authenticate get access-token to the API.
|
2017-04-17 04:10:29 +02:00
|
|
|
func (c *Client) Authenticate(ctx context.Context, username, password string) error {
|
2017-04-13 09:47:00 +02:00
|
|
|
params := url.Values{}
|
|
|
|
params.Set("client_id", c.config.ClientID)
|
|
|
|
params.Set("client_secret", c.config.ClientSecret)
|
|
|
|
params.Set("grant_type", "password")
|
|
|
|
params.Set("username", username)
|
|
|
|
params.Set("password", password)
|
2017-04-13 19:16:52 +02:00
|
|
|
params.Set("scope", "read write follow")
|
|
|
|
|
2017-04-14 21:36:27 +02:00
|
|
|
u, err := url.Parse(c.config.Server)
|
2017-04-13 19:16:52 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-14 21:36:27 +02:00
|
|
|
u.Path = path.Join(u.Path, "/oauth/token")
|
2017-04-13 19:16:52 +02:00
|
|
|
|
2017-04-14 21:36:27 +02:00
|
|
|
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(params.Encode()))
|
2017-04-13 09:47:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-19 11:26:12 +02:00
|
|
|
req = req.WithContext(ctx)
|
2017-04-14 10:10:13 +02:00
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
2017-04-13 09:47:00 +02:00
|
|
|
resp, err := c.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2017-04-14 11:19:12 +02:00
|
|
|
if resp.StatusCode != http.StatusOK {
|
2017-04-19 07:32:53 +02:00
|
|
|
return parseAPIError("bad authorization", resp)
|
2017-04-14 10:10:13 +02:00
|
|
|
}
|
|
|
|
|
2017-04-13 09:47:00 +02:00
|
|
|
res := struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
}{}
|
2017-04-13 19:16:52 +02:00
|
|
|
err = json.NewDecoder(resp.Body).Decode(&res)
|
2017-04-13 09:47:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.config.AccessToken = res.AccessToken
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-04-14 05:21:27 +02:00
|
|
|
// Toot is struct to post status.
|
2017-04-13 19:16:52 +02:00
|
|
|
type Toot struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
InReplyToID int64 `json:"in_reply_to_id"`
|
2017-04-14 05:21:27 +02:00
|
|
|
MediaIDs []int64 `json:"media_ids"`
|
2017-04-13 19:16:52 +02:00
|
|
|
Sensitive bool `json:"sensitive"`
|
|
|
|
SpoilerText string `json:"spoiler_text"`
|
|
|
|
Visibility string `json:"visibility"`
|
|
|
|
}
|
|
|
|
|
2017-04-14 17:10:04 +02:00
|
|
|
// Mention hold information for mention.
|
|
|
|
type Mention struct {
|
|
|
|
URL string `json:"url"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Acct string `json:"acct"`
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tag hold information for tag.
|
|
|
|
type Tag struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attachment hold information for attachment.
|
|
|
|
type Attachment struct {
|
|
|
|
ID int64 `json:"id"`
|
|
|
|
Type string `json:"type"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
RemoteURL string `json:"remote_url"`
|
|
|
|
PreviewURL string `json:"preview_url"`
|
|
|
|
TextURL string `json:"text_url"`
|
|
|
|
}
|
2017-04-15 16:21:37 +02:00
|
|
|
|
2017-04-16 16:38:53 +02:00
|
|
|
// Results hold information for search result.
|
2017-04-15 16:21:37 +02:00
|
|
|
type Results struct {
|
|
|
|
Accounts []*Account `json:"accounts"`
|
|
|
|
Statuses []*Status `json:"statuses"`
|
|
|
|
Hashtags []string `json:"hashtags"`
|
|
|
|
}
|