diff --git a/TODO.md b/TODO.md index 30aa37c..2b9349b 100644 --- a/TODO.md +++ b/TODO.md @@ -13,6 +13,8 @@ * Folder creation * Change flags to os.Args[1] for config path * Cleanup +* Make Rhash optional +* Wrap content inside item ## Issues * ✓ Last record is not written fully to csv: *Fixed*. Had to Flush writer @@ -44,6 +46,8 @@ * Always running * Handle telegram reaction? * Filter from app based on keyword +* Move telegram auth token to credential file +* Check if context is used correctly ### Log * Log to file diff --git a/app/app.go b/app/app.go index c81fd8c..c29cd95 100644 --- a/app/app.go +++ b/app/app.go @@ -41,7 +41,7 @@ func Run(configPath string) { } } -func ProcessFeed(feed FeedCfg, scheduler *Scheduler, dbDir string, tgram telegram.TelegramSender) { +func ProcessFeed(feed FeedCfg, scheduler Scheduler, dbDir string, tgram telegram.TelegramSender) { sd, err := scheduler.ShouldDownload(feed.Name, feed.Cron) if err != nil { log.Error("shouldDownload failed", "feed", feed.Name, "err", err) diff --git a/app/schedule.go b/app/schedule.go index 308d833..74a6530 100644 --- a/app/schedule.go +++ b/app/schedule.go @@ -11,13 +11,19 @@ import ( "gopkg.in/yaml.v3" ) -type Scheduler struct { +type Scheduler interface { + ShouldDownload(name string, scheduleSpec string) (bool, error) + Save() error + Good(name string) +} + +type scheduler struct { filePath string lastSuccessTime map[string]time.Time } -func NewScheduler(filePath string) (*Scheduler, error) { - s := Scheduler{filePath: filePath} +func NewScheduler(filePath string) (Scheduler, error) { + s := scheduler{filePath: filePath} data, err := os.ReadFile(filePath) if err != nil { if !errors.Is(err, os.ErrNotExist) { @@ -38,7 +44,7 @@ func NewScheduler(filePath string) (*Scheduler, error) { return &s, nil } -func (s *Scheduler) Save() error { +func (s *scheduler) Save() error { data, err := yaml.Marshal(&s.lastSuccessTime) if err != nil { return err @@ -46,11 +52,11 @@ func (s *Scheduler) Save() error { return os.WriteFile(s.filePath, data, 0644) } -func (s *Scheduler) Good(name string) { +func (s *scheduler) Good(name string) { s.lastSuccessTime[name] = time.Now() } -func (s *Scheduler) ShouldDownload(name string, scheduleSpec string) (bool, error) { +func (s *scheduler) ShouldDownload(name string, scheduleSpec string) (bool, error) { lastSuccess, ok := s.lastSuccessTime[name] if !ok { return true, nil