diff options
Diffstat (limited to '')
-rw-r--r-- | internal/feed/feed.go | 64 |
1 files changed, 59 insertions, 5 deletions
diff --git a/internal/feed/feed.go b/internal/feed/feed.go index cd906a2..5af4188 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -3,10 +3,13 @@ package feed import ( "fmt" "strings" + "sync" + "time" "github.com/mmcdole/gofeed" "github.com/Necoro/feed2imap-go/internal/config" + "github.com/Necoro/feed2imap-go/internal/log" ) type Feed struct { @@ -14,8 +17,9 @@ type Feed struct { Target []string Url string config.Options - feed *gofeed.Feed - items []feeditem + feed *gofeed.Feed + items []feeditem + cached CachedFeed } type feeditem struct { @@ -23,9 +27,18 @@ type feeditem struct { *gofeed.Item } -type Feeds map[string]*Feed +type Feeds struct { + feeds map[string]*Feed + cache Cache +} + +func NewFeeds() *Feeds { + return &Feeds{ + feeds: map[string]*Feed{}, + } +} -func (f Feeds) String() string { +func (feeds *Feeds) String() string { var b strings.Builder app := func(a ...interface{}) { _, _ = fmt.Fprint(&b, a...) @@ -33,7 +46,7 @@ func (f Feeds) String() string { app("Feeds [") first := true - for k, v := range f { + for k, v := range feeds.feeds { if !first { app(", ") } @@ -49,3 +62,44 @@ func (f Feeds) String() string { return b.String() } + +func (feeds *Feeds) Len() int { + return len(feeds.feeds) +} + +func (feeds *Feeds) Contains(name string) bool { + _, ok := feeds.feeds[name] + return ok +} + +func (feeds *Feeds) Set(name string, feed *Feed) { + feeds.feeds[name] = feed +} + +func (feeds *Feeds) Foreach(f func(*Feed)) { + for _, feed := range feeds.feeds { + f(feed) + } +} + +func (feeds *Feeds) ForeachGo(goFunc func(*Feed, *sync.WaitGroup)) { + var wg sync.WaitGroup + wg.Add(feeds.Len()) + + for _, feed := range feeds.feeds { + go goFunc(feed, &wg) + } + wg.Wait() +} + +func (feed *Feed) NeedsUpdate(updateTime time.Time) bool { + if !updateTime.IsZero() && int(time.Since(updateTime).Hours()) >= feed.MinFreq { + log.Printf("Feed '%s' does not need updating, skipping.", feed.Name) + return false + } + return true +} + +func (feed *Feed) Success() bool { + return feed.feed != nil +} |