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,19 +224,45 @@ 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)
// To make the message look not so bloated we remove double newlines. // Split article description/content in single lines.
// Split the article description/content into fragments between double newlines. lines := strings.Split(description, "\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. // Get amount of lines in description/content.
for _, line := range fragments { descriptionLength := len(lines)
// Only if the only content is not empty. for i, line := range lines {
// Remove empty lines to safe space.
if line != "" { if line != "" {
description = description + line + "\n" // 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.
// Split the article description/content into fragments between double newlines.
// fragments := strings.Split(description, "\n\n")
// Empty article description/content
// description = ""
// Fill article description/content with the fragments separated by one newline.
// for _, line := range fragments {
// Only if the only content is not empty.
// if line != "" {
// 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 {
// Replace article link with URL cleaned from redirects and trackers. // Replace article link with URL cleaned from redirects and trackers.