Sync HN items every minute

This commit is contained in:
ekzyis 2024-03-18 08:19:46 +01:00
parent a2633b1fdb
commit 0285c5a53a
1 changed files with 24 additions and 7 deletions

31
main.go
View File

@ -15,6 +15,13 @@ func WaitUntilNextHour() {
time.Sleep(dur) 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() { func WaitUntilNextRun() {
now := time.Now() now := time.Now()
dur := now.Truncate(time.Minute).Add(15 * time.Minute).Sub(now) dur := now.Truncate(time.Minute).Add(15 * time.Minute).Sub(now)
@ -51,25 +58,35 @@ func SessionKeepAlive() {
} }
} }
func main() { func SyncStories() {
go CheckNotifications()
go SessionKeepAlive()
for { for {
stories, err := FetchHackerNewsTopStories() stories, err := FetchHackerNewsTopStories()
if err != nil { if err != nil {
SendErrorToDiscord(err) SendErrorToDiscord(err)
WaitUntilNextRun() WaitUntilNextMinute()
continue continue
} }
if err := SaveStories(&stories); err != nil { if err := SaveStories(&stories); err != nil {
SendErrorToDiscord(err) SendErrorToDiscord(err)
WaitUntilNextRun() WaitUntilNextMinute()
continue 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 { if filtered, err = CurateContentForStackerNews(); err != nil {
SendErrorToDiscord(err) SendErrorToDiscord(err)
WaitUntilNextRun() WaitUntilNextRun()