hnbot/main.go

68 lines
1.3 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
2024-04-07 03:37:31 +00:00
sn "github.com/ekzyis/snappy"
2023-04-25 09:51:12 +00:00
)
2023-04-19 22:20:46 +00:00
2024-03-18 07:19:46 +00:00
func SyncStories() {
2023-04-19 22:20:46 +00:00
for {
2024-03-18 07:24:54 +00:00
now := time.Now()
dur := now.Truncate(time.Minute).Add(time.Minute).Sub(now)
log.Println("[hn] sleeping for", dur.Round(time.Second))
time.Sleep(dur)
2023-04-25 09:51:12 +00:00
stories, err := FetchHackerNewsTopStories()
if err != nil {
2024-04-07 03:42:05 +00:00
log.Println(err)
2023-04-25 09:51:12 +00:00
continue
}
if err := SaveStories(&stories); err != nil {
2024-04-07 03:42:05 +00:00
log.Println(err)
continue
}
2024-03-18 07:19:46 +00:00
}
}
func main() {
go SyncStories()
for {
var (
filtered *[]Story
err error
)
2024-03-18 07:24:54 +00:00
now := time.Now()
dur := now.Truncate(time.Minute).Add(15 * time.Minute).Sub(now)
log.Println("[sn] sleeping for", dur.Round(time.Second))
time.Sleep(dur)
if filtered, err = CurateContentForStackerNews(); err != nil {
2024-04-07 03:42:05 +00:00
log.Println(err)
continue
}
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)
2024-03-13 13:13:19 +00:00
// save dupe in db to prevent retries
parentId := dupesErr.Dupes[0].Id
if err := SaveSnItem(parentId, story.ID); err != nil {
2024-04-07 03:42:05 +00:00
log.Println(err)
2024-03-13 13:13:19 +00:00
}
2023-04-25 09:51:12 +00:00
continue
}
2024-04-07 03:42:05 +00:00
log.Println(err)
2023-04-25 09:51:12 +00:00
continue
}
2023-04-19 22:20:46 +00:00
}
}
}