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-16 19:05:10 +00:00
|
|
|
"github.com/dustin/go-humanize"
|
2024-04-07 03:37:31 +00:00
|
|
|
sn "github.com/ekzyis/snappy"
|
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 (
|
2024-03-31 01:48:51 +00:00
|
|
|
SELECT id, MIN(created_at) AS start, MAX(created_at) AS end
|
|
|
|
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
|
2024-03-31 14:47:51 +00:00
|
|
|
HAVING unixepoch(end) - unixepoch(start) >= 3600
|
|
|
|
ORDER BY time ASC
|
2024-03-18 06:07:09 +00:00
|
|
|
LIMIT 1
|
2024-03-31 01:48:51 +00:00
|
|
|
) t JOIN hn_items ON t.id = hn_items.id AND t.end = hn_items.created_at;
|
2024-03-13 12:13:11 +00:00
|
|
|
`); 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) {
|
2024-04-07 03:37:31 +00:00
|
|
|
c := sn.NewClient()
|
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 {
|
2024-04-07 03:37:31 +00:00
|
|
|
dupes, err := c.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]
|
|
|
|
}
|
|
|
|
|
2024-04-07 03:37:31 +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,
|
|
|
|
)
|
|
|
|
|
|
|
|
parentId, err := c.PostLink(url, title, comment, "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-24 22:42:11 +00:00
|
|
|
return parentId, nil
|
2023-04-16 19:05:10 +00:00
|
|
|
}
|