hnbot/main.go

74 lines
1.5 KiB
Go
Raw Permalink Normal View History

package main
2023-04-25 09:51:12 +00:00
import (
"errors"
"log"
"time"
2023-06-01 01:07:02 +00:00
"github.com/ekzyis/sn-goapi"
2023-04-25 09:51:12 +00:00
)
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)
}
2023-05-31 23:58:07 +00:00
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 CheckNotifications() {
var prevHasNewNotes bool
for {
2023-06-01 01:07:02 +00:00
hasNewNotes, err := sn.HasNewNotes()
2023-05-31 23:58:07 +00:00
if err != nil {
SendErrorToDiscord(err)
} else {
if !prevHasNewNotes && hasNewNotes {
// only send embed on "rising edge"
SendNotificationsEmbedToDiscord()
log.Println("Forwarded to monitoring")
} else if hasNewNotes {
log.Println("Already forwarded")
}
}
prevHasNewNotes = hasNewNotes
WaitUntilNextMinute()
}
}
func main() {
2023-05-31 23:58:07 +00:00
go CheckNotifications()
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 {
2023-06-01 01:07:02 +00:00
var dupesErr *sn.DupesError
2023-04-25 09:51:12 +00:00
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()
}
}