2023-04-16 15:14:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-13 12:13:11 +00:00
|
|
|
"database/sql"
|
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
|
|
|
)
|
|
|
|
|
2024-03-13 12:13:11 +00:00
|
|
|
func CurateContentForStackerNews() (*[]Story, error) {
|
|
|
|
var (
|
|
|
|
rows *sql.Rows
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if rows, err = db.Query(`
|
|
|
|
SELECT t.id, time, title, url, author, score, ndescendants
|
|
|
|
FROM (
|
|
|
|
SELECT id, MAX(created_at) AS created_at FROM hn_items
|
2024-03-17 20:26:59 +00:00
|
|
|
WHERE rank = 1 AND id NOT IN (SELECT hn_id FROM sn_items) AND length(title) >= 5
|
2024-03-13 12:13:11 +00:00
|
|
|
GROUP BY id
|
|
|
|
) t JOIN hn_items ON t.id = hn_items.id AND t.created_at = hn_items.created_at;
|
|
|
|
`); err != nil {
|
|
|
|
err = fmt.Errorf("error querying hn_items: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-04-16 15:14:44 +00:00
|
|
|
|
2024-03-13 12:13:11 +00:00
|
|
|
var stories []Story
|
|
|
|
for rows.Next() {
|
|
|
|
var story Story
|
|
|
|
if err = rows.Scan(&story.ID, &story.Time, &story.Title, &story.Url, &story.By, &story.Score, &story.Descendants); err != nil {
|
|
|
|
err = fmt.Errorf("error scanning hn_items: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stories = append(stories, story)
|
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
err = fmt.Errorf("error iterating hn_items: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &stories, nil
|
2023-04-16 15:14:44 +00:00
|
|
|
}
|
|
|
|
|
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-31 07:56:51 +00:00
|
|
|
title := story.Title
|
|
|
|
if len(title) > 80 {
|
|
|
|
title = title[0:80]
|
|
|
|
}
|
|
|
|
|
|
|
|
parentId, err := sn.PostLink(url, title, "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)
|
2024-03-13 12:13:11 +00:00
|
|
|
if err := SaveSnItem(parentId, story.ID); err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|