go-mastodon/mastodon.go

403 lines
10 KiB
Go
Raw Normal View History

2017-04-13 09:47:00 +02:00
package mastodon
import (
2017-04-14 02:32:46 +02:00
"bufio"
"context"
2017-04-13 09:47:00 +02:00
"encoding/json"
2017-04-13 19:39:34 +02:00
"fmt"
2017-04-13 09:47:00 +02:00
"net/http"
"net/url"
2017-04-13 19:16:52 +02:00
"path"
2017-04-13 09:47:00 +02:00
"strings"
"time"
)
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
config *Config
}
2017-04-14 05:21:27 +02:00
func (c *Client) doAPI(method string, uri string, params url.Values, res interface{}) error {
2017-04-14 05:12:09 +02:00
url, err := url.Parse(c.config.Server)
if err != nil {
return err
}
url.Path = path.Join(url.Path, uri)
var resp *http.Response
req, err := http.NewRequest(method, url.String(), strings.NewReader(params.Encode()))
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
resp, err = c.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
if res == nil {
return nil
}
if method == "GET" && resp.StatusCode != 200 {
return fmt.Errorf("bad request: %v", resp.Status)
}
2017-04-14 05:12:09 +02:00
return json.NewDecoder(resp.Body).Decode(&res)
}
2017-04-14 05:21:27 +02:00
// NewClient return new mastodon API client.
func NewClient(config *Config) *Client {
return &Client{
2017-04-13 09:47:00 +02:00
Client: *http.DefaultClient,
config: config,
}
}
2017-04-14 05:21:27 +02:00
// Authenticate get access-token to the API.
func (c *Client) Authenticate(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")
url, err := url.Parse(c.config.Server)
if err != nil {
return err
}
url.Path = path.Join(url.Path, "/oauth/token")
req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode()))
2017-04-13 09:47:00 +02:00
if err != nil {
return err
}
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 10:10:13 +02:00
if resp.StatusCode != 200 {
return fmt.Errorf("bad authorization: %v", resp.Status)
}
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 02:03:32 +02:00
// AppConfig is a setting for registering applications.
type AppConfig struct {
http.Client
Server string
ClientName string
// Where the user should be redirected after authorization (for no redirect, use urn:ietf:wg:oauth:2.0:oob)
RedirectURIs string
// This can be a space-separated list of the following items: "read", "write" and "follow".
Scopes string
// Optional.
Website string
2017-04-13 19:39:34 +02:00
}
2017-04-14 02:03:32 +02:00
// Application is mastodon application.
type Application struct {
ID int64 `json:"id"`
RedirectURI string `json:"redirect_uri"`
ClientID string `json:"client_id"`
ClientSecret string `json:"client_secret"`
}
// RegisterApp returns the mastodon application.
func RegisterApp(appConfig *AppConfig) (*Application, error) {
params := url.Values{}
params.Set("client_name", appConfig.ClientName)
params.Set("redirect_uris", appConfig.RedirectURIs)
params.Set("scopes", appConfig.Scopes)
params.Set("website", appConfig.Website)
url, err := url.Parse(appConfig.Server)
2017-04-13 19:39:34 +02:00
if err != nil {
return nil, err
}
2017-04-14 02:03:32 +02:00
url.Path = path.Join(url.Path, "/api/v1/apps")
2017-04-13 19:39:34 +02:00
2017-04-14 02:03:32 +02:00
req, err := http.NewRequest("POST", url.String(), strings.NewReader(params.Encode()))
2017-04-13 19:39:34 +02:00
if err != nil {
return nil, err
}
2017-04-14 02:03:32 +02:00
resp, err := appConfig.Do(req)
2017-04-13 19:39:34 +02:00
if err != nil {
return nil, err
}
defer resp.Body.Close()
2017-04-14 02:03:32 +02:00
app := &Application{}
err = json.NewDecoder(resp.Body).Decode(app)
2017-04-13 19:39:34 +02:00
if err != nil {
return nil, err
}
2017-04-14 02:03:32 +02:00
return app, nil
}
2017-04-14 05:21:27 +02:00
// Account hold information for mastodon account.
2017-04-14 03:23:02 +02:00
type Account struct {
ID int64 `json:"id"`
Username string `json:"username"`
Acct string `json:"acct"`
DisplayName string `json:"display_name"`
Locked bool `json:"locked"`
CreatedAt time.Time `json:"created_at"`
FollowersCount int64 `json:"followers_count"`
FollowingCount int64 `json:"following_count"`
StatusesCount int64 `json:"statuses_count"`
Note string `json:"note"`
URL string `json:"url"`
Avatar string `json:"avatar"`
AvatarStatic string `json:"avatar_static"`
Header string `json:"header"`
HeaderStatic string `json:"header_static"`
2017-04-13 19:39:34 +02:00
}
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 05:21:27 +02:00
// Status is struct to hold status.
2017-04-13 19:16:52 +02:00
type Status struct {
2017-04-13 19:39:34 +02:00
ID int64 `json:"id"`
CreatedAt time.Time `json:"created_at"`
InReplyToID interface{} `json:"in_reply_to_id"`
InReplyToAccountID interface{} `json:"in_reply_to_account_id"`
Sensitive bool `json:"sensitive"`
SpoilerText string `json:"spoiler_text"`
Visibility string `json:"visibility"`
Application interface{} `json:"application"`
Account Account `json:"account"`
MediaAttachments []interface{} `json:"media_attachments"`
Mentions []interface{} `json:"mentions"`
Tags []interface{} `json:"tags"`
URI string `json:"uri"`
Content string `json:"content"`
URL string `json:"url"`
ReblogsCount int64 `json:"reblogs_count"`
FavouritesCount int64 `json:"favourites_count"`
Reblog interface{} `json:"reblog"`
Favourited interface{} `json:"favourited"`
Reblogged interface{} `json:"reblogged"`
2017-04-13 09:47:00 +02:00
}
2017-04-14 05:21:27 +02:00
// GetAccount return Account.
func (c *Client) GetAccount(id int) (*Account, error) {
2017-04-14 05:12:09 +02:00
var account Account
err := c.doAPI("GET", fmt.Sprintf("/api/v1/accounts/%d", id), nil, &account)
2017-04-14 03:23:02 +02:00
if err != nil {
return nil, err
}
2017-04-14 05:12:09 +02:00
return &account, nil
2017-04-13 09:47:00 +02:00
}
2017-04-14 08:56:24 +02:00
// GetAccountCurrentUser return Account of current user.
2017-04-14 08:46:21 +02:00
func (c *Client) GetAccountCurrentUser() (*Account, error) {
var account Account
err := c.doAPI("GET", "/api/v1/accounts/verify_credentials", nil, &account)
if err != nil {
return nil, err
}
return &account, nil
}
2017-04-14 07:18:10 +02:00
// GetAccountFollowers return followers list.
2017-04-14 06:37:43 +02:00
func (c *Client) GetAccountFollowers(id int64) ([]*Account, error) {
var accounts []*Account
err := c.doAPI("GET", fmt.Sprintf("/api/v1/accounts/%d/followers", id), nil, &accounts)
if err != nil {
return nil, err
}
return accounts, nil
}
2017-04-14 09:41:47 +02:00
// GetAccountFollowing return following list.
func (c *Client) GetAccountFollowing(id int64) ([]*Account, error) {
var accounts []*Account
err := c.doAPI("GET", fmt.Sprintf("/api/v1/accounts/%d/following", id), nil, &accounts)
if err != nil {
return nil, err
}
return accounts, nil
}
2017-04-14 05:21:27 +02:00
// GetTimelineHome return statuses from home timeline.
func (c *Client) GetTimelineHome() ([]*Status, error) {
2017-04-13 19:16:52 +02:00
var statuses []*Status
2017-04-14 05:12:09 +02:00
err := c.doAPI("GET", "/api/v1/timelines/home", nil, &statuses)
2017-04-13 19:16:52 +02:00
if err != nil {
return nil, err
}
return statuses, nil
}
2017-04-14 05:21:27 +02:00
// PostStatus post the toot.
func (c *Client) PostStatus(toot *Toot) (*Status, error) {
2017-04-13 19:16:52 +02:00
params := url.Values{}
params.Set("status", toot.Status)
2017-04-14 04:49:52 +02:00
if toot.InReplyToID > 0 {
params.Set("in_reply_to_id", fmt.Sprint(toot.InReplyToID))
}
2017-04-13 19:16:52 +02:00
// TODO: media_ids, senstitive, spoiler_text, visibility
//params.Set("visibility", "public")
var status Status
2017-04-14 05:12:09 +02:00
err := c.doAPI("POST", "/api/v1/statuses", params, &status)
2017-04-13 09:47:00 +02:00
if err != nil {
return nil, err
}
2017-04-13 19:16:52 +02:00
return &status, nil
2017-04-13 09:47:00 +02:00
}
2017-04-13 14:45:44 +02:00
2017-04-14 05:21:27 +02:00
// UpdateEvent is struct for passing status event to app.
2017-04-14 07:18:10 +02:00
type UpdateEvent struct{ Status *Status }
2017-04-13 14:45:44 +02:00
2017-04-14 02:32:46 +02:00
func (e *UpdateEvent) event() {}
2017-04-13 14:45:44 +02:00
2017-04-14 05:21:27 +02:00
// NotificationEvent is struct for passing notification event to app.
2017-04-14 07:18:10 +02:00
type NotificationEvent struct{}
2017-04-13 14:45:44 +02:00
2017-04-14 02:32:46 +02:00
func (e *NotificationEvent) event() {}
2017-04-14 05:21:27 +02:00
// DeleteEvent is struct for passing deletion event to app.
2017-04-14 07:18:10 +02:00
type DeleteEvent struct{ ID int64 }
2017-04-13 14:45:44 +02:00
2017-04-14 02:32:46 +02:00
func (e *DeleteEvent) event() {}
2017-04-13 14:45:44 +02:00
2017-04-14 05:21:27 +02:00
// ErrorEvent is struct for passing errors to app.
2017-04-14 07:18:10 +02:00
type ErrorEvent struct{ err error }
2017-04-14 04:49:52 +02:00
func (e *ErrorEvent) event() {}
2017-04-14 07:18:10 +02:00
func (e *ErrorEvent) Error() string { return e.err.Error() }
2017-04-14 04:49:52 +02:00
2017-04-14 05:21:27 +02:00
// Event is interface passing events to app.
2017-04-14 02:32:46 +02:00
type Event interface {
event()
}
2017-04-13 14:45:44 +02:00
2017-04-14 05:21:27 +02:00
// StreamingPublic return channel to read events.
func (c *Client) StreamingPublic(ctx context.Context) (chan Event, error) {
2017-04-14 02:32:46 +02:00
url, err := url.Parse(c.config.Server)
2017-04-13 14:45:44 +02:00
if err != nil {
return nil, err
}
2017-04-14 02:32:46 +02:00
url.Path = path.Join(url.Path, "/api/v1/streaming/public")
2017-04-14 04:49:52 +02:00
var resp *http.Response
2017-04-13 14:45:44 +02:00
2017-04-14 04:49:52 +02:00
q := make(chan Event, 10)
2017-04-14 02:32:46 +02:00
go func() {
defer ctx.Done()
2017-04-14 04:49:52 +02:00
for {
req, err := http.NewRequest("GET", url.String(), nil)
if err == nil {
req.Header.Set("Authorization", "Bearer "+c.config.AccessToken)
resp, err = c.Do(req)
2017-04-14 02:32:46 +02:00
}
2017-04-14 04:49:52 +02:00
if err == nil {
name := ""
2017-04-14 06:53:56 +02:00
s := bufio.NewScanner(resp.Body)
2017-04-14 04:49:52 +02:00
for s.Scan() {
line := s.Text()
token := strings.SplitN(line, ":", 2)
if len(token) != 2 {
continue
}
switch strings.TrimSpace(token[0]) {
case "event":
name = strings.TrimSpace(token[1])
case "data":
switch name {
case "update":
var status Status
2017-04-14 05:23:20 +02:00
err = json.Unmarshal([]byte(token[1]), &status)
if err == nil {
q <- &UpdateEvent{&status}
}
2017-04-14 04:49:52 +02:00
case "notification":
case "delete":
}
default:
}
2017-04-14 02:32:46 +02:00
}
2017-04-14 04:49:52 +02:00
resp.Body.Close()
err = ctx.Err()
if err == nil {
break
}
} else {
q <- &ErrorEvent{err}
2017-04-14 02:32:46 +02:00
}
2017-04-14 04:49:52 +02:00
time.Sleep(3 * time.Second)
2017-04-14 02:32:46 +02:00
}
}()
go func() {
<-ctx.Done()
2017-04-14 04:49:52 +02:00
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
2017-04-14 02:32:46 +02:00
}()
return q, nil
2017-04-13 14:45:44 +02:00
}
2017-04-14 07:18:10 +02:00
2017-04-14 07:20:22 +02:00
// Follow send follow-request.
2017-04-14 07:18:10 +02:00
func (c *Client) Follow(uri string) (*Account, error) {
params := url.Values{}
params.Set("uri", uri)
var account Account
err := c.doAPI("POST", "/api/v1/follows", params, &account)
if err != nil {
return nil, err
}
return &account, nil
}
2017-04-14 07:20:22 +02:00
// GetFollowRequest return follow-requests.
func (c *Client) GetFollowRequests(uri string) ([]*Account, error) {
params := url.Values{}
params.Set("uri", uri)
var accounts []*Account
err := c.doAPI("GET", "/api/v1/follow_requests", params, &accounts)
if err != nil {
return nil, err
}
return accounts, nil
}