2025-02-03 22:47:39 +01:00
|
|
|
package sn
|
2023-04-16 17:14:44 +02:00
|
|
|
|
|
|
|
import (
|
2024-03-13 13:13:11 +01:00
|
|
|
"database/sql"
|
2023-04-16 17:28:38 +02:00
|
|
|
"fmt"
|
2023-04-16 17:14:44 +02:00
|
|
|
"log"
|
2023-04-16 21:05:10 +02:00
|
|
|
"time"
|
2023-04-16 19:43:48 +02:00
|
|
|
|
2023-04-16 21:05:10 +02:00
|
|
|
"github.com/dustin/go-humanize"
|
2024-04-07 05:37:31 +02:00
|
|
|
sn "github.com/ekzyis/snappy"
|
2025-02-03 22:47:39 +01:00
|
|
|
"gitlab.com/ekzyis/hnbot/db"
|
|
|
|
"gitlab.com/ekzyis/hnbot/hn"
|
2023-04-16 17:14:44 +02:00
|
|
|
)
|
|
|
|
|
2025-02-03 22:47:39 +01:00
|
|
|
type DupesError = sn.DupesError
|
|
|
|
|
|
|
|
func CurateContent() (*[]hn.Item, error) {
|
2024-03-13 13:13:11 +01:00
|
|
|
var (
|
|
|
|
rows *sql.Rows
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if rows, err = db.Query(`
|
|
|
|
SELECT t.id, time, title, url, author, score, ndescendants
|
|
|
|
FROM (
|
2024-03-31 03:48:51 +02:00
|
|
|
SELECT id, MIN(created_at) AS start, MAX(created_at) AS end
|
|
|
|
FROM hn_items
|
2024-03-17 21:26:59 +01:00
|
|
|
WHERE rank = 1 AND id NOT IN (SELECT hn_id FROM sn_items) AND length(title) >= 5
|
2024-03-13 13:13:11 +01:00
|
|
|
GROUP BY id
|
2024-03-31 16:47:51 +02:00
|
|
|
HAVING unixepoch(end) - unixepoch(start) >= 3600
|
|
|
|
ORDER BY time ASC
|
2024-03-18 07:07:09 +01:00
|
|
|
LIMIT 1
|
2024-03-31 03:48:51 +02:00
|
|
|
) t JOIN hn_items ON t.id = hn_items.id AND t.end = hn_items.created_at;
|
2024-03-13 13:13:11 +01:00
|
|
|
`); err != nil {
|
|
|
|
err = fmt.Errorf("error querying hn_items: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-04-16 17:14:44 +02:00
|
|
|
|
2025-02-03 22:47:39 +01:00
|
|
|
var items []hn.Item
|
2024-03-13 13:13:11 +01:00
|
|
|
for rows.Next() {
|
2025-02-03 22:47:39 +01:00
|
|
|
var item hn.Item
|
|
|
|
if err = rows.Scan(&item.ID, &item.Time, &item.Title, &item.Url, &item.By, &item.Score, &item.Descendants); err != nil {
|
2024-03-13 13:13:11 +01:00
|
|
|
err = fmt.Errorf("error scanning hn_items: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-03 22:47:39 +01:00
|
|
|
items = append(items, item)
|
2024-03-13 13:13:11 +01:00
|
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
|
|
err = fmt.Errorf("error iterating hn_items: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-02-03 22:47:39 +01:00
|
|
|
return &items, nil
|
2023-04-16 17:14:44 +02:00
|
|
|
}
|
|
|
|
|
2025-02-03 22:47:39 +01:00
|
|
|
type PostOptions struct {
|
2023-04-25 02:22:27 +02:00
|
|
|
SkipDupes bool
|
|
|
|
}
|
|
|
|
|
2025-02-03 22:47:39 +01:00
|
|
|
func Post(item *hn.Item, options PostOptions) (int, error) {
|
2024-04-07 05:37:31 +02:00
|
|
|
c := sn.NewClient()
|
2025-02-03 22:47:39 +01:00
|
|
|
url := item.Url
|
2023-08-15 22:23:16 +02:00
|
|
|
if url == "" {
|
2025-02-03 22:47:39 +01:00
|
|
|
url = hn.ItemLink(item.ID)
|
2023-08-15 22:23:16 +02:00
|
|
|
}
|
2025-02-03 22:47:39 +01:00
|
|
|
log.Printf("post to SN: %s ...\n", url)
|
2023-04-25 11:51:12 +02:00
|
|
|
|
2023-04-25 02:22:27 +02:00
|
|
|
if !options.SkipDupes {
|
2024-04-07 05:37:31 +02:00
|
|
|
dupes, err := c.Dupes(url)
|
2023-04-25 11:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
2023-04-25 02:22:27 +02:00
|
|
|
if len(*dupes) > 0 {
|
2023-08-15 22:23:16 +02:00
|
|
|
return -1, &sn.DupesError{Url: url, Dupes: *dupes}
|
2023-04-25 02:22:27 +02:00
|
|
|
}
|
2023-04-16 17:53:49 +02:00
|
|
|
}
|
2023-04-16 17:14:44 +02:00
|
|
|
|
2025-02-03 22:47:39 +01:00
|
|
|
title := item.Title
|
2023-08-31 09:56:51 +02:00
|
|
|
if len(title) > 80 {
|
|
|
|
title = title[0:80]
|
|
|
|
}
|
|
|
|
|
2024-04-07 05:37:31 +02:00
|
|
|
comment := fmt.Sprintf(
|
|
|
|
"This link was posted by [%s](%s) %s on [HN](%s). It received %d points and %d comments.",
|
2025-02-03 22:47:39 +01:00
|
|
|
item.By,
|
|
|
|
hn.UserLink(item.By),
|
|
|
|
humanize.Time(time.Unix(int64(item.Time), 0)),
|
|
|
|
hn.ItemLink(item.ID),
|
|
|
|
item.Score, item.Descendants,
|
2024-04-07 05:37:31 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
parentId, err := c.PostLink(url, title, comment, "tech")
|
2023-05-11 23:37:48 +02:00
|
|
|
if err != nil {
|
2023-06-01 03:07:02 +02:00
|
|
|
return -1, fmt.Errorf("error posting link: %w", err)
|
2023-05-11 23:37:48 +02:00
|
|
|
}
|
2023-04-16 21:50:57 +02:00
|
|
|
|
2025-02-03 22:47:39 +01:00
|
|
|
log.Printf("post to SN: %s ... OK \n", url)
|
|
|
|
if err := db.SaveSnItem(parentId, item.ID); err != nil {
|
2024-03-13 13:13:11 +01:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
2023-04-25 00:42:11 +02:00
|
|
|
return parentId, nil
|
2023-04-16 21:05:10 +02:00
|
|
|
}
|