aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/state.go
diff options
context:
space:
mode:
authorRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-22 22:54:43 +0100
committerRené 'Necoro' Neumann <necoro@necoro.eu>2021-02-22 22:54:43 +0100
commit7106d5a6e7585dce5fdd552cca30063dd352dc23 (patch)
tree88a9cb7150f86fcadb8f87e3a6d0892bf68c2251 /internal/feed/state.go
parentfb2aa9b1f04d509c8215c1fa6505a144482c343d (diff)
downloadfeed2imap-go-7106d5a6e7585dce5fdd552cca30063dd352dc23.tar.gz
feed2imap-go-7106d5a6e7585dce5fdd552cca30063dd352dc23.tar.bz2
feed2imap-go-7106d5a6e7585dce5fdd552cca30063dd352dc23.zip
Split cache and feed packages
Diffstat (limited to 'internal/feed/state.go')
-rw-r--r--internal/feed/state.go184
1 files changed, 0 insertions, 184 deletions
diff --git a/internal/feed/state.go b/internal/feed/state.go
deleted file mode 100644
index 364616f..0000000
--- a/internal/feed/state.go
+++ /dev/null
@@ -1,184 +0,0 @@
-package feed
-
-import (
- "encoding/json"
- "sync"
-
- "github.com/mmcdole/gofeed"
-
- "github.com/Necoro/feed2imap-go/pkg/config"
- "github.com/Necoro/feed2imap-go/pkg/log"
-)
-
-type State struct {
- feeds map[string]*Feed
- cache Cache
- cfg *config.Config
-}
-
-func (state *State) Foreach(f func(*Feed)) {
- for _, feed := range state.feeds {
- f(feed)
- }
-}
-
-func (state *State) ForeachGo(goFunc func(*Feed)) {
- var wg sync.WaitGroup
- wg.Add(len(state.feeds))
-
- f := func(feed *Feed, wg *sync.WaitGroup) {
- goFunc(feed)
- wg.Done()
- }
-
- for _, feed := range state.feeds {
- go f(feed, &wg)
- }
- wg.Wait()
-}
-
-func (state *State) LoadCache(fileName string, forceNew bool) error {
- var (
- cache Cache
- err error
- )
-
- if forceNew {
- cache, err = newCache()
- } else {
- cache, err = LoadCache(fileName)
- }
-
- if err != nil {
- return err
- }
- state.cache = cache
-
- for _, feed := range state.feeds {
- feed.cached = cache.findItem(feed)
- }
- return nil
-}
-
-func (state *State) StoreCache(fileName string) error {
- return state.cache.store(fileName)
-}
-
-func (state *State) UnlockCache() {
- _ = state.cache.Unlock()
-}
-
-func (state *State) Fetch() int {
- state.ForeachGo(handleFeed)
-
- ctr := 0
- for _, feed := range state.feeds {
- success := feed.FetchSuccessful()
- feed.cached.Checked(!success)
-
- if success {
- ctr++
- }
- }
-
- return ctr
-}
-
-func printItem(item *gofeed.Item) string {
- // analogous to gofeed.Feed.String
- json, _ := json.MarshalIndent(item, "", " ")
- return string(json)
-}
-
-func (feed *Feed) filterItems() []item {
- if feed.filter == nil {
- return feed.items
- }
-
- items := make([]item, 0, len(feed.items))
-
- for _, item := range feed.items {
- res, err := feed.filter.Run(item.Item)
- if err != nil {
- log.Errorf("Feed %s: Item %s: Error applying item filter: %s", feed.Name, printItem(item.Item), err)
- res = true // include
- }
-
- if res {
- if log.IsDebug() {
- log.Debugf("Filter '%s' matches for item %s", feed.ItemFilter, printItem(item.Item))
- }
- items = append(items, item)
- } else if log.IsDebug() { // printItem is not for free
- log.Debugf("Filter '%s' does not match for item %s", feed.ItemFilter, printItem(item.Item))
- }
- }
- return items
-}
-
-func filterFeed(feed *Feed) {
- if len(feed.items) > 0 {
- origLen := len(feed.items)
-
- log.Debugf("Filtering %s. Starting with %d items", feed.Name, origLen)
-
- items := feed.filterItems()
- newLen := len(items)
- if newLen < origLen {
- log.Printf("Item filter on %s: Reduced from %d to %d items.", feed.Name, origLen, newLen)
- origLen = newLen
- }
-
- items = feed.cached.filterItems(items, feed.IgnHash, feed.AlwaysNew)
- feed.items = items
-
- newLen = len(feed.items)
- if newLen < origLen {
- log.Printf("Filtered %s. Reduced from %d to %d items.", feed.Name, origLen, newLen)
- } else {
- log.Printf("Filtered %s, no reduction.", feed.Name)
- }
-
- } else {
- log.Debugf("No items for %s. No filtering.", feed.Name)
- }
-}
-
-func (state *State) Filter() {
- if log.IsDebug() {
- // single threaded for better output
- state.Foreach(filterFeed)
- } else {
- state.ForeachGo(filterFeed)
- }
-}
-
-func NewState(cfg *config.Config) (*State, error) {
- state := State{
- feeds: map[string]*Feed{},
- cache: Cache{}, // loaded later on
- cfg: cfg,
- }
-
- for name, parsedFeed := range cfg.Feeds {
- feed, err := Create(parsedFeed, cfg.GlobalOptions)
- if err != nil {
- return nil, err
- }
- state.feeds[name] = feed
- }
-
- return &state, nil
-}
-
-func (state *State) RemoveUndue() {
- for name, feed := range state.feeds {
- if feed.Disable || !feed.NeedsUpdate(feed.cached.Last()) {
- delete(state.feeds, name)
- }
- }
-}
-
-func (state *State) NumFeeds() int {
- return len(state.feeds)
-}