-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.go
82 lines (70 loc) · 1.93 KB
/
subscription.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
package feedcrawler
import (
"regexp"
"github.com/mmcdole/gofeed"
)
// FeedID is an identifier of a feed.
type FeedID string
// Subscription is a subscription information of a feed.
type Subscription interface {
ID() FeedID
URI() string
Filter(*gofeed.Item) bool
}
// SimpleSubscription is a feed configuration to be subscribed.
type SimpleSubscription struct {
FeedID FeedID
FeedURI string
TitleFilter *regexp.Regexp
DescriptionFilter *regexp.Regexp
ContentFilter *regexp.Regexp
AuthorFilter *regexp.Regexp
CategoryFilter *regexp.Regexp
FilterFunc func(*gofeed.Item) bool
}
// ID returns the feed ID.
func (s *SimpleSubscription) ID() FeedID { return s.FeedID }
// URI returns the feed URI.
func (s *SimpleSubscription) URI() string { return s.FeedURI }
// Filter returns true if the item is acceptable.
func (s *SimpleSubscription) Filter(item *gofeed.Item) bool {
if s.AuthorFilter != nil && !matchAuthor(s.AuthorFilter, item.Author) {
return false
}
if s.CategoryFilter != nil && !matchCategories(s.CategoryFilter, item.Categories) {
return false
}
if s.TitleFilter != nil && !matchString(s.TitleFilter, item.Title) {
return false
}
if s.DescriptionFilter != nil && !matchString(s.DescriptionFilter, item.Description) {
return false
}
if s.ContentFilter != nil && !matchString(s.ContentFilter, item.Content) {
return false
}
if s.FilterFunc != nil && !s.FilterFunc(item) {
return false
}
return true
}
func matchAuthor(r *regexp.Regexp, author *gofeed.Person) bool {
if author == nil {
return false
}
return matchString(r, author.Name) || matchString(r, author.Email)
}
func matchCategories(r *regexp.Regexp, categories []string) bool {
for _, cat := range categories {
if matchString(r, cat) {
return true
}
}
return false
}
func matchString(r *regexp.Regexp, value string) bool {
if value != "" && r.MatchString(value) {
return true
}
return false
}