hnbot/main.go

44 lines
816 B
Go
Raw Permalink Normal View History

package main
2023-04-25 09:51:12 +00:00
import (
"errors"
"log"
"time"
)
2023-04-19 22:20:46 +00:00
2023-04-26 22:41:57 +00:00
func WaitUntilNextHour() {
now := time.Now()
dur := now.Truncate(time.Hour).Add(time.Hour).Sub(now)
log.Println("sleeping for", dur.Round(time.Second))
time.Sleep(dur)
}
func main() {
2023-04-19 22:20:46 +00:00
for {
2023-04-25 09:51:12 +00:00
stories, err := FetchHackerNewsTopStories()
if err != nil {
SendErrorToDiscord(err)
2023-04-26 22:41:57 +00:00
WaitUntilNextHour()
2023-04-25 09:51:12 +00:00
continue
}
2023-04-19 22:20:46 +00:00
filtered := CurateContentForStackerNews(&stories)
2023-04-25 09:51:12 +00:00
2023-04-19 22:20:46 +00:00
for _, story := range *filtered {
2023-04-25 09:51:12 +00:00
_, err := PostStoryToStackerNews(&story, PostStoryOptions{SkipDupes: false})
if err != nil {
var dupesErr *DupesError
if errors.As(err, &dupesErr) {
SendDupesErrorToDiscord(story.ID, dupesErr)
continue
}
SendErrorToDiscord(err)
continue
}
log.Println("Posting to SN ... OK")
2023-04-19 22:20:46 +00:00
}
2023-04-26 22:41:57 +00:00
WaitUntilNextHour()
}
}