From ff4fd6e75a85f8eca680065a8ec194f751ce6875 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Sun, 13 Mar 2022 06:41:54 +0200 Subject: [PATCH] Allow sleeping between feed-fetches. Added support for a per-feed `sleep` setting, which can force a sleep after making a feed-request. This was inspired by #83. --- config_cmd.go | 1 + processor/processor.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/config_cmd.go b/config_cmd.go index c8dc011..92ef889 100644 --- a/config_cmd.go +++ b/config_cmd.go @@ -117,6 +117,7 @@ include-title | Include only items with title matching the given regular-express notify | Comma-delimited list of emails to send notifications to (if set, | replaces the emails set in the cron/daemon command). retry | The maximum number of times to retry a failing HTTP-fetch. +sleep | Sleep the specified number of seconds, after making the request. template | The path to a feed-specific email template to use. user-agent | Configure a specific User-Agent when making HTTP requests. diff --git a/processor/processor.go b/processor/processor.go index 4825215..b164035 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -11,7 +11,9 @@ package processor import ( "fmt" "regexp" + "strconv" "strings" + "time" "github.com/k3a/html2text" "github.com/skx/rss2email/configfile" @@ -67,6 +69,9 @@ func (p *Processor) ProcessFeeds(recipients []string) []error { // we'll prefer if available. feedRecipients := recipients + // Should we sleep after getting this feed? + sleep := 0 + // For each option for _, opt := range entry.Options { @@ -81,6 +86,19 @@ func (p *Processor) ProcessFeeds(recipients []string) []error { feedRecipients[i] = strings.TrimSpace(feedRecipients[i]) } } + + // Sleep setting? + if opt.Name == "sleep" { + + // Convert the value, and if there was + // no error save it away. + num, err := strconv.Atoi(opt.Value) + if err != nil { + fmt.Printf("WARNING: %s:%s - failed to parse as sleep-delay %s\n", opt.Name, opt.Value, err.Error()) + } else { + sleep = num + } + } } // Process this specific entry. @@ -88,6 +106,11 @@ func (p *Processor) ProcessFeeds(recipients []string) []error { if err != nil { errors = append(errors, fmt.Errorf("error processing %s - %s", entry.URL, err)) } + + // If we're supposed to sleep, do so + if sleep != 0 { + time.Sleep(time.Duration(sleep) * time.Second) + } } // Prune old state files, unless we saw an error.