Add option to filter complete articles.

This commit is contained in:
Martin Dosch 2020-07-06 21:06:00 +02:00
parent c29f55c40e
commit 2ff9a3ca41
3 changed files with 30 additions and 3 deletions

View file

@ -50,6 +50,7 @@ If the flag `-config` is not used the configuration is expected at
"NoExcerpt": false,
"Quiet": false,
"Filter": [ "submitted by", "[link]" ],
"FilterMessage": [ "Block me!", "Block me too!" ],
"Feeds": [ "https://www.debian.org/News/news",
"https://www.debian.org/security/dsa-long",
"https://www.reddit.com/r/FDroidUpdates/new.rss" ]
@ -62,7 +63,8 @@ posted. `RefreshTime` defines the intervall for checking the feeds
in seconds.
With `Filter` you can specify strings for filtering out lines
beginning with those strings.
beginning with those strings, with `FilterMessage` you can filter
out complete postings containing the string.
`Contact` is for providing contact information about who is running
the bot.

View file

@ -18,7 +18,7 @@ import (
)
// Get new articles for specified feed.
func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (string, error) {
func getArticles(feedURL string, max int, noExcerpt bool, filter []string, filterMessage []string) (string, error) {
type feedCache struct {
LastChange string
@ -203,6 +203,18 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (stri
// Only send title and link if option noExcerpt is set,
// otherwise add the description.
if noExcerpt {
// Stop processing the title if it contains the string
// configured in FilterMessage
filterStrike := false
for _, filterString := range filterMessage {
if strings.Contains(feed.Title, filterString) {
filterStrike = true
}
}
if filterStrike {
continue
}
output = output + feed.Title + ": *" + article.Title + "*\n" +
cleanURL
} else {
@ -224,6 +236,18 @@ func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (stri
}
}
// Stop processing the article if it contains the string
// configured in FilterMessage
filterStrike := false
for _, filterString := range filterMessage {
if strings.Contains(description, filterString) {
filterStrike = true
}
}
if filterStrike {
continue
}
// Remove lines only consisting of "> "; thank you reddit.
description = strings.Replace(description, "> \n", "", -1)

View file

@ -23,6 +23,7 @@ type configuration struct {
Quiet bool
Contact string
Filter []string
FilterMessage []string
Feeds []string
}
@ -83,7 +84,7 @@ func main() {
// Check all configured feeds for new articles and send
// new articles to configured MUC.
for i := 0; i < len(config.Feeds); i++ {
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt, config.Filter)
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt, config.Filter, config.FilterMessage)
if err != nil {
// Exit if an error occurs checking the feeds.
log.Fatal("Error: Can't check feeds for new articles: ", err)