From 800db2616edfeb22aabe4aca44b220b3d06690d5 Mon Sep 17 00:00:00 2001 From: Martin Dosch Date: Fri, 31 May 2019 09:50:46 +0200 Subject: [PATCH] Added filter option. --- feed-to-muc.go | 3 ++- getarticles.go | 44 +++++++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/feed-to-muc.go b/feed-to-muc.go index 611763a..b0a5a6b 100644 --- a/feed-to-muc.go +++ b/feed-to-muc.go @@ -38,6 +38,7 @@ func main() { MaxArticles int RefreshTime time.Duration NoExcerpt bool + Filter []string Feeds []string } @@ -149,7 +150,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) + output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt, config.Filter) if err != nil { // Exit if an error occurs checking the feeds. log.Fatal("Error: Can't check feeds for new articles: ", err) diff --git a/getarticles.go b/getarticles.go index 7b089da..e740ab3 100644 --- a/getarticles.go +++ b/getarticles.go @@ -18,7 +18,7 @@ import ( ) // 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 { LastChange string @@ -224,19 +224,45 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) { // Remove lines only consisting of "> "; thank you reddit. description = strings.Replace(description, "> \n", "", -1) - // 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 + // Split article description/content in single lines. + lines := strings.Split(description, "\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. + // Get amount of lines in description/content. + descriptionLength := len(lines) + for i, line := range lines { + // Remove empty lines to safe space. 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). if strings.Contains(description, article.Link) == true { // Replace article link with URL cleaned from redirects and trackers.