hnbot/sn.go

93 lines
2.1 KiB
Go
Raw Normal View History

2023-04-16 15:14:44 +00:00
package main
import (
2023-04-16 15:28:38 +00:00
"fmt"
2023-04-16 15:14:44 +00:00
"log"
2023-04-16 19:05:10 +00:00
"time"
2023-04-16 17:43:48 +00:00
2023-04-24 23:57:37 +00:00
"github.com/bwmarrin/discordgo"
2023-04-16 19:05:10 +00:00
"github.com/dustin/go-humanize"
2023-06-01 01:07:02 +00:00
"github.com/ekzyis/sn-goapi"
2023-04-16 15:14:44 +00:00
)
2023-04-16 17:56:42 +00:00
func CurateContentForStackerNews(stories *[]Story) *[]Story {
2023-04-16 15:14:44 +00:00
// TODO: filter by relevance
slice := (*stories)[0:1]
return &slice
}
2023-04-25 00:22:27 +00:00
type PostStoryOptions struct {
SkipDupes bool
}
func PostStoryToStackerNews(story *Story, options PostStoryOptions) (int, error) {
2023-08-15 20:23:16 +00:00
url := story.Url
if url == "" {
url = HackerNewsItemLink(story.ID)
}
log.Printf("Posting to SN (url=%s) ...\n", url)
2023-04-25 09:51:12 +00:00
2023-04-25 00:22:27 +00:00
if !options.SkipDupes {
2023-08-15 20:23:16 +00:00
dupes, err := sn.Dupes(url)
2023-04-25 09:51:12 +00:00
if err != nil {
return -1, err
}
2023-04-25 00:22:27 +00:00
if len(*dupes) > 0 {
2023-08-15 20:23:16 +00:00
return -1, &sn.DupesError{Url: url, Dupes: *dupes}
2023-04-25 00:22:27 +00:00
}
2023-04-16 15:53:49 +00:00
}
2023-04-16 15:14:44 +00:00
2023-08-30 14:16:53 +00:00
parentId, err := sn.PostLink(url, story.Title[0:80], "tech")
2023-05-11 21:37:48 +00:00
if err != nil {
2023-06-01 01:07:02 +00:00
return -1, fmt.Errorf("error posting link: %w", err)
2023-05-11 21:37:48 +00:00
}
2023-04-16 19:50:57 +00:00
2023-08-15 20:23:16 +00:00
log.Printf("Posting to SN (url=%s) ... OK \n", url)
2023-04-19 20:48:24 +00:00
SendStackerNewsEmbedToDiscord(story.Title, parentId)
2023-04-16 19:50:57 +00:00
2023-04-16 19:05:10 +00:00
comment := fmt.Sprintf(
"This link was posted by [%s](%s) %s on [HN](%s). It received %d points and %d comments.",
story.By,
HackerNewsUserLink(story.By),
humanize.Time(time.Unix(int64(story.Time), 0)),
HackerNewsItemLink(story.ID),
story.Score, story.Descendants,
)
2023-06-01 01:07:02 +00:00
sn.CreateComment(parentId, comment)
2023-04-24 22:42:11 +00:00
return parentId, nil
2023-04-16 19:05:10 +00:00
}
2023-04-19 20:48:24 +00:00
func SendStackerNewsEmbedToDiscord(title string, id int) {
Timestamp := time.Now().Format(time.RFC3339)
2023-06-01 01:07:02 +00:00
url := sn.FormatLink(id)
2023-04-19 20:48:24 +00:00
color := 0xffc107
2023-04-24 23:57:37 +00:00
embed := discordgo.MessageEmbed{
2023-04-19 20:48:24 +00:00
Title: title,
2023-04-24 23:57:37 +00:00
URL: url,
2023-04-19 20:48:24 +00:00
Color: color,
2023-04-24 23:57:37 +00:00
Footer: &discordgo.MessageEmbedFooter{
2023-04-19 20:48:24 +00:00
Text: "Stacker News",
2023-04-24 23:57:37 +00:00
IconURL: "https://stacker.news/favicon.png",
2023-04-19 20:48:24 +00:00
},
Timestamp: Timestamp,
}
2023-04-24 23:57:37 +00:00
SendEmbedToDiscord(&embed)
2023-04-19 20:48:24 +00:00
}
2023-05-31 23:58:07 +00:00
func SendNotificationsEmbedToDiscord() {
Timestamp := time.Now().Format(time.RFC3339)
color := 0xffc107
embed := discordgo.MessageEmbed{
Title: "new notifications",
URL: "https://stacker.news/hn/posts",
Color: color,
Footer: &discordgo.MessageEmbedFooter{
Text: "Stacker News",
IconURL: "https://stacker.news/favicon-notify.png",
},
Timestamp: Timestamp,
}
SendEmbedToDiscord(&embed)
}