Added config option to post only the article link without excerpt.

This commit is contained in:
Martin Dosch 2018-09-03 20:42:19 +02:00
parent 5a5014d449
commit 7e2c58256d
4 changed files with 24 additions and 14 deletions

View file

@ -45,6 +45,7 @@ If the flag `-config` is not used the configuration is expected at
"Muc": "muc-to-feed@conference.example.com", "Muc": "muc-to-feed@conference.example.com",
"MucNick": "feedbot", "MucNick": "feedbot",
"MaxArticles": 5, "MaxArticles": 5,
"NoExcerpt": false,
"Feeds": [ "https://www.debian.org/News/news", "Feeds": [ "https://www.debian.org/News/news",
"https://www.debian.org/security/dsa-long" ] "https://www.debian.org/security/dsa-long" ]
} }

View file

@ -5,6 +5,7 @@
"Muc": "muc-to-feed@conference.example.com", "Muc": "muc-to-feed@conference.example.com",
"MucNick": "feedbot", "MucNick": "feedbot",
"MaxArticles": 5, "MaxArticles": 5,
"NoExcerpt": false,
"Feeds": [ "https://www.debian.org/News/news", "Feeds": [ "https://www.debian.org/News/news",
"https://www.debian.org/security/dsa-long" ] "https://www.debian.org/security/dsa-long" ]
} }

View file

@ -36,6 +36,7 @@ func main() {
Muc string Muc string
MucNick string MucNick string
MaxArticles int MaxArticles int
NoExcerpt bool
Feeds []string Feeds []string
} }
@ -147,7 +148,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) output, err := getArticles(config.Feeds[i], config.MaxArticles, config.NoExcerpt)
if err != nil { if err != nil {
// Exit if an error occurs checking the feeds. // Exit if an error occurs checking the feeds.
log.Fatal(err) log.Fatal(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) (string, error) { func getArticles(feedURL string, max int, noExcerpt bool) (string, error) {
type feedCache struct { type feedCache struct {
LastChange string LastChange string
@ -194,19 +194,26 @@ func getArticles(feedURL string, max int) (string, error) {
// Remove redirects and tracking parameters from URL. // Remove redirects and tracking parameters from URL.
cleanURL, _ := removeTracking(article.Link) cleanURL, _ := removeTracking(article.Link)
// Strip HTML as we want to get plain text. // Only send title and link if option noExcerpt is set,
description, err := html2text.FromString(article.Description) // otherwise add the description.
if err != nil { if noExcerpt == true {
return "", err output = output + feed.Title + ": *" + article.Title + "*\n" +
} cleanURL
// 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.
description = strings.Replace(description, article.Link, cleanURL, -1)
output = output + feed.Title + ": *" + article.Title + "*\n\n" + description
} else { } else {
output = output + feed.Title + ": *" + article.Title + "*\n\n" + description + "\n" + cleanURL // Strip HTML as we want to get plain text.
description, err := html2text.FromString(article.Description)
if err != nil {
return "", err
}
// 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.
description = strings.Replace(description, article.Link, cleanURL, -1)
output = output + feed.Title + ": *" + article.Title + "*\n\n" + description
} else {
output = output + feed.Title + ": *" + article.Title + "*\n\n" + description + "\n" + cleanURL
}
} }
if i > 0 { if i > 0 {