-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
200 lines (172 loc) · 4.39 KB
/
config.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
package main
import (
"bytes"
"fmt"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/chimeracoder/anaconda"
"github.com/fsnotify/fsnotify"
"github.com/spf13/viper"
)
type Config struct {
configViper *viper.Viper
soundViper *viper.Viper
soundMutex sync.RWMutex
soundMap map[string]*Sound
StreamInfo [2]*tweetStreamInfo
userIds []int64
}
func (conf *Config) IsDebugging() bool {
return conf.configViper.GetBool("debug")
}
func (conf *Config) loadStreamInfo() {
streamInfo1 := newTweetStreamInfo()
streamInfo2 := newTweetStreamInfo()
conf.configViper.UnmarshalKey("twitter.stream1", streamInfo1)
conf.configViper.UnmarshalKey("twitter.stream2", streamInfo2)
conf.StreamInfo[0] = streamInfo1
conf.StreamInfo[1] = streamInfo2
}
func (conf *Config) ResolveUserIds(api *anaconda.TwitterApi) {
if len(conf.userIds) > 0 {
return
}
var userNames bytes.Buffer
for _, tsi := range conf.StreamInfo {
for _, user := range tsi.Users {
userNames.WriteString(",")
userNames.WriteString(user)
}
}
userNames.Next(1)
val := url.Values{}
users, err := api.GetUsersLookup(userNames.String(), val)
if err != nil {
//log error
return
}
for _, user := range users {
conf.userIds = append(conf.userIds, user.Id)
}
}
func (conf *Config) GetTweetFilter() (string, string) {
var track bytes.Buffer
var follow bytes.Buffer
for _, tsi := range conf.StreamInfo {
for _, hashtag := range tsi.Hashtags {
track.WriteString(",#")
track.WriteString(hashtag)
}
}
track.Next(1)
for _, uid := range conf.userIds {
follow.WriteString(",")
follow.WriteString(strconv.FormatInt(uid, 10))
}
follow.Next(1)
return track.String(), follow.String()
}
func (conf *Config) StoreTweet(t Tweet) {
for _, tsi := range conf.StreamInfo {
if tsi.ContainsTweet(t) {
tsi.Add(&t)
}
}
}
func (conf *Config) GetTimetableInfo() TimetableInfo {
ti := TimetableInfo{}
v := conf.configViper.Sub("timetable")
keys := make(map[string]bool)
for _, key := range v.AllKeys() {
keys[strings.Split(key, ".")[0]] = true
}
for key := range keys {
tir := &TimetableInfoRow{}
err := v.UnmarshalKey(key, tir)
if err != nil {
fmt.Println(err)
continue
}
ti[tir.Start] = *tir
}
return ti
}
func (conf *Config) iterateSounds() <-chan *Sound {
sounds := make(chan *Sound)
go func(c chan<- *Sound) {
conf.soundMutex.RLock()
defer conf.soundMutex.RUnlock()
defer close(c)
timeout := time.After(100 * time.Millisecond)
for _, sound := range conf.soundMap {
select {
case c <- sound:
case <-timeout:
return
}
}
}(sounds)
return sounds
}
func (conf *Config) updateSounds() {
sounds := make(map[string]*Sound)
keys := make(map[string]bool)
for _, key := range conf.soundViper.AllKeys() {
keys[strings.Split(key, ".")[0]] = true
}
for key := range keys {
sound := &Sound{}
err := conf.soundViper.UnmarshalKey(key, sound)
if err != nil {
fmt.Println("Sound error:", err) //error handling, no panic
continue
}
sound.Name = key
err = sound.CompileRegexpr()
if err != nil {
fmt.Println("Sound error:", err) //error handling, no panic
continue
}
sounds[key] = sound
}
conf.soundMutex.Lock()
defer conf.soundMutex.Unlock()
conf.soundMap = sounds
}
func soundConfigChanged(conf *Config) func(fsnotify.Event) {
return func(_ fsnotify.Event) {
conf.updateSounds()
}
}
func loadConfig() *Config {
conf := &Config{
configViper: viper.New(),
soundViper: viper.New(),
}
conf.configViper.SetConfigName("koma_bot")
conf.configViper.AddConfigPath("/etc/koma_bot/")
conf.configViper.AddConfigPath(".")
err := conf.configViper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
conf.configViper.SetDefault("debug", false)
conf.loadStreamInfo()
conf.soundViper.SetConfigName("koma_bot_sounds")
conf.soundViper.AddConfigPath("/etc/koma_bot/")
conf.soundViper.AddConfigPath(".")
err = conf.soundViper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
conf.soundViper.OnConfigChange(soundConfigChanged(conf))
conf.soundViper.WatchConfig()
conf.updateSounds()
return conf
}
func (conf *Config) GetConfigString(name string) string {
return conf.configViper.GetString(name)
}