-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
457 lines (402 loc) · 12.2 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
"unicode/utf8"
typetalk "github.com/nulab/go-typetalk/v3/typetalk/v1"
uuid "github.com/satori/go.uuid"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/typetalk-gadget/go-typetalk-token-source/source"
"github.com/vvatanabe/spotify-playing-stream/stream"
"github.com/zmb3/spotify"
"golang.org/x/oauth2"
)
const (
cmdName = "nowplaying-on-typetalk"
defaultPort = 18080
flagNameDebug = "debug"
flagNameConfig = "config"
flagNameTypetalkClientID = "typetalk_client_id"
flagNameTypetalkClientSecret = "typetalk_client_secret"
flagNameTypetalkSpaceKey = "typetalk_space_key"
flagNameSpotifyClientID = "spotify_client_id"
flagNameSpotifyClientSecret = "spotify_client_secret"
flagNameStatusEmoji = "status_emoji"
flagNamePort = "port"
flagNameTopic = "topic"
configNameTopics = "topics"
)
type Config struct {
Debug bool `mapstructure:"debug"`
TypetalkClientID string `mapstructure:"typetalk_client_id"`
TypetalkClientSecret string `mapstructure:"typetalk_client_secret"`
TypetalkSpaceKey string `mapstructure:"typetalk_space_key"`
SpotifyClientID string `mapstructure:"spotify_client_id"`
SpotifyClientSecret string `mapstructure:"spotify_client_secret"`
StatusEmoji string `mapstructure:"status_emoji"`
Port int `mapstructure:"port"`
Topics []int `mapstructure:"topics"`
}
var (
config Config
dotDir string
)
func main() {
home, err := os.UserHomeDir()
if err != nil {
printFatal("UserHomeDir:", err)
}
dotDir = getDotDir(home)
err = os.MkdirAll(dotDir, 0700)
if err != nil {
printFatal("MkdirAll:", err)
}
defaultConfigFile := filepath.Join(dotDir, "config.yaml")
if !exists(defaultConfigFile) {
defaultConfigFile = filepath.Join(dotDir, "config.yml")
if !exists(defaultConfigFile) {
_, err = os.Create(defaultConfigFile)
if err != nil {
printFatal("Create:", err)
}
}
}
rootCmd := &cobra.Command{
Use: cmdName,
Run: run,
Version: FmtVersion(),
}
flags := rootCmd.PersistentFlags()
flags.Bool(flagNameDebug, false, "debug mode")
flags.StringP(flagNameConfig, "c", defaultConfigFile, "config file path")
flags.String(flagNameTypetalkClientID, "", "typetalk client id [TYPETALK_CLIENT_ID]")
flags.String(flagNameTypetalkClientSecret, "", "typetalk client secret [TYPETALK_CLIENT_SECRET]")
flags.String(flagNameTypetalkSpaceKey, "", "typetalk space key [TYPETALK_SPACE_KEY]")
flags.String(flagNameSpotifyClientID, "", "spotify client id [SPOTIFY_CLIENT_ID]")
flags.String(flagNameSpotifyClientSecret, "", "spotify client secret [SPOTIFY_CLIENT_SECRET]")
flags.String(flagNameStatusEmoji, ":musical_note:", "typetalk status emoji [STATUS_EMOJI]")
flags.Int(flagNamePort, defaultPort, "port number for OAuth")
flags.StringSlice(flagNameTopic, nil, "topic ID to post")
_ = viper.BindPFlag(flagNameDebug, flags.Lookup(flagNameDebug))
_ = viper.BindPFlag(flagNameTypetalkClientID, flags.Lookup(flagNameTypetalkClientID))
_ = viper.BindPFlag(flagNameTypetalkClientSecret, flags.Lookup(flagNameTypetalkClientSecret))
_ = viper.BindPFlag(flagNameTypetalkSpaceKey, flags.Lookup(flagNameTypetalkSpaceKey))
_ = viper.BindPFlag(flagNameSpotifyClientID, flags.Lookup(flagNameSpotifyClientID))
_ = viper.BindPFlag(flagNameSpotifyClientSecret, flags.Lookup(flagNameSpotifyClientSecret))
_ = viper.BindPFlag(flagNameStatusEmoji, flags.Lookup(flagNameStatusEmoji))
_ = viper.BindPFlag(flagNamePort, flags.Lookup(flagNamePort))
_ = viper.BindPFlag(configNameTopics, flags.Lookup(flagNameTopic))
cobra.OnInitialize(func() {
configFile, err := flags.GetString(flagNameConfig)
if err != nil {
printFatal(err)
}
viper.SetConfigFile(configFile)
viper.SetConfigType("yaml")
// viper.SetEnvPrefix(envPrefix)
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err != nil {
printFatal("failed to read config", err)
}
if err := viper.Unmarshal(&config); err != nil {
printFatal("failed to unmarshal config", err)
}
})
if err := rootCmd.Execute(); err != nil {
printFatal(err)
}
}
var (
// redirectURI is the OAuth redirect URI for the application.
// You must register an application at Spotify's developer portal
// and enter this value.
// http://localhost:18080/nowplaying-on-typetalk
redirectURI string
auth spotify.Authenticator
state = uuid.NewV4().String()
ch = make(chan *spotify.Client)
)
func run(c *cobra.Command, args []string) {
// printDebug(fmt.Sprintf("config: %#v\n", config))
redirectURI = fmt.Sprintf("http://localhost:%d/%s", config.Port, cmdName)
auth = spotify.NewAuthenticator(redirectURI, spotify.ScopeUserReadCurrentlyPlaying)
auth.SetAuthInfo(config.SpotifyClientID, config.SpotifyClientSecret)
var sc *spotify.Client
defer func() {
if sc == nil {
return
}
tok, err := sc.Token()
if err != nil {
printError(err)
return
}
err = saveSpotifyTokenToFile(dotDir, tok)
if err != nil {
printError(err)
return
}
}()
if tok, err := getSpotifyTokenFromFile(dotDir); err != nil {
printInfo(err)
} else {
sc, err = newSpotify(&auth, tok)
if err != nil {
printError(err)
}
}
if sc == nil {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", config.Port))
if err != nil {
printFatal(err)
}
// first start an HTTP server
mux := http.NewServeMux()
// pattern /nowplaying-on-typetalk
mux.HandleFunc("/"+cmdName, completeAuth)
srv := http.Server{
Handler: mux,
}
go func() {
err := srv.Serve(ln)
if err != nil && err != http.ErrServerClosed {
printError(err)
}
}()
authURL := auth.AuthURL(state)
err = openBrowser(authURL)
if err != nil {
printFatal(err)
}
printDebug("Logged in to Spotify by visiting the following page in your browser:", authURL)
// wait for auth to complete
sc = <-ch
go func() {
err := srv.Shutdown(context.Background())
if err != nil {
printError(err)
}
ln.Close()
}()
}
tc := newTypetalk(config.TypetalkClientID, config.TypetalkClientSecret, "my topic.post")
sps := stream.Stream{
Conn: sc,
Handler: &handler{
tc: tc,
spaceKey: config.TypetalkSpaceKey,
emoji: config.StatusEmoji,
topics: config.Topics,
},
LoggerFunc: printError,
}
go func() {
printInfo("start to subscribe spotify playing stream")
err := sps.Subscribe()
if err != nil {
printError(err)
}
}()
sigint := make(chan os.Signal, 1)
signal.Notify(sigint, os.Interrupt, syscall.SIGTERM)
<-sigint
printInfo("received a signal of graceful shutdown")
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
err := sps.Shutdown(ctx)
if err != nil {
printError("failed to graceful shutdown", err)
return
}
printInfo("completed graceful shutdown")
}
func getSpotifyTokenFromFile(dir string) (*oauth2.Token, error) {
blob, err := ioutil.ReadFile(filepath.Join(dir, "spotify"))
if err != nil {
return nil, err
}
var token oauth2.Token
err = json.Unmarshal(blob, &token)
if err != nil {
return nil, err
}
if token.Expiry.Before(time.Now()) {
return nil, errors.New("expired token")
}
return &token, nil
}
func saveSpotifyTokenToFile(dir string, token *oauth2.Token) error {
blob, err := json.Marshal(token)
if err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(dir, "spotify"), blob, 0644)
}
func newTypetalk(clientID, clientSecret, scope string) *typetalk.Client {
tokenSource := &source.TokenSource{
ClientID: clientID,
ClientSecret: clientSecret,
Scope: scope,
}
oc := oauth2.NewClient(context.Background(), tokenSource)
return typetalk.NewClient(oc)
}
func newSpotify(auth *spotify.Authenticator, token *oauth2.Token) (*spotify.Client, error) {
c := auth.NewClient(token)
c.AutoRetry = true
user, err := c.CurrentUser()
if err != nil {
return nil, err
}
// use the sc to make calls that require authorization
printDebug("You are logged in as:", user.ID)
return &c, nil
}
type handler struct {
tc *typetalk.Client
spaceKey, emoji string
topics []int
}
func (h *handler) Serve(playing *spotify.CurrentlyPlaying) {
if len(h.topics) > 0 {
for _, topicID := range h.topics {
h.postTopic(topicID, playing)
}
}
// eg. https://open.spotify.com/track/6aOaB0vl2ilHxRb23Wiazv
externalURL := playing.Item.ExternalURLs["spotify"]
// eg. Retarded/KID FRESINO
metadata := generateMetadata(playing, &metadataOption{trackInfo: true, albumName: false, albumImage: false, short: true})
if 25 < utf8.RuneCountInString(metadata) {
metadata = string([]rune(metadata)[:25]) + "…"
}
// eg. Retarded/KID FRESINO https://open.spotify.com/track/6aOaB0vl2ilHxRb23Wiazv
msg := fmt.Sprintf("%s %s", metadata, externalURL)
printDebug("NOW PLAYING", "-", msg)
_, _, err := h.tc.Statuses.SaveUserStatus(context.Background(),
h.spaceKey, h.emoji, &typetalk.SaveUserStatusOptions{
Message: msg,
ClearAt: "",
IsNotificationDisabled: false,
})
if err != nil {
printError(err)
}
}
func (h *handler) postTopic(topicID int, playing *spotify.CurrentlyPlaying) {
// eg. https://open.spotify.com/track/6aOaB0vl2ilHxRb23Wiazv
externalURL := playing.Item.ExternalURLs["spotify"]
// eg. Retarded/KID FRESINO - ai qing [ ](https://i.scdn.co/image/ab67616d0000b273b3ca13afd5b1315924854ce7)
metadata := generateMetadata(playing, &metadataOption{trackInfo: true, albumName: true, albumImage: true, short: false})
msg := fmt.Sprintf("%s %s\n%s", h.emoji, metadata, externalURL)
_, _, err := h.tc.Messages.PostMessage(context.Background(), topicID, msg, &typetalk.PostMessageOptions{})
if err != nil {
printError(err)
}
}
type metadataOption struct {
trackInfo bool
albumName bool
albumImage bool
short bool
}
func generateMetadata(playing *spotify.CurrentlyPlaying, opt *metadataOption) string {
meta := ""
if opt.trackInfo {
// eg. Retarded
trackName := playing.Item.Name
// eg. KID FRESINO
artistName := playing.Item.Artists[0].Name
format := "%s / %s"
if opt.short {
format = "%s/%s"
}
meta += fmt.Sprintf(format, trackName, artistName)
}
if opt.albumName {
albumName := playing.Item.Album.Name
meta += fmt.Sprintf(" - %s", albumName)
}
if opt.albumImage {
albumImageURL := playing.Item.Album.Images[0].URL
meta += fmt.Sprintf(" [ ](%s)", albumImageURL)
}
return meta
}
func completeAuth(w http.ResponseWriter, r *http.Request) {
tok, err := auth.Token(state, r)
if err != nil {
http.Error(w, "Couldn't get token", http.StatusForbidden)
printFatal(err)
}
if st := r.FormValue("state"); st != state {
http.NotFound(w, r)
printFatal("State mismatch: %s != %s\n", st, state)
}
// use the token to get an authenticated client
c, err := newSpotify(&auth, tok)
if err != nil {
http.Error(w, "Couldn't connect spotify api", http.StatusForbidden)
printFatal(err)
}
fmt.Fprintf(w, "Login completed. You can close this tab.")
ch <- c
}
func openBrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err
}
func exists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func getDotDir(home string) string {
if dir, exist := os.LookupEnv("XDG_CONFIG_HOME"); dir != "" && exist {
return filepath.Join(dir, cmdName)
}
return filepath.Join(home, "."+cmdName)
}
func printDebug(args ...interface{}) {
if config.Debug {
args = append([]interface{}{"[DEBUG]"}, args...)
log.Println(args...)
}
}
func printInfo(args ...interface{}) {
args = append([]interface{}{"[INFO]"}, args...)
log.Println(args...)
}
func printError(args ...interface{}) {
args = append([]interface{}{"[ERROR]"}, args...)
log.Println(args...)
}
func printFatal(args ...interface{}) {
args = append([]interface{}{"[FATAL]"}, args...)
log.Fatalln(args...)
}