-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
217 lines (182 loc) · 4.56 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
package main
import (
"bufio"
"bytes"
"encoding/json"
"log"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go/service/polly"
"github.com/bamchoh/nagome-polly/player"
)
var (
logger *log.Logger
save_dir string = "mp3"
pc *PollyConfig
started_time time.Time
counter int
)
type Message struct {
Domain string `json:"domain"`
Command string `json:"command"`
Content json.RawMessage `json:"content,omitempty"` // The structure of Content is depend on the Command (and Domain).
prgno int
}
type CtCommentGot struct {
No int `json:"no"`
Date time.Time `json:"date"`
Raw string `json:"raw"`
Comment string `json:"comment"`
UserID string `json:"user_id"`
UserName string `json:"user_name"`
UserThumbnailURL string `json:"user_thumbnail_url,omitempty"`
Score int `json:"score,omitempty"`
IsPremium bool `json:"is_premium"`
IsBroadcaster bool `json:"is_broadcaster"`
IsStaff bool `json:"is_staff"`
IsAnonymity bool `json:"is_anonymity"`
}
type CtNagomeBroadOpen struct {
BroadID string `json:"broad_id"`
Title string `json:"title"`
Description string `json:"description"`
CommunityID string `json:"community_id"`
OwnerID string `json:"owner_id"`
OwnerName string `json:"owner_name"`
OwnerBroad bool `json:"owner_broad"`
OpenTime time.Time `json:"open_time"`
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
}
func set_log() *log.Logger {
basedir := filepath.Dir(os.Args[0])
log_path := filepath.Join(basedir, "nagome-polly.log")
f, _ := os.Create(log_path)
return log.New(f, "nagome-polly:", 0)
}
func send_aws(msg string, m *sync.Mutex) (resp *polly.SynthesizeSpeechOutput, err error) {
m.Lock()
defer m.Unlock()
speed := 100
speed += 20 * (counter - 1)
packed_msg := `<speak><prosody rate="` + strconv.Itoa(speed) + `%"><![CDATA[` + msg + `]]></prosody></speak>`
resp, err = synthesize_speech(pc, packed_msg)
return
}
func play(resp *polly.SynthesizeSpeechOutput, m *sync.Mutex) (err error) {
m.Lock()
defer m.Unlock()
err = player.Play(resp, logger)
if err != nil {
logger.Println(err)
}
return
}
func increment_count() {
counter++
}
func descrement_count() {
counter--
}
func read_aloud(broad_id string, content []byte, m1 *sync.Mutex, m2 *sync.Mutex) (err error) {
dec := json.NewDecoder(bytes.NewReader(content))
com := new(CtCommentGot)
err = dec.Decode(com)
if err != nil {
logger.Println(err)
return
}
if com.Date.Before(started_time) {
return
}
matched, err := regexp.MatchString("^/hb", com.Raw)
if err != nil {
logger.Println(err)
return
}
if matched {
return
}
go func(msg string) {
increment_count()
defer descrement_count()
if counter > 10 {
logger.Println("Skipped : ", msg)
return
}
resp, err := send_aws(msg, m1)
if err != nil {
logger.Println(err)
return
}
play(resp, m2)
}(string(com.Raw))
return
}
func init_plugin() (err error) {
basedir := filepath.Dir(os.Args[0])
filepath := filepath.Join(basedir, "nagome-polly.yml")
f, err := os.Open(filepath)
if err != nil {
log.Println(err)
return err
}
defer f.Close()
pc, err = load(f)
if err != nil {
log.Println(err)
return err
}
started_time = time.Now()
return
}
func pick_broad_id(content []byte) (broad_id string, err error) {
dec := json.NewDecoder(bytes.NewReader(content))
cnbo := new(CtNagomeBroadOpen)
err = dec.Decode(cnbo)
if err != nil {
logger.Println(err)
return
}
broad_id = cnbo.BroadID
return
}
func main() {
var m1 *sync.Mutex = new(sync.Mutex)
var m2 *sync.Mutex = new(sync.Mutex)
var broad_id string
logger = set_log()
err := init_plugin()
if err != nil {
logger.Println(err)
}
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
txt := scanner.Text()
dec := json.NewDecoder(strings.NewReader(txt))
msg := new(Message)
err := dec.Decode(msg)
if err != nil {
logger.Println(err)
}
switch msg.Command {
case "Broad.Open":
broad_id, err = pick_broad_id(msg.Content)
if err != nil {
logger.Println(msg.Command, err)
}
case "Got":
read_aloud(broad_id, msg.Content, m1, m2)
default:
logger.Println(msg.Command)
}
}
if err := scanner.Err(); err != nil {
logger.Println("reading standard input:", err)
}
}