-
Notifications
You must be signed in to change notification settings - Fork 1
/
feed.go
85 lines (74 loc) · 1.55 KB
/
feed.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
package feed
import (
"crypto/md5"
"strings"
strip "github.com/grokify/html-strip-tags-go"
"github.com/mmcdole/gofeed"
conf "github.com/dmvass/rfeed/config"
)
// Messanger interface for clients
type Messanger interface {
Send(i *Item)
Check() bool
}
// Item can validate items and send to channels
type Item struct {
Title, Link string
Origin *gofeed.Item
}
// NewItem constructor
func NewItem(feedItem *gofeed.Item) *Item {
item := &Item{
Title: strip.StripTags(feedItem.Title),
Link: feedItem.Link,
Origin: feedItem,
}
return item
}
// GetMD5Hash return hashable link
func (i *Item) GetMD5Hash() []byte {
hasher := md5.New()
hasher.Write([]byte(i.Link))
return hasher.Sum(nil)
}
// Send message to all channels
func (i *Item) Send(clients *[]Messanger) {
for _, client := range *clients {
go client.Send(i)
}
}
// Read resource
func Read(url string) (*gofeed.Feed, error) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
return nil, err
}
return feed, err
}
// FindItems return filtered items
func FindItems(feed *gofeed.Feed) (matchItems []*gofeed.Item) {
for _, item := range feed.Items {
if skipItem(item) {
continue
}
matchItems = append(matchItems, item)
}
return
}
func skipItem(item *gofeed.Item) bool {
contains := func(categories []string, tag string) bool {
for _, c := range categories {
if strings.EqualFold(c, tag) {
return true
}
}
return false
}
for _, tag := range conf.Settings.Tags {
if contains(item.Categories, tag) {
return false
}
}
return true
}