This repository has been archived by the owner on May 7, 2024. It is now read-only.
/
mastodon.go
106 lines (88 loc) · 2.59 KB
/
mastodon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
log "github.com/sirupsen/logrus"
)
type apiError struct {
Message string `json:"error"`
}
type apiApp struct {
Name string `json:"name"`
}
type apiPostStatus struct {
Status string `json:"status"`
Visibility string `json:"visibility"`
}
type apiStatus struct {
Uri string `json:"uri"`
}
func MastodonVerifyApp(accessToken string, instance string) {
client := &http.Client{}
req, err := http.NewRequest("GET", instance+"/api/v1/apps/verify_credentials", nil)
if err != nil {
panic(err)
}
req.Header.Add("Authorization", "Bearer "+accessToken)
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var error apiError
if err := json.NewDecoder(resp.Body).Decode(&error); err != nil {
log.Error(fmt.Sprintf("Error: %s (%d)", err.Error(), resp.StatusCode))
os.Exit(1)
}
log.Error(fmt.Sprintf("Error: %s (%d)", error.Message, resp.StatusCode))
os.Exit(1)
}
var app apiApp
if err := json.NewDecoder(resp.Body).Decode(&app); err != nil {
panic(err)
}
log.Info("App verified: " + app.Name)
}
func MastodonPostSong(accessToken string, instance string, nextSong KiiteSongInfo) (bool, error) {
client := &http.Client{}
reqBody := apiPostStatus{
Status: fmt.Sprintf("\u266a%s #%s #Kiite\nKiite Cafeできいてます https://cafe.kiite.jp", nextSong.Title, nextSong.VideoId),
Visibility: "unlisted",
}
reqBodyJson, err := json.Marshal(reqBody)
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", instance+"/api/v1/statuses", bytes.NewBuffer(reqBodyJson))
if err != nil {
panic(err)
}
req.Header.Add("Authorization", "Bearer "+accessToken)
req.Header.Add("Content-Type", "application/json")
log.Info("Posting song...")
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var error apiError
if err := json.NewDecoder(resp.Body).Decode(&error); err != nil {
log.Error(fmt.Sprintf("Error: %s (%d)", err.Error(), resp.StatusCode))
return false, fmt.Errorf("Error: Failed to post song (%s)", resp.Status)
}
log.Error(fmt.Sprintf("Error: %s (%d)", error.Message, resp.StatusCode))
return false, fmt.Errorf("Error: Failed to post song (%s)", resp.Status)
}
var status apiStatus
if err := json.NewDecoder(resp.Body).Decode(&status); err != nil {
log.Error(fmt.Sprintf("Error: %s (%d)", err.Error(), resp.StatusCode))
return false, fmt.Errorf("Error: Failed to get status data (%s)", resp.Status)
}
log.Info("Posted song: " + status.Uri)
return true, nil
}