-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
concern.go
223 lines (203 loc) · 6.01 KB
/
concern.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
package youtube
import (
"errors"
"fmt"
"github.com/Sora233/DDBOT/lsp/concern"
"github.com/Sora233/DDBOT/lsp/concern_type"
"github.com/Sora233/DDBOT/lsp/mmsg"
localutils "github.com/Sora233/DDBOT/utils"
"github.com/Sora233/MiraiGo-Template/utils"
)
var logger = utils.GetModuleLogger("youtube")
type Concern struct {
*StateManager
}
func (c *Concern) Site() string {
return "youtube"
}
func (c *Concern) Types() []concern_type.Type {
return []concern_type.Type{Live, Video}
}
func (c *Concern) ParseId(s string) (interface{}, error) {
return s, nil
}
func (c *Concern) GetStateManager() concern.IStateManager {
return c.StateManager
}
func (c *Concern) Add(ctx mmsg.IMsgCtx, groupCode int64, _id interface{}, ctype concern_type.Type) (concern.IdentityInfo, error) {
id := _id.(string)
log := logger.WithFields(localutils.GroupLogFields(groupCode)).WithField("id", id)
err := c.StateManager.CheckGroupConcern(groupCode, id, ctype)
if err != nil {
return nil, err
}
info, err := c.FindOrLoad(id)
if err != nil {
log.Errorf("FindOrLoad error %v", err)
return nil, fmt.Errorf("查询channel信息失败 %v - %v", id, err)
}
_, err = c.StateManager.AddGroupConcern(groupCode, id, ctype)
if err != nil {
return nil, err
}
return concern.NewIdentity(info.ChannelId, info.ChannelName), nil
}
func (c *Concern) Remove(ctx mmsg.IMsgCtx, groupCode int64, _id interface{}, ctype concern_type.Type) (concern.IdentityInfo, error) {
id := _id.(string)
identity, _ := c.Get(id)
_, err := c.StateManager.RemoveGroupConcern(groupCode, id, ctype)
if identity == nil {
identity = concern.NewIdentity(_id, "unknown")
}
return identity, err
}
func (c *Concern) Get(id interface{}) (concern.IdentityInfo, error) {
info, err := c.FindInfo(id.(string), false)
if err != nil {
return nil, err
}
return concern.NewIdentity(info.ChannelId, info.ChannelName), nil
}
func (c *Concern) Stop() {
logger.Trace("正在停止youtube concern")
logger.Trace("正在停止youtube StateManager")
c.StateManager.Stop()
logger.Trace("youtube StateManager已停止")
logger.Trace("youtube concern已停止")
}
func (c *Concern) Start() error {
c.UseFreshFunc(c.fresh())
c.UseNotifyGeneratorFunc(c.notifyGenerator())
return c.StateManager.Start()
}
func (c *Concern) fresh() concern.FreshFunc {
return c.EmitQueueFresher(func(ctype concern_type.Type, id interface{}) ([]concern.Event, error) {
if ctype.ContainAny(Live.Add(Video)) {
channelId, ok := id.(string)
if !ok {
return nil, errors.New("canst fresh id to string failed")
}
return c.freshInfo(channelId)
}
return nil, fmt.Errorf("unknown concern_type %v", ctype.String())
})
}
func (c *Concern) notifyGenerator() concern.NotifyGeneratorFunc {
return func(groupCode int64, ievent concern.Event) (result []concern.Notify) {
switch event := ievent.(type) {
case *VideoInfo:
log := event.Logger()
if prev, err := c.StateManager.GetVideo(event.ChannelId, event.VideoId); err == nil {
if prev.VideoStatus == event.VideoStatus && prev.VideoType == event.VideoType &&
prev.VideoTimestamp == event.VideoTimestamp && prev.VideoTitle == event.VideoTitle {
log.Debugf("duplicate event")
return
}
}
if err := c.StateManager.AddVideo(event); err != nil {
log.Errorf("add video err %v", err)
}
notify := NewConcernNotify(groupCode, event)
result = append(result, notify)
if event.IsVideo() {
log.WithFields(localutils.GroupLogFields(groupCode)).Debugf("video notify")
} else if event.IsLive() {
if event.IsWaiting() {
log.WithFields(localutils.GroupLogFields(groupCode)).Debugf("live waiting notify")
} else if event.IsLiving() {
log.WithFields(localutils.GroupLogFields(groupCode)).Debugf("living notify")
}
}
}
return
}
}
func (c *Concern) freshInfo(channelId string) (result []concern.Event, err error) {
log := logger.WithField("channel_id", channelId)
oldInfo, _ := c.FindInfo(channelId, false)
newInfo, err := c.FindInfo(channelId, true)
if err != nil {
log.Errorf("load newInfo failed %v", err)
return
}
if oldInfo == nil {
// first load, just notify if living
for _, newV := range newInfo.VideoInfo {
if newV.IsLive() {
if newV.IsLiving() {
newV.liveStatusChanged = true
}
result = append(result, newV)
log.Debugf("first load live notify")
}
}
} else {
var notifyCount = 0
for _, newV := range newInfo.VideoInfo {
var found bool
for _, oldV := range oldInfo.VideoInfo {
if newV.VideoId == oldV.VideoId {
found = true
if newV.IsVideo() && oldV.IsLive() {
// 应该是下播了吧?
log.Debug("offline notify")
result = append(result, newV)
}
if newV.IsLive() && oldV.IsLive() {
if newV.IsWaiting() && oldV.IsWaiting() && newV.VideoTimestamp != oldV.VideoTimestamp {
// live time changed, notify
result = append(result, newV)
log.Debugf("live time change notify")
} else if newV.IsLiving() && oldV.IsWaiting() {
// live begin
newV.liveStatusChanged = true
result = append(result, newV)
log.Debugf("live begin notify")
} else if newV.VideoTitle != oldV.VideoTitle {
newV.liveTitleChanged = true
result = append(result, newV)
}
}
}
}
if !found {
// new video
if notifyCount == 0 {
result = append(result, newV)
log.Debugf("new video notify")
notifyCount += 1
// notify video most once
}
}
}
}
return
}
func (c *Concern) FindInfo(channelId string, load bool) (*Info, error) {
var info *Info
if load {
vi, err := XFetchInfo(channelId)
if err != nil {
return nil, err
}
info = NewInfo(vi)
c.StateManager.AddInfo(info)
}
if info != nil {
return info, nil
}
return c.GetInfo(channelId)
}
func (c *Concern) FindOrLoad(channelId string) (*Info, error) {
info, _ := c.FindInfo(channelId, false)
if info == nil {
return c.FindInfo(channelId, true)
} else {
return info, nil
}
}
func NewConcern(notify chan<- concern.Notify) *Concern {
return &Concern{
StateManager: NewStateManager(notify),
}
}