-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
255 lines (206 loc) · 7.7 KB
/
main.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package main
import (
"context"
b64 "encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"reflect"
"strings"
"time"
"github.com/joho/godotenv"
"github.com/mattn/go-mastodon"
)
func main() {
go func() {
http.HandleFunc("/login", spotify_login)
auth_code := make(chan string)
go pass_callback(auth_code)
handleCallback := spotify_callback(auth_code)
http.HandleFunc("/callback", handleCallback)
err := http.ListenAndServe("0.0.0.0:3000", nil)
if err != nil {
log.Fatal(err)
}
}()
godotenv.Load(".env")
if os.Getenv("MASTODON_INSTANCE_URL") == "" || os.Getenv("MASTODON_CLIENT_ID") == "" || os.Getenv("MASTODON_CLIENT_SECRET") == "" || os.Getenv("MASTODON_ACCOUNT_EMAIL") == "" || os.Getenv("MASTODON_ACCOUNT_PASSWORD") == "" || os.Getenv("SPOTIFY_CLIENT_ID") == "" || os.Getenv("SPOTIFY_CLIENT_SECRET") == "" {
log.Fatal("Failed to load credentials.")
} else if os.Getenv("SPOTIFY_REFRESH_TOKEN") == "" {
fmt.Println("`SPOTIFY_REFRESH_TOKEN` is not set. Please click the URL below.")
values := url.Values{}
values.Add("client_id", os.Getenv("SPOTIFY_CLIENT_ID"))
values.Add("response_type", "code")
values.Add("redirect_uri", "http://localhost:3000/callback")
fmt.Println("https://accounts.spotify.com/authorize?" + values.Encode())
}
mastodon_client := mastodon.NewClient(&mastodon.Config{
Server: os.Getenv("MASTODON_INSTANCE_URL"),
ClientID: os.Getenv("MASTODON_CLIENT_ID"),
ClientSecret: os.Getenv("MASTODON_CLIENT_SECRET"),
})
err := mastodon_client.Authenticate(context.Background(), os.Getenv("MASTODON_ACCOUNT_EMAIL"), os.Getenv("MASTODON_ACCOUNT_PASSWORD"))
if err != nil {
log.Fatal(err)
}
last_title := ""
ticker := time.NewTicker(20 * time.Second)
for {
select {
case <-ticker.C:
is_playing, title, artist, album, url, progress := get_spotify_np()
if is_playing {
if last_title == "" || title != last_title {
if progress > 5000 {
message := fmt.Sprintf("🎵 #NowPlaying #np: %s / %s (%s)\n%s", title, artist, album, url)
fmt.Println(message)
toot := mastodon.Toot{
Status: message,
Visibility: "unlisted",
}
mastodon_client.PostStatus(context.Background(), &toot)
last_title = title
}
}
} else {
title, artist, album = "", "", ""
}
}
}
}
func spotify_login(w http.ResponseWriter, req *http.Request) {
values := url.Values{}
values.Add("client_id", os.Getenv("SPOTIFY_CLIENT_ID"))
values.Add("response_type", "code")
values.Add("redirect_uri", "http://localhost:3000/callback")
http.Redirect(w, req, "https://accounts.spotify.com/authorize?"+values.Encode(), http.StatusFound)
}
func spotify_callback(auth_code chan string) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, req *http.Request) {
query := req.URL.Query().Get("code")
auth_code <- query
w.WriteHeader(200)
w.Header().Set("Content-Type", "text/html; charset=utf8")
w.Write([]byte("処理が完了しました。この画面を閉じることができます。\nnp2mast を再起動してください。"))
}
}
func pass_callback(auth_code chan string) {
for item := range auth_code {
save_refresh_token(item)
}
}
func save_refresh_token(auth_code string) {
values := make(url.Values)
values.Set("grant_type", "authorization_code")
values.Set("code", auth_code)
values.Set("redirect_uri", "http://localhost:3000/callback")
req, err := http.NewRequest(http.MethodPost, "https://accounts.spotify.com/api/token", strings.NewReader(values.Encode()))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", os.Getenv("SPOTIFY_CLIENT_ID"), os.Getenv("SPOTIFY_CLIENT_SECRET"))))))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var jsonObj interface{}
if err := json.Unmarshal(body, &jsonObj); err != nil {
fmt.Println(string(body))
log.Fatal(err)
}
refresh_token := jsonObj.(map[string]interface{})["refresh_token"].(string)
refresh_token_env, err := godotenv.Unmarshal(fmt.Sprintf("MASTODON_INSTANCE_URL=%s\nMASTODON_CLIENT_ID=%s\nMASTODON_CLIENT_SECRET=%s\nMASTODON_ACCOUNT_EMAIL=%s\nMASTODON_ACCOUNT_PASSWORD=%s\nSPOTIFY_CLIENT_ID=%s\nSPOTIFY_CLIENT_SECRET=%s\nSPOTIFY_REFRESH_TOKEN=%s\n", os.Getenv("MASTODON_INSTANCE_URL"), os.Getenv("MASTODON_CLIENT_ID"), os.Getenv("MASTODON_CLIENT_SECRET"), os.Getenv("MASTODON_ACCOUNT_EMAIL"), os.Getenv("MASTODON_ACCOUNT_PASSWORD"), os.Getenv("SPOTIFY_CLIENT_ID"), os.Getenv("SPOTIFY_CLIENT_SECRET"), refresh_token))
if err != nil {
log.Fatal(err)
}
err = godotenv.Write(refresh_token_env, "./.env")
if err != nil {
log.Fatal(err)
}
os.Exit(0)
}
func get_spotify_access_token() string {
values := make(url.Values)
values.Set("grant_type", "refresh_token")
values.Set("refresh_token", os.Getenv("SPOTIFY_REFRESH_TOKEN"))
req, err := http.NewRequest(http.MethodPost, "https://accounts.spotify.com/api/token", strings.NewReader(values.Encode()))
if err != nil {
log.Fatal(err)
}
spotify_auth_string := b64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", os.Getenv("SPOTIFY_CLIENT_ID"), os.Getenv("SPOTIFY_CLIENT_SECRET"))))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", spotify_auth_string))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var jsonObj interface{}
if err := json.Unmarshal(body, &jsonObj); err != nil {
fmt.Println(string(body))
log.Fatal(err)
}
if isNil(jsonObj.(map[string]interface{})["access_token"]) {
fmt.Println(body)
os.Exit(1)
}
return jsonObj.(map[string]interface{})["access_token"].(string)
}
func isNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}
func get_spotify_np() (is_playing bool, title string, artist string, album string, url string, progress float64) {
req, err := http.NewRequest(http.MethodGet, "https://api.spotify.com/v1/me/player/currently-playing", nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", get_spotify_access_token()))
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
body, _ := io.ReadAll(resp.Body)
var jsonObj interface{}
if err := json.Unmarshal(body, &jsonObj); err != nil {
fmt.Println(string(body))
log.Fatal(err)
}
if isNil(jsonObj.(map[string]interface{})["is_playing"]) {
fmt.Println(string(body))
os.Exit(1)
}
is_playing = jsonObj.(map[string]interface{})["is_playing"].(bool)
if is_playing {
title = jsonObj.(map[string]interface{})["item"].(map[string]interface{})["name"].(string)
artists := jsonObj.(map[string]interface{})["item"].(map[string]interface{})["artists"]
for i := 0; i < len(artists.([]interface{})); i++ {
artist += artists.([]interface{})[i].(map[string]interface{})["name"].(string)
if i < len(artists.([]interface{}))-1 {
artist += ", "
}
}
album = jsonObj.(map[string]interface{})["item"].(map[string]interface{})["album"].(map[string]interface{})["name"].(string)
url = jsonObj.(map[string]interface{})["item"].(map[string]interface{})["external_urls"].(map[string]interface{})["spotify"].(string)
progress = jsonObj.(map[string]interface{})["progress_ms"].(float64)
} else {
is_playing = false
title, artist, album = "", "", ""
}
return is_playing, title, artist, album, url, progress
}