diff options
Diffstat (limited to 'internal/feed')
-rw-r--r-- | internal/feed/cache.go | 42 | ||||
-rw-r--r-- | internal/feed/feed.go | 80 | ||||
-rw-r--r-- | internal/feed/mail.go | 2 | ||||
-rw-r--r-- | internal/feed/parse.go | 18 | ||||
-rw-r--r-- | internal/feed/state.go | 76 | ||||
-rw-r--r-- | internal/feed/template/template.go | 2 |
6 files changed, 101 insertions, 119 deletions
diff --git a/internal/feed/cache.go b/internal/feed/cache.go index 4f27144..411ed47 100644 --- a/internal/feed/cache.go +++ b/internal/feed/cache.go @@ -9,7 +9,7 @@ import ( "os" "time" - "github.com/Necoro/feed2imap-go/internal/log" + "github.com/Necoro/feed2imap-go/pkg/log" ) const ( @@ -77,7 +77,7 @@ func (cache *v1Cache) Version() byte { return cache.version } -func New() Cache { +func newCache() Cache { cache := v1Cache{ Ids: map[feedDescriptor]feedId{}, Feeds: map[feedId]*cachedFeed{}, @@ -90,7 +90,7 @@ func New() Cache { func cacheForVersion(version byte) (Cache, error) { switch version { case 1: - return New(), nil + return newCache(), nil default: return nil, fmt.Errorf("unknown cache version '%d'", version) } @@ -114,20 +114,20 @@ func (cache *v1Cache) findItem(feed *Feed) CachedFeed { return feed.cached.(*cachedFeed) } - fId := feedDescriptor{Name: feed.Name, Url: feed.Url} - id, ok := cache.Ids[fId] + fDescr := feed.descriptor() + id, ok := cache.Ids[fDescr] if !ok { var otherId feedDescriptor changed := false for otherId, id = range cache.Ids { - if otherId.Name == fId.Name { - log.Warnf("Feed %s seems to have changed URLs: New '%s', old '%s'. Updating.", - fId.Name, fId.Url, otherId.Url) + if otherId.Name == fDescr.Name { + log.Warnf("Feed %s seems to have changed URLs: newCache '%s', old '%s'. Updating.", + fDescr.Name, fDescr.Url, otherId.Url) changed = true break - } else if otherId.Url == fId.Url { - log.Warnf("Feed with URL '%s' seems to have changed its name: New '%s', old '%s'. Updating", - fId.Url, fId.Name, otherId.Name) + } else if otherId.Url == fDescr.Url { + log.Warnf("Feed with URL '%s' seems to have changed its name: newCache '%s', old '%s'. Updating", + fDescr.Url, fDescr.Name, otherId.Name) changed = true break } @@ -139,7 +139,7 @@ func (cache *v1Cache) findItem(feed *Feed) CachedFeed { cache.NextId++ } - cache.Ids[fId] = id + cache.Ids[fDescr] = id } item := cache.getItem(id) @@ -147,8 +147,7 @@ func (cache *v1Cache) findItem(feed *Feed) CachedFeed { return item } -func (feeds *Feeds) StoreCache(fileName string) error { - cache := feeds.cache +func storeCache(cache Cache, fileName string) error { if cache == nil { return fmt.Errorf("trying to store nil cache") } @@ -178,25 +177,12 @@ func (feeds *Feeds) StoreCache(fileName string) error { return nil } -func (feeds *Feeds) LoadCache(fileName string) error { - cache, err := loadCache(fileName) - if err != nil { - return err - } - feeds.cache = cache - - for _, feed := range feeds.feeds { - feed.cached = cache.findItem(feed) - } - return nil -} - func loadCache(fileName string) (Cache, error) { f, err := os.Open(fileName) if err != nil { if errors.Is(err, os.ErrNotExist) { // no cache there yet -- make new - return New(), nil + return newCache(), nil } return nil, fmt.Errorf("opening cache at '%s': %w", fileName, err) } diff --git a/internal/feed/feed.go b/internal/feed/feed.go index 5af4188..c7fdd5f 100644 --- a/internal/feed/feed.go +++ b/internal/feed/feed.go @@ -1,22 +1,16 @@ 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" + "github.com/Necoro/feed2imap-go/pkg/config" + "github.com/Necoro/feed2imap-go/pkg/log" ) type Feed struct { - Name string - Target []string - Url string - config.Options + config.Feed feed *gofeed.Feed items []feeditem cached CachedFeed @@ -27,73 +21,15 @@ type feeditem struct { *gofeed.Item } -type Feeds struct { - feeds map[string]*Feed - cache Cache -} - -func NewFeeds() *Feeds { - return &Feeds{ - feeds: map[string]*Feed{}, - } -} - -func (feeds *Feeds) String() string { - var b strings.Builder - app := func(a ...interface{}) { - _, _ = fmt.Fprint(&b, a...) - } - app("Feeds [") - - first := true - for k, v := range feeds.feeds { - if !first { - app(", ") - } - app(`"`, k, `"`, ": ") - if v == nil { - app("<nil>") - } else { - _, _ = fmt.Fprintf(&b, "%+v", *v) - } - first = false - } - app("]") - - 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) +func (feed *Feed) descriptor() feedDescriptor { + return feedDescriptor{ + Name: feed.Name, + Url: feed.Url, } - wg.Wait() } func (feed *Feed) NeedsUpdate(updateTime time.Time) bool { - if !updateTime.IsZero() && int(time.Since(updateTime).Hours()) >= feed.MinFreq { + if !updateTime.IsZero() && int(time.Since(updateTime).Hours()) >= *feed.MinFreq { log.Printf("Feed '%s' does not need updating, skipping.", feed.Name) return false } diff --git a/internal/feed/mail.go b/internal/feed/mail.go index ecc011f..d07f8cf 100644 --- a/internal/feed/mail.go +++ b/internal/feed/mail.go @@ -8,8 +8,8 @@ import ( "github.com/emersion/go-message/mail" - "github.com/Necoro/feed2imap-go/internal/config" "github.com/Necoro/feed2imap-go/internal/feed/template" + "github.com/Necoro/feed2imap-go/pkg/config" ) func address(name, address string) []*mail.Address { diff --git a/internal/feed/parse.go b/internal/feed/parse.go index 6deebb2..afca971 100644 --- a/internal/feed/parse.go +++ b/internal/feed/parse.go @@ -8,7 +8,7 @@ import ( "github.com/mmcdole/gofeed" - "github.com/Necoro/feed2imap-go/internal/log" + "github.com/Necoro/feed2imap-go/pkg/log" ) func context() (ctxt.Context, ctxt.CancelFunc) { @@ -41,19 +41,3 @@ func handleFeed(feed *Feed, group *sync.WaitGroup) { log.Error(err) } } - -func (feeds Feeds) Parse() int { - feeds.ForeachGo(handleFeed) - - ctr := 0 - for _, feed := range feeds.feeds { - success := feed.Success() - feed.cached.Checked(!success) - - if success { - ctr++ - } - } - - return ctr -} diff --git a/internal/feed/state.go b/internal/feed/state.go new file mode 100644 index 0000000..9a6f836 --- /dev/null +++ b/internal/feed/state.go @@ -0,0 +1,76 @@ +package feed + +import ( + "sync" + + "github.com/Necoro/feed2imap-go/pkg/config" +) + +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, *sync.WaitGroup)) { + var wg sync.WaitGroup + wg.Add(len(state.feeds)) + + for _, feed := range state.feeds { + go goFunc(feed, &wg) + } + wg.Wait() +} + +func (state *State) LoadCache(fileName string) error { + 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 storeCache(state.cache, fileName) +} + +func (state *State) Fetch() int { + state.ForeachGo(handleFeed) + + ctr := 0 + for _, feed := range state.feeds { + success := feed.Success() + feed.cached.Checked(!success) + + if success { + ctr++ + } + } + + return ctr +} + +func NewState(cfg *config.Config) *State { + state := State{ + feeds: map[string]*Feed{}, + cache: nil, // loaded later on + cfg: cfg, + } + + for name, parsedFeed := range cfg.Feeds { + state.feeds[name] = &Feed{Feed: parsedFeed} + } + + return &state +} diff --git a/internal/feed/template/template.go b/internal/feed/template/template.go index dd31f51..7871e06 100644 --- a/internal/feed/template/template.go +++ b/internal/feed/template/template.go @@ -6,7 +6,7 @@ import ( "strconv" "strings" - "github.com/Necoro/feed2imap-go/internal/log" + "github.com/Necoro/feed2imap-go/pkg/log" ) func dict(v ...string) map[string]string { |