mirror of
https://salsa.debian.org/mdosch/feed-to-muc.git
synced 2024-12-22 21:05:25 +01:00
Added filter option.
This commit is contained in:
parent
86a5c5f610
commit
800db2616e
2 changed files with 37 additions and 10 deletions
|
@ -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)
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue