/
rss.go
170 lines (149 loc) · 4.34 KB
/
rss.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
package scheduler
import (
"context"
"sync"
"time"
"github.com/mmcdole/gofeed"
"go.uber.org/atomic"
"github.com/andatoshiki/toshiki-rssbot/internal/config"
"github.com/andatoshiki/toshiki-rssbot/internal/core"
"github.com/andatoshiki/toshiki-rssbot/internal/feed"
"github.com/andatoshiki/toshiki-rssbot/internal/log"
"github.com/andatoshiki/toshiki-rssbot/internal/model"
"github.com/andatoshiki/toshiki-rssbot/pkg/client"
)
// RssUpdateObserver Rss Update observer
type RssUpdateObserver interface {
SourceUpdate(*model.Source, []*model.Content, []*model.Subscribe)
SourceUpdateError(*model.Source)
}
// NewRssTask new RssUpdateTask
func NewRssTask(appCore *core.Core) *RssUpdateTask {
return &RssUpdateTask{
observerList: []RssUpdateObserver{},
core: appCore,
feedParser: appCore.FeedParser(),
httpClient: appCore.HttpClient(),
}
}
// RssUpdateTask rss update task
type RssUpdateTask struct {
observerList []RssUpdateObserver
isStop atomic.Bool
core *core.Core
feedParser *feed.FeedParser
httpClient *client.HttpClient
}
// Register 注册rss更新订阅者
func (t *RssUpdateTask) Register(observer RssUpdateObserver) {
t.observerList = append(t.observerList, observer)
}
// Stop scheduler
func (t *RssUpdateTask) Stop() {
t.isStop.Store(true)
}
// Start run scheduler
func (t *RssUpdateTask) Start() {
if config.RunMode == config.TestMode {
return
}
t.isStop.Store(false)
go func() {
for {
if t.isStop.Load() {
log.Info("RssUpdateTask stopped")
return
}
sources, err := t.core.GetSources(context.Background())
if err != nil {
log.Errorf("get sources failed, %v", err)
time.Sleep(time.Duration(config.UpdateInterval) * time.Minute)
continue
}
for _, source := range sources {
if source.ErrorCount >= config.ErrorThreshold {
continue
}
newContents, err := t.getSourceNewContents(source)
if err != nil {
if source.ErrorCount >= config.ErrorThreshold {
t.notifyAllObserverErrorUpdate(source)
}
continue
}
if len(newContents) > 0 {
subs, err := t.core.GetSourceAllSubscriptions(
context.Background(), source.ID,
)
if err != nil {
log.Errorf("get subscriptions failed, %v", err)
continue
}
t.notifyAllObserverUpdate(source, newContents, subs)
}
}
time.Sleep(time.Duration(config.UpdateInterval) * time.Minute)
}
}()
}
// getSourceNewContents 获取rss新内容
func (t *RssUpdateTask) getSourceNewContents(source *model.Source) ([]*model.Content, error) {
log.Debugf("fetch source [%d]%s update", source.ID, source.Link)
rssFeed, err := t.feedParser.ParseFromURL(context.Background(), source.Link)
if err != nil {
log.Errorf("unable to fetch feed, source %#v, err %v", source, err)
t.core.SourceErrorCountIncr(context.Background(), source.ID)
return nil, err
}
t.core.ClearSourceErrorCount(context.Background(), source.ID)
newContents, err := t.saveNewContents(source, rssFeed.Items)
if err != nil {
return nil, err
}
return newContents, nil
}
// saveNewContents generate content by fetcher item
func (t *RssUpdateTask) saveNewContents(
s *model.Source, items []*gofeed.Item,
) ([]*model.Content, error) {
var newItems []*gofeed.Item
for _, item := range items {
hashID := model.GenHashID(s.Link, item.GUID)
exist, err := t.core.ContentHashIDExist(context.Background(), hashID)
if err != nil {
log.Errorf("check item hash id failed, %v", err)
}
if exist {
// if exist, then end the process and skip
continue
}
newItems = append(newItems, item)
}
return t.core.AddSourceContents(context.Background(), s, newItems)
}
// notifyAllObserverUpdate notify all rss SourceUpdate observer
func (t *RssUpdateTask) notifyAllObserverUpdate(
source *model.Source, newContents []*model.Content, subscribes []*model.Subscribe,
) {
wg := sync.WaitGroup{}
for _, observer := range t.observerList {
wg.Add(1)
go func(o RssUpdateObserver) {
defer wg.Done()
o.SourceUpdate(source, newContents, subscribes)
}(observer)
}
wg.Wait()
}
// notifyAllObserverErrorUpdate notify all rss error SourceUpdate observer
func (t *RssUpdateTask) notifyAllObserverErrorUpdate(source *model.Source) {
wg := sync.WaitGroup{}
for _, observer := range t.observerList {
wg.Add(1)
go func(o RssUpdateObserver) {
defer wg.Done()
o.SourceUpdateError(source)
}(observer)
}
wg.Wait()
}