-
Notifications
You must be signed in to change notification settings - Fork 1
/
sound.go
65 lines (58 loc) · 1.12 KB
/
sound.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
package main
import (
"regexp"
"strings"
"github.com/thelegy/koma-bot/sse"
)
type Sound struct {
Name string
Regex []string
Retweeted bool
regex *regexp.Regexp
}
func (s *Sound) CompileRegexpr() error {
reg := "(?i)" + strings.Join(s.Regex, "|")
regex, err := regexp.Compile(reg)
if err != nil {
return err
}
s.regex = regex
return nil
}
func (s *Sound) GetPosition(t Tweet) [][]int {
if s.Retweeted && t.RetweetedStatus == nil {
return nil
}
pos := s.regex.FindAllStringIndex(t.FullText, -1)
if len(pos) == 0 {
return nil
}
return pos
}
func tweetSounds(conf *Config, sse sse.Provider, t Tweet) {
positions := make([]*Sound, len(t.FullText)+1)
for sound := range conf.iterateSounds() {
pos := sound.GetPosition(t)
for _, p := range pos {
positions[p[0]] = sound
}
}
for _, sound := range positions {
if sound == nil {
continue
}
sse.EventStream <- sound
}
}
func processTweetSounds(conf *Config, sse sse.Provider) {
for {
c := <-sse.NewClients
for m := range c.Channel {
switch msg := m.(type) {
default:
case Tweet:
tweetSounds(conf, sse, msg)
}
}
}
}