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
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)

View file

@ -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,18 +224,44 @@ func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
// Remove lines only consisting of "> "; thank you reddit.
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.
// 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
description = ""
// description = ""
// 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.
if line != "" {
description = description + line + "\n"
}
}
// 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 {