hnbot/main.go

75 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-12 21:58:02 +00:00
log.Println("Checking notifications ...")
2023-06-01 01:41:20 +00:00
hasNewNotes, err := sn.CheckNotifications()
2023-05-31 23:58:07 +00:00
if err != nil {
SendErrorToDiscord(err)
} else {
if !prevHasNewNotes && hasNewNotes {
// only send embed on "rising edge"
SendNotificationsEmbedToDiscord()
2023-06-12 21:58:02 +00:00
log.Println("Forwarded notifications to monitoring")
2023-05-31 23:58:07 +00:00
} else if hasNewNotes {
2023-06-12 21:58:02 +00:00
log.Println("Notifications already forwarded")
2023-05-31 23:58:07 +00:00
}
}
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) {
2023-06-09 04:02:55 +00:00
// SendDupesErrorToDiscord(story.ID, dupesErr)
2023-06-12 21:58:02 +00:00
log.Println(dupesErr)
2023-04-25 09:51:12 +00:00
continue
}
SendErrorToDiscord(err)
continue
}
2023-04-19 22:20:46 +00:00
}
2023-04-26 22:41:57 +00:00
WaitUntilNextHour()
}
}