-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
filter.go
258 lines (233 loc) · 8.48 KB
/
filter.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package filters
import (
"crypto/sha256"
"fmt"
"math/rand"
"regexp"
"strconv"
"strings"
"time"
"github.com/slurdge/goeland/config"
"github.com/slurdge/goeland/internal/goeland"
"github.com/slurdge/goeland/log"
)
type filter struct {
help string
filterFunc func(source *goeland.Source, params *filterParams)
}
type filterParams struct {
args []string
config config.Provider
}
const defaultHeaderLevel = 2
var filters = map[string]filter{
"all": {"Default, include all entries", filterAll},
"none": {"Removes all entries", filterNone},
"first": {"Keep only the first (optional: N) entry. Use either 'first' or 'first(3')", filterFirst},
"last": {"Keep only the last (optional: N) entry. Use either 'last' or 'last(3')", filterLast},
"random": {"Keep 1 or more random entries. Use either 'random' or 'random(5)'", filterRandom},
"reverse": {"Reverse the order of the entries", filterReverse},
"today": {"Keep only the entries for today", filterToday},
"lasthours": {"Keep only the entries that are from the X last hours (default 24)", filterLastHour},
"digest": {"Make a digest of all entries (optional heading level, default is " + fmt.Sprint(defaultHeaderLevel) + ")", filterDigest},
"combine": {"Combine all the entries into one source and use the first entry title as source title. Useful for merge sources", filterCombine},
"links": {`Rewrite relative links src="// and href="// to have an https:// prefix`, filterRelativeLinks},
"embedimage": {`Embed a picture if the entry has an attachment with a type of picture (optional position: top|bottom|left|right, default is top)`, filterEmbedImage},
"replace": {`Replace a string with another. Use with an argument like this: replace(myreplace) and define
[replace.myreplace]
from="A string"
to="Another string"
in your config file.`, filterReplace},
"includelink": {"Include the link of entries in the digest form", filterIncludeLink},
"language": {"Keep only the specified languages (best effort detection), use like this: language(en,de)", filterLanguage},
"unseen": {"Keep only unseen entry", filterUnSeen},
"lebrief": {"Deprecated. Use retrieve(div.content) instead. Retrieves the full excerpts for Next INpact's Lebrief", filterLeBrief},
"retrieve": {"Retrieves the full content from a goquery", filterRetrieveContent},
"untrack": {"Removes feedburner pixel tracking", filterUntrack},
}
func GetFiltersHelp() string {
lines := []string{}
for name, value := range filters {
line := fmt.Sprintf("\t- %s: %s", name, value.help)
lines = append(lines, line)
}
return strings.Join(lines, "\n")
}
func filterAll(source *goeland.Source, params *filterParams) {
}
func filterNone(source *goeland.Source, params *filterParams) {
source.Entries = nil
}
func filterFirst(source *goeland.Source, params *filterParams) {
number := extractNumber(source, params)
source.Entries = source.Entries[:number]
}
func filterLast(source *goeland.Source, params *filterParams) {
number := extractNumber(source, params)
source.Entries = source.Entries[len(source.Entries)-number:]
}
func filterRandom(source *goeland.Source, params *filterParams) {
number := extractNumber(source, params)
rand.Shuffle(len(source.Entries), func(i, j int) {
source.Entries[i], source.Entries[j] = source.Entries[j], source.Entries[i]
})
source.Entries = source.Entries[:number]
}
func extractNumber(source *goeland.Source, params *filterParams) int {
number := 1
if len(params.args) > 0 {
number, _ = strconv.Atoi(params.args[0])
}
if number <= 0 {
number = 1
}
if number > len(source.Entries) {
number = len(source.Entries)
}
return number
}
func filterReverse(source *goeland.Source, params *filterParams) {
for i, j := 0, len(source.Entries)-1; i < j; i, j = i+1, j-1 {
source.Entries[i], source.Entries[j] = source.Entries[j], source.Entries[i]
}
}
func filterLastHour(source *goeland.Source, params *filterParams) {
hours := 24
if len(params.args) > 0 {
hours, _ = strconv.Atoi(params.args[0])
}
var current int
for _, entry := range source.Entries {
if entry.Date.Before(time.Now().Add(time.Hour * time.Duration(-hours))) {
continue
}
source.Entries[current] = entry
current++
}
source.Entries = source.Entries[:current]
}
func filterToday(source *goeland.Source, params *filterParams) {
var current int
for _, entry := range source.Entries {
if entry.Date.Day() != time.Now().Day() {
continue
}
source.Entries[current] = entry
current++
}
source.Entries = source.Entries[:current]
}
func filterDigestGeneric(source *goeland.Source, level int, useFirstEntryTitle bool) {
if len(source.Entries) < 1 {
return
}
digest := goeland.Entry{}
digest.Title = fmt.Sprintf("Digest for %s", source.Title)
if useFirstEntryTitle && len(source.Entries) > 0 {
digest.Title = source.Entries[0].Title
}
content := ""
for _, entry := range source.Entries {
if entry.IncludeLink {
content += fmt.Sprintf(`<h%d><a href="%s">%s</a></h%d>`, level, entry.URL, entry.Title, level)
} else {
content += fmt.Sprintf("<h%d>%s</h%d>", level, entry.Title, level)
}
content += entry.Content
}
h := sha256.New()
h.Write([]byte(content))
digest.UID = fmt.Sprintf("%x", h.Sum(nil))
digest.Date = time.Now()
digest.Content = content
source.Entries = []goeland.Entry{digest}
}
func filterDigest(source *goeland.Source, params *filterParams) {
args := params.args
level := defaultHeaderLevel
if len(args) > 0 {
level, _ = strconv.Atoi(args[0])
}
filterDigestGeneric(source, level, false)
}
func filterCombine(source *goeland.Source, params *filterParams) {
args := params.args
level := defaultHeaderLevel
if len(args) > 0 {
level, _ = strconv.Atoi(args[0])
}
filterDigestGeneric(source, level, true)
}
func filterRelativeLinks(source *goeland.Source, params *filterParams) {
re := regexp.MustCompile(`(src|href)\s*=('|")\/\/`)
for i, entry := range source.Entries {
entry.Content = re.ReplaceAllString(entry.Content, "${1}=${2}https://")
source.Entries[i] = entry
}
}
func filterReplace(source *goeland.Source, params *filterParams) {
key := params.args[0]
config := params.config
from := config.GetString(fmt.Sprintf("replace.%s.from", key))
to := config.GetString(fmt.Sprintf("replace.%s.to", key))
for i, entry := range source.Entries {
entry.Content = strings.ReplaceAll(entry.Content, from, to)
source.Entries[i] = entry
}
}
func filterIncludeLink(source *goeland.Source, params *filterParams) {
for i := range source.Entries {
source.Entries[i].IncludeLink = true
}
}
func filterEmbedImage(source *goeland.Source, params *filterParams) {
args := params.args
positions := []string{"top", "bottom", "left", "right"}
position := 0
if len(args) > 0 {
for i, v := range positions {
if v == args[0] {
position = i
}
}
}
for i, entry := range source.Entries {
imageLink := fmt.Sprintf(`<img src="%s" class="%s">`, entry.ImageURL, positions[position])
switch position {
case 0:
entry.Content = imageLink + entry.Content
case 1:
entry.Content = entry.Content + imageLink
case 2:
entry.Content = imageLink + entry.Content + `<br style="clear:both" />`
case 3:
entry.Content = imageLink + entry.Content + `<br style="clear:both" />`
}
source.Entries[i] = entry
}
}
// FilterSource filters a source according to the config
func FilterSource(source *goeland.Source, config config.Provider) {
log.Infof("Retrieved %v feeds for source %v", len(source.Entries), source.Name)
filterNames := config.GetStringSlice(fmt.Sprintf("sources.%s.filters", source.Name))
for _, filterName := range filterNames {
filterShort := filterName
args := []string{}
if strings.Contains(filterName, "(") {
filterShort = strings.Split(filterName, "(")[0]
args = strings.Split(strings.ReplaceAll(strings.Split(filterName, "(")[1], ")", ""), ",")
for i, arg := range args {
args[i] = strings.TrimSpace(arg)
}
}
if filter, found := filters[filterShort]; found {
log.Debugf("Executing %s filter with args: %v", filterShort, args)
params := filterParams{args: args, config: config}
filter.filterFunc(source, ¶ms)
} else {
log.Errorf("unknown filter: %s\n", filterName)
}
log.Infof("After %s: %v feeds", filterName, len(source.Entries))
log.Debugf("After %s: %+v", filterName, source.Entries)
}
}