hnbot/sn.go

274 lines
6.1 KiB
Go
Raw Normal View History

2023-04-16 15:14:44 +00:00
package main
import (
"bytes"
"encoding/json"
2023-04-16 15:28:38 +00:00
"fmt"
2023-04-16 15:14:44 +00:00
"log"
"net/http"
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-04-16 17:43:48 +00:00
"github.com/joho/godotenv"
"github.com/namsral/flag"
2023-04-16 15:14:44 +00:00
)
type GraphQLPayload struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables,omitempty"`
}
2023-04-25 00:26:44 +00:00
type User struct {
2023-04-24 22:42:11 +00:00
Name string `json:"name"`
}
2023-04-25 00:26:44 +00:00
2023-04-16 15:53:49 +00:00
type Dupe struct {
2023-04-24 22:42:11 +00:00
Id int `json:"id,string"`
Url string `json:"url"`
Title string `json:"title"`
2023-04-25 00:26:44 +00:00
User User `json:"user"`
2023-04-24 22:42:11 +00:00
CreatedAt time.Time `json:"createdAt"`
Sats int `json:"sats"`
NComments int `json:"ncomments"`
2023-04-16 15:53:49 +00:00
}
type DupesResponse struct {
Data struct {
Dupes []Dupe `json:"dupes"`
} `json:"data"`
}
2023-04-24 22:42:11 +00:00
type DupesError struct {
Url string
Dupes []Dupe
}
func (e *DupesError) Error() string {
2023-04-25 09:51:12 +00:00
return fmt.Sprintf("found %d dupes for %s", len(e.Dupes), e.Url)
2023-04-24 22:42:11 +00:00
}
2023-04-16 22:41:16 +00:00
type Comment struct {
2023-04-16 23:58:38 +00:00
Id int `json:"id,string"`
Text string `json:"text"`
User User `json:"user"`
Comments []Comment `json:"comments"`
2023-04-16 22:41:16 +00:00
}
2023-04-16 19:05:10 +00:00
type Item struct {
2023-04-16 22:41:16 +00:00
Id int `json:"id,string"`
Title string `json:"title"`
Url string `json:"url"`
Sats int `json:"sats"`
2023-04-16 23:58:38 +00:00
CreatedAt time.Time `json:"createdAt"`
2023-04-16 22:41:16 +00:00
Comments []Comment `json:"comments"`
2023-04-16 23:58:38 +00:00
NComments int `json:"ncomments"`
2023-04-16 19:05:10 +00:00
}
type UpsertLinkResponse struct {
Data struct {
UpsertLink Item `json:"upsertLink"`
} `json:"data"`
}
2023-04-16 22:41:16 +00:00
type ItemsResponse struct {
Data struct {
Items struct {
Items []Item `json:"items"`
Cursor string `json:"cursor"`
} `json:"items"`
} `json:"data"`
}
2023-04-16 17:43:48 +00:00
var (
2023-04-25 00:30:04 +00:00
StackerNewsUrl = "https://stacker.news"
SnApiUrl = "https://stacker.news/api/graphql"
2023-04-16 23:58:38 +00:00
SnAuthCookie string
2023-04-16 17:43:48 +00:00
)
func init() {
err := godotenv.Load()
if err != nil {
2023-04-25 09:51:12 +00:00
log.Fatal("error loading .env file")
2023-04-16 17:43:48 +00:00
}
2023-04-16 21:26:31 +00:00
flag.StringVar(&SnAuthCookie, "SN_AUTH_COOKIE", "", "Cookie required for authorizing requests to stacker.news/api/graphql")
2023-04-16 17:43:48 +00:00
flag.Parse()
2023-04-16 20:03:14 +00:00
if SnAuthCookie == "" {
2023-04-16 21:26:31 +00:00
log.Fatal("SN_AUTH_COOKIE not set")
2023-04-16 17:43:48 +00:00
}
}
2023-04-25 09:51:12 +00:00
func MakeStackerNewsRequest(body GraphQLPayload) (*http.Response, error) {
2023-04-16 17:09:33 +00:00
bodyJSON, err := json.Marshal(body)
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error encoding SN payload: %w", err)
return nil, err
2023-04-16 17:09:33 +00:00
}
2023-04-16 17:51:17 +00:00
req, err := http.NewRequest("POST", SnApiUrl, bytes.NewBuffer(bodyJSON))
2023-04-16 17:09:33 +00:00
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error preparing SN request: %w", err)
return nil, err
2023-04-16 17:09:33 +00:00
}
req.Header.Set("Content-Type", "application/json")
2023-04-16 20:03:14 +00:00
req.Header.Set("Cookie", SnAuthCookie)
2023-04-16 17:09:33 +00:00
client := http.DefaultClient
resp, err := client.Do(req)
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error posting SN payload: %w", err)
return nil, err
2023-04-16 17:09:33 +00:00
}
2023-04-25 09:51:12 +00:00
return resp, nil
2023-04-16 17:09:33 +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 09:51:12 +00:00
func FetchStackerNewsDupes(url string) (*[]Dupe, error) {
log.Printf("Fetching SN dupes (url=%s) ...\n", url)
2023-04-16 15:53:49 +00:00
body := GraphQLPayload{
Query: `
query Dupes($url: String!) {
dupes(url: $url) {
id
url
title
2023-04-24 22:42:11 +00:00
user {
name
}
createdAt
sats
ncomments
2023-04-16 15:53:49 +00:00
}
}`,
Variables: map[string]interface{}{
"url": url,
},
}
2023-04-25 09:51:12 +00:00
resp, err := MakeStackerNewsRequest(body)
if err != nil {
return nil, err
}
2023-04-16 15:53:49 +00:00
defer resp.Body.Close()
var dupesResp DupesResponse
2023-04-25 09:51:12 +00:00
err = json.NewDecoder(resp.Body).Decode(&dupesResp)
2023-04-16 15:53:49 +00:00
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error decoding SN dupes: %w", err)
return nil, err
2023-04-16 15:53:49 +00:00
}
2023-04-25 09:51:12 +00:00
log.Printf("Fetching SN dupes (url=%s) ... OK\n", url)
return &dupesResp.Data.Dupes, nil
2023-04-16 15:53:49 +00:00
}
2023-04-25 00:22:27 +00:00
type PostStoryOptions struct {
SkipDupes bool
}
func PostStoryToStackerNews(story *Story, options PostStoryOptions) (int, error) {
2023-04-25 09:51:12 +00:00
log.Printf("Posting to SN (url=%s) ...\n", story.Url)
2023-04-25 00:22:27 +00:00
if !options.SkipDupes {
2023-04-25 09:51:12 +00:00
dupes, err := FetchStackerNewsDupes(story.Url)
if err != nil {
return -1, err
}
2023-04-25 00:22:27 +00:00
if len(*dupes) > 0 {
return -1, &DupesError{story.Url, *dupes}
}
2023-04-16 15:53:49 +00:00
}
2023-04-16 15:14:44 +00:00
body := GraphQLPayload{
Query: `
2023-04-16 18:00:13 +00:00
mutation upsertLink($url: String!, $title: String!) {
upsertLink(url: $url, title: $title) {
id
}
}`,
2023-04-16 15:14:44 +00:00
Variables: map[string]interface{}{
"url": story.Url,
"title": story.Title,
},
}
2023-04-25 09:51:12 +00:00
resp, err := MakeStackerNewsRequest(body)
if err != nil {
return -1, err
}
2023-04-16 15:14:44 +00:00
defer resp.Body.Close()
2023-04-16 19:05:10 +00:00
var upsertLinkResp UpsertLinkResponse
2023-04-25 09:51:12 +00:00
err = json.NewDecoder(resp.Body).Decode(&upsertLinkResp)
2023-04-16 19:05:10 +00:00
if err != nil {
2023-04-25 09:51:12 +00:00
err = fmt.Errorf("error decoding SN upsertLink: %w", err)
return -1, err
2023-04-16 19:05:10 +00:00
}
parentId := upsertLinkResp.Data.UpsertLink.Id
2023-04-16 19:50:57 +00:00
2023-04-25 09:51:12 +00:00
log.Printf("Posting to SN (url=%s) ... OK \n", story.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,
)
CommentStackerNewsPost(comment, parentId)
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 StackerNewsItemLink(id int) string {
return fmt.Sprintf("https://stacker.news/items/%d", id)
}
2023-04-25 09:51:12 +00:00
func CommentStackerNewsPost(text string, parentId int) (*http.Response, error) {
log.Printf("Commenting SN post (parentId=%d) ...\n", parentId)
2023-04-16 19:05:10 +00:00
body := GraphQLPayload{
Query: `
mutation createComment($text: String!, $parentId: ID!) {
createComment(text: $text, parentId: $parentId) {
id
}
}`,
Variables: map[string]interface{}{
"text": text,
"parentId": parentId,
},
}
2023-04-25 09:51:12 +00:00
resp, err := MakeStackerNewsRequest(body)
if err != nil {
return nil, err
}
2023-04-16 19:05:10 +00:00
defer resp.Body.Close()
2023-04-17 16:55:08 +00:00
2023-04-25 09:51:12 +00:00
log.Printf("Commenting SN post (parentId=%d) ... OK\n", parentId)
return resp, nil
2023-04-16 15:14:44 +00:00
}
2023-04-19 20:48:24 +00:00
func SendStackerNewsEmbedToDiscord(title string, id int) {
Timestamp := time.Now().Format(time.RFC3339)
url := StackerNewsItemLink(id)
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
}