2023-04-07 06:56:18 +00:00
|
|
|
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
|
|
|
|
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 {
|
|
|
|
SendErrorToDiscord(err)
|
|
|
|
continue
|
|
|
|
}
|
2024-03-13 12:13:11 +00:00
|
|
|
if err := SaveStories(&stories); err != nil {
|
|
|
|
SendErrorToDiscord(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)
|
|
|
|
|
2024-03-13 12:13:11 +00:00
|
|
|
if filtered, err = CurateContentForStackerNews(); err != nil {
|
|
|
|
SendErrorToDiscord(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 {
|
|
|
|
SendErrorToDiscord(err)
|
|
|
|
}
|
2023-04-25 09:51:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
SendErrorToDiscord(err)
|
|
|
|
continue
|
|
|
|
}
|
2023-04-19 22:20:46 +00:00
|
|
|
}
|
2023-04-07 06:56:18 +00:00
|
|
|
}
|
|
|
|
}
|