From 0285c5a53a1d8a6215749b38825d9760bde16e48 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Mon, 18 Mar 2024 08:19:46 +0100 Subject: [PATCH] Sync HN items every minute --- main.go | 31 ++++++++++++++++++++++++------- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index de005f9..c015a9f 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,13 @@ func WaitUntilNextHour() { time.Sleep(dur) } +func WaitUntilNextMinute() { + now := time.Now() + dur := now.Truncate(time.Minute).Add(time.Minute).Sub(now) + log.Println("sleeping for", dur.Round(time.Second)) + time.Sleep(dur) +} + func WaitUntilNextRun() { now := time.Now() dur := now.Truncate(time.Minute).Add(15 * time.Minute).Sub(now) @@ -51,25 +58,35 @@ func SessionKeepAlive() { } } -func main() { - go CheckNotifications() - go SessionKeepAlive() +func SyncStories() { for { - stories, err := FetchHackerNewsTopStories() if err != nil { SendErrorToDiscord(err) - WaitUntilNextRun() + WaitUntilNextMinute() continue } if err := SaveStories(&stories); err != nil { SendErrorToDiscord(err) - WaitUntilNextRun() + WaitUntilNextMinute() continue } - var filtered *[]Story + WaitUntilNextMinute() + } +} + +func main() { + go CheckNotifications() + go SessionKeepAlive() + go SyncStories() + for { + var ( + filtered *[]Story + err error + ) + if filtered, err = CurateContentForStackerNews(); err != nil { SendErrorToDiscord(err) WaitUntilNextRun()