-
-
Notifications
You must be signed in to change notification settings - Fork 70
/
concern.go
218 lines (202 loc) · 5.73 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
package youtube
import (
"errors"
"fmt"
"github.com/Logiase/MiraiGo-Template/utils"
"github.com/Sora233/DDBOT/concern"
"github.com/Sora233/DDBOT/lsp/concern_manager"
)
var logger = utils.GetModuleLogger("youtube")
type Concern struct {
*StateManager
eventChan chan ConcernEvent
notify chan<- concern.Notify
}
func (c *Concern) Add(groupCode int64, id string, ctype concern.Type) (info *Info, err error) {
log := logger.WithField("group_code", groupCode)
err = c.StateManager.CheckGroupConcern(groupCode, id, ctype)
if err != nil {
if err == concern_manager.ErrAlreadyExists {
return nil, errors.New("已经watch过了")
}
return nil, err
}
videoInfo, err := XFetchInfo(id)
if err != nil {
log.WithField("id", id).Errorf("XFetchInfo failed %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 NewInfo(videoInfo), nil
}
func (c *Concern) ListWatching(groupCode int64, ctype concern.Type) ([]*UserInfo, []concern.Type, error) {
log := logger.WithField("group_code", groupCode)
ids, ctypes, err := c.StateManager.ListByGroup(groupCode, func(id interface{}, p concern.Type) bool {
return p.ContainAny(ctype)
})
if err != nil {
return nil, nil, err
}
var result = make([]*UserInfo, 0, len(ids))
var resultTypes = make([]concern.Type, 0, len(ids))
for index, id := range ids {
info, err := c.findOrLoad(id.(string))
if err != nil {
log.WithField("id", id.(string)).Errorf("findInfo failed %v", err)
continue
}
result = append(result, NewUserInfo(info.ChannelId, info.ChannelName))
resultTypes = append(resultTypes, ctypes[index])
}
return result, resultTypes, nil
}
func (c *Concern) Start() {
if err := c.StateManager.Start(); err != nil {
logger.Errorf("state manager start err %v", err)
}
go c.notifyLoop()
go c.EmitFreshCore("youtube", func(ctype concern.Type, id interface{}) error {
if ctype.ContainAny(concern.YoutubeLive | concern.YoutubeVideo) {
channelId, ok := id.(string)
if !ok {
return errors.New("canst fresh id to string failed")
}
c.freshInfo(channelId)
}
return nil
})
}
func (c *Concern) notifyLoop() {
for ievent := range c.eventChan {
event := ievent.(*VideoInfo)
log := logger.WithField("channel_id", event.ChannelId).
WithField("video_id", event.VideoId).
WithField("video_type", event.VideoType.String()).
WithField("video_title", event.VideoTitle).
WithField("video_status", event.VideoStatus.String())
if prev, err := c.StateManager.GetVideo(event.ChannelId, event.VideoId); err == nil {
if prev.VideoStatus == event.VideoStatus && prev.VideoType == event.VideoType {
log.Debugf("duplicate event")
continue
}
}
log.Debugf("debug event")
if err := c.StateManager.AddVideo(event); err != nil {
log.Errorf("add video err %v", err)
}
groups, _, idTypes, err := c.StateManager.List(func(groupCode int64, id interface{}, p concern.Type) bool {
return id.(string) == event.ChannelId && p.ContainAny(concern.YoutubeLive|concern.YoutubeVideo)
})
if err != nil {
logger.Errorf("list id failed %v", err)
continue
}
for index, groupCode := range groups {
var doNotify bool
ctype := idTypes[index]
if ctype.ContainAny(concern.YoutubeLive) && event.IsLive() {
notify := NewConcernNotify(groupCode, event)
c.notify <- notify
doNotify = true
}
if ctype.ContainAny(concern.YoutubeVideo) && event.IsVideo() {
notify := NewConcernNotify(groupCode, event)
c.notify <- notify
doNotify = true
}
if doNotify {
if event.IsVideo() {
log.Debugf("video notify")
} else if event.IsLive() {
if event.IsWaiting() {
log.Debugf("live waiting notify")
} else if event.IsLiving() {
log.Debugf("living notify")
}
}
}
}
}
}
func (c *Concern) freshInfo(channelId string) {
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() {
c.eventChan <- 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.IsLive() && oldV.IsLive() {
if newV.IsWaiting() && oldV.IsWaiting() && newV.VideoTimestamp != oldV.VideoTimestamp {
// live time changed, notify
c.eventChan <- newV
log.Debugf("live time change notify")
} else if newV.IsLiving() && oldV.IsWaiting() {
// live begin
c.eventChan <- newV
log.Debugf("live begin notify")
}
// any other case?
}
}
}
if !found {
// new video
if notifyCount == 0 {
c.eventChan <- newV
log.Debugf("new video notify")
notifyCount += 1
// notify video most once
}
}
}
}
}
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: notify,
eventChan: make(chan ConcernEvent, 500),
}
}