Added filter option.

This commit is contained in:
Martin Dosch 2019-05-31 09:50:46 +02:00
parent 86a5c5f610
commit 800db2616e
2 changed files with 37 additions and 10 deletions

View file

@ -38,6 +38,7 @@ func main() {
MaxArticles int MaxArticles int
RefreshTime time.Duration RefreshTime time.Duration
NoExcerpt bool NoExcerpt bool
Filter []string
Feeds []string Feeds []string
} }
@ -149,7 +150,7 @@ func main() {
// Check all configured feeds for new articles and send // Check all configured feeds for new articles and send
// new articles to configured MUC. // new articles to configured MUC.
for i := 0; i < len(config.Feeds); i++ { for i := 0; i < len(config.Feeds); i++ {
output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt) output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt, config.Filter)
if err != nil { if err != nil {
// Exit if an error occurs checking the feeds. // Exit if an error occurs checking the feeds.
log.Fatal("Error: Can't check feeds for new articles: ", err) log.Fatal("Error: Can't check feeds for new articles: ", err)

View file

@ -18,7 +18,7 @@ import (
) )
// Get new articles for specified feed. // Get new articles for specified feed.
func getArticles(feedURL string, max int, noExcerpt bool) (string, error) { func getArticles(feedURL string, max int, noExcerpt bool, filter []string) (string, error) {
type feedCache struct { type feedCache struct {
LastChange string LastChange string
@ -224,18 +224,44 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
// Remove lines only consisting of "> "; thank you reddit. // Remove lines only consisting of "> "; thank you reddit.
description = strings.Replace(description, "> \n", "", -1) description = strings.Replace(description, "> \n", "", -1)
// Split article description/content in single lines.
lines := strings.Split(description, "\n")
// Empty article description/content.
description = ""
// Get amount of lines in description/content.
descriptionLength := len(lines)
for i, line := range lines {
// Remove empty lines to safe space.
if line != "" {
// Remove lines starting with one of the defined filters.
filterStrike := false
for _, filterString := range filter {
if strings.HasPrefix(line, filterString) == true {
filterStrike = true
}
}
if filterStrike == false {
description = description + line
// Add new line, except for the last line.
if i < descriptionLength-1 {
description = description + "\n"
}
}
}
}
// To make the message look not so bloated we remove double newlines. // To make the message look not so bloated we remove double newlines.
// Split the article description/content into fragments between double newlines. // Split the article description/content into fragments between double newlines.
fragments := strings.Split(description, "\n\n") // fragments := strings.Split(description, "\n\n")
// Empty article description/content // Empty article description/content
description = "" // description = ""
// Fill article description/content with the fragments separated by one newline. // Fill article description/content with the fragments separated by one newline.
for _, line := range fragments { // for _, line := range fragments {
// Only if the only content is not empty. // Only if the only content is not empty.
if line != "" { // if line != "" {
description = description + line + "\n" // description = description + line + "\n"
} // }
} //}
// Only append article link if it is not yet contained in description (e.g. read more: URL). // Only append article link if it is not yet contained in description (e.g. read more: URL).
if strings.Contains(description, article.Link) == true { if strings.Contains(description, article.Link) == true {