-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
375 lines (318 loc) · 8.15 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
package main
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"math/rand"
"os"
"strings"
"time"
"github.com/gempir/go-twitch-irc"
"github.com/lyrenhex/twedia/twedia"
)
type Config struct {
Username string `json:"username"`
Channel string `json:"channel"`
ClientID string `json:"clientID"`
ClientSecret string `json:"clientSecret"`
MusicDir string `json:"musicDir"`
MusicFile string `json:"musicFile"`
OauthToken string `json:"oauthToken"`
PubsubOauthToken string `json:"pubsubOauthToken"`
MusicCollectionURL string `json:"musicCollectionURL"`
ChatCommands []command `json:"chatCommands"`
PointRewards []reward `json:"pointRewards"`
}
type command struct {
Trigger string `json:"trigger"`
Action action `json:"action"`
}
type reward struct {
Title string `json:"rewardTitle"`
Action action `json:"action"`
}
type action struct {
Type string `json:"type"`
Text string `json:"text"`
Artist string `json:"artist"`
Album string `json:"album"`
Song string `json:"title"`
}
var config Config
var artists twedia.Music
var t *twitch.Client
var channelID string
var musicPlayer twedia.Player
var speechPlayer twedia.Player
var lastSpeech time.Time = time.Unix(0, 0)
func init() {
var err error
config, err = loadConfig(os.Getenv("TWITCH_CONFIG_FILE"))
if err != nil {
panic(err)
}
err = twedia.GetSongs(&artists, config.MusicCollectionURL)
if err != nil {
log.Fatal(err)
}
err = twedia.InitSpeaker()
if err != nil {
log.Println("Error initialising speaker:", err)
}
musicPlayer = twedia.NewPlayer()
speechPlayer = twedia.NewPlayer()
for {
channelID, err = twedia.GetChannelID(config.PubsubOauthToken, config.ClientID)
if err == nil {
break
} else if err.Error() == "invalid oauth token" {
config.PubsubOauthToken = twedia.GetOAuthToken(config.ClientID)
config.saveConfig(os.Getenv("TWITCH_CONFIG_FILE"))
} else {
log.Println("Error obtaining channel ID:", err)
os.Exit(1)
}
}
// Seed the random Source such that we don't always listen to Blessed are the Teamakers...
rand.Seed(time.Now().UnixNano())
fmt.Println(`Twedia Music Manager
Commands:
start : start playing random music
pause : pause / unpause the current song
skip : skip the current song
stop : stop playing music
select: play a specific song
quit : exit program`)
}
func loadConfig(s string) (Config, error) {
var config Config
f, err := os.Open(s)
if err != nil {
return config, err
}
defer f.Close()
b, _ := ioutil.ReadAll(f)
json.Unmarshal(b, &config)
return config, nil
}
func (c *Config) saveConfig(s string) error {
f, err := os.Open(s)
if err != nil {
return err
}
defer f.Close()
json, err := json.Marshal(c)
if err != nil {
return err
}
err = ioutil.WriteFile(s, json, 0777)
if err != nil {
return err
}
return nil
}
func exists(fp string) bool {
// cheers to https://stackoverflow.com/a/12518877/4897375 (CC-BY-SA 4.0)
if _, err := os.Stat(fp); err == nil {
return true
} else if errors.Is(err, os.ErrNotExist) {
return false
} else {
fmt.Printf("Unexpected error when verifying file existence: %s\n", err)
return false
}
}
func play(artist twedia.Artist, album twedia.Album, song twedia.Song) error {
var f *os.File
var err error
for {
f, err = os.Create(config.MusicFile)
if err == nil {
break
}
}
defer f.Close()
// open the song for playing
s := string(os.PathSeparator)
path := config.MusicDir + s + artist.Artist + s + album.Name + s + song.Title
if exists(path + ".mp3") {
path += ".mp3"
} else if exists(path + ".wav") {
path += ".wav"
} else if exists(path + ".ogg") {
path += ".ogg"
} else if exists(path + ".flac") {
path += ".flac"
} else {
log.Println("Song file cannot be found: " + path)
return errors.New("Song file cannot be found: " + path)
}
f.WriteString(fmt.Sprintf("\n%s, by %s", song.Title, artist.Artist))
if song.URL != "" {
t.Say(config.Channel, fmt.Sprintf("Playing %s by %s. Listen on YouTube: %s", song.Title, artist.Artist, song.URL))
} else {
t.Say(config.Channel, fmt.Sprintf("Playing %s by %s.", song.Title, artist.Artist))
}
musicPlayer.PlayFile(path)
// clear the current song from the now playing file list
os.Create(config.MusicFile)
return nil
}
func playRnd() {
for musicPlayer.Playing {
// select a random artist, with probability adjusted proportionally to the number of songs by that artist (this finally solves the disproportionate frequency of 'The Tea Song' and 'Blessed Are The Teamakers')
var artist twedia.Artist
r := rand.Intn(artists.TotalSongs)
for _, a := range artists.Artists {
if r < a.TotalSongs {
artist = a
break
}
r -= a.TotalSongs
}
// now select a random album by that artist
album := artist.Albums[rand.Intn(len(artist.Albums))]
// and a random song from that album
song := album.Songs[rand.Intn(len(album.Songs))]
err := play(artist, album, song)
if err != nil {
log.Println(err)
continue
}
}
}
func stopPlayback() {
err := musicPlayer.Stop()
if err != nil {
log.Println("Error stopping music player:", err)
}
err = speechPlayer.Stop()
if err != nil {
log.Println("Error stopping speech player:", err)
}
os.Create(config.MusicFile)
}
func rewardCallback(r twedia.TwitchRedemption) {
for _, rewardAction := range config.PointRewards {
if strings.EqualFold(r.Reward.Title, rewardAction.Title) {
completeAction(rewardAction.Action)
return
}
}
}
func completeAction(a action) {
switch a.Type {
case "song":
var artist twedia.Artist
var album twedia.Album
var song twedia.Song
if a.Artist != "" {
for _, ar := range artists.Artists {
if strings.EqualFold(ar.Artist, a.Artist) {
artist = ar
for _, al := range ar.Albums {
if strings.EqualFold(al.Name, a.Album) {
album = al
for _, s := range al.Songs {
if strings.EqualFold(s.Title, a.Song) {
song = s
break
}
}
break
}
}
break
}
}
err := musicPlayer.Stop()
if err != nil {
log.Println("Error stopping music player:", err)
}
go play(artist, album, song)
} else {
err := musicPlayer.Stop()
if err != nil {
log.Println("Error stopping music player:", err)
}
go playRnd()
}
case "tts":
lastSpeech = time.Now()
// write spoken speech to file
fn := twedia.SynthesiseText(a.Text)
// lower the music volume while the TTS occurs...
musicPlayer.AdjustVolume(-1.0)
err := speechPlayer.PlayFile(fn)
if err != nil {
log.Println("Error playing synthesised speech:", err)
}
// and raise it again!
musicPlayer.AdjustVolume(1.0)
}
}
func main() {
r := make(chan bool)
// Set up Twitch bot
t = twitch.NewClient(config.Username, "oauth:"+config.OauthToken)
t.OnNewMessage(func(c string, u twitch.User, m twitch.Message) {
if time.Since(lastSpeech) > (5 * time.Minute) {
for _, chatCommand := range config.ChatCommands {
if strings.EqualFold(strings.Split(m.Text, " ")[0], chatCommand.Trigger) {
completeAction(chatCommand.Action)
return
}
}
}
})
t.Join(config.Channel)
t.OnConnect(func() {
log.Println("Connected to Twitch chat as " + config.Username)
r <- true
})
go func() {
err := t.Connect()
if err != nil {
panic(err)
}
}()
<-r
go twedia.ListenChannelPoints(channelID, config.ClientID, config.PubsubOauthToken, rewardCallback)
for {
fmt.Print("> ")
var opt string
var err error
for {
reader := bufio.NewReader(os.Stdin)
opt, err = reader.ReadString('\n')
if err == nil {
break
}
}
opt = strings.ToLower(strings.Replace(strings.Replace(opt, "\n", "", -1), "\r", "", -1))
if opt == "start" {
musicPlayer.Playing = true
go playRnd()
} else if opt == "pause" {
musicPlayer.TogglePause()
} else if opt == "skip" {
err = musicPlayer.Skip()
if err != nil {
log.Println("Error skipping song:", err)
}
} else if opt == "stop" {
stopPlayback()
} else if opt == "select" {
artist, album, song := twedia.SelectSong(&artists)
musicPlayer.Playing = true
go play(*artist, *album, *song)
} else if opt == "quit" {
break
}
}
stopPlayback()
os.Exit(0)
}