hnbot/sn.go

272 lines
5.6 KiB
Go
Raw Permalink 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-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-16 15:53:49 +00:00
type Dupe struct {
Id int `json:"id,string"`
Url string `json:"url"`
Title string `json:"title"`
}
type DupesResponse struct {
Data struct {
Dupes []Dupe `json:"dupes"`
} `json:"data"`
}
2023-04-16 22:41:16 +00:00
type User struct {
Name string `json:"name"`
}
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-16 23:58:38 +00:00
StackerNewsUrl string
SnApiUrl string
SnAuthCookie string
2023-04-16 17:43:48 +00:00
)
func init() {
2023-04-16 23:58:38 +00:00
StackerNewsUrl = "https://stacker.news"
2023-04-16 17:51:17 +00:00
SnApiUrl = "https://stacker.news/api/graphql"
2023-04-16 17:43:48 +00:00
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}
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-16 17:56:42 +00:00
func MakeStackerNewsRequest(body GraphQLPayload) *http.Response {
2023-04-16 17:09:33 +00:00
bodyJSON, err := json.Marshal(body)
if err != nil {
log.Fatal("Error during json.Marshal:", err)
}
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 {
panic(err)
}
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 {
panic(err)
}
2023-04-16 17:51:17 +00:00
log.Printf("POST %s %d\n", SnApiUrl, resp.StatusCode)
2023-04-16 17:09:33 +00:00
return resp
}
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-16 17:56:42 +00:00
func FetchStackerNewsDupes(url string) *[]Dupe {
2023-04-16 15:53:49 +00:00
body := GraphQLPayload{
Query: `
query Dupes($url: String!) {
dupes(url: $url) {
id
url
title
}
}`,
Variables: map[string]interface{}{
"url": url,
},
}
2023-04-16 17:56:42 +00:00
resp := MakeStackerNewsRequest(body)
2023-04-16 15:53:49 +00:00
defer resp.Body.Close()
var dupesResp DupesResponse
2023-04-16 17:09:33 +00:00
err := json.NewDecoder(resp.Body).Decode(&dupesResp)
2023-04-16 15:53:49 +00:00
if err != nil {
log.Fatal("Error decoding dupes JSON:", err)
}
return &dupesResp.Data.Dupes
}
2023-04-16 17:56:42 +00:00
func PostStoryToStackerNews(story *Story) {
dupes := FetchStackerNewsDupes(story.Url)
2023-04-16 15:53:49 +00:00
if len(*dupes) > 0 {
2023-04-16 19:50:57 +00:00
log.Printf("%s was already posted. Skipping.\n", story.Url)
2023-04-16 15:53:49 +00:00
return
}
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-16 17:56:42 +00:00
resp := MakeStackerNewsRequest(body)
2023-04-16 15:14:44 +00:00
defer resp.Body.Close()
2023-04-16 19:05:10 +00:00
var upsertLinkResp UpsertLinkResponse
err := json.NewDecoder(resp.Body).Decode(&upsertLinkResp)
if err != nil {
log.Fatal("Error decoding dupes JSON:", err)
}
parentId := upsertLinkResp.Data.UpsertLink.Id
2023-04-16 19:50:57 +00:00
log.Printf("Created new post on SN: id=%d url=%s\n", parentId, story.Url)
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-16 19:50:57 +00:00
log.Printf("Commented post on SN: parentId=%d text='%s'\n", parentId, comment)
2023-04-16 19:05:10 +00:00
}
func CommentStackerNewsPost(text string, parentId int) {
body := GraphQLPayload{
Query: `
mutation createComment($text: String!, $parentId: ID!) {
createComment(text: $text, parentId: $parentId) {
id
}
}`,
Variables: map[string]interface{}{
"text": text,
"parentId": parentId,
},
}
resp := MakeStackerNewsRequest(body)
defer resp.Body.Close()
2023-04-16 15:14:44 +00:00
}
2023-04-16 22:41:16 +00:00
func FetchStackerNewsUserItems(user string) *[]Item {
query := `
query items($name: String!, $cursor: String) {
items(name: $name, sort: "user", cursor: $cursor) {
items {
id
title
url
sats
2023-04-16 23:58:38 +00:00
createdAt
2023-04-16 22:41:16 +00:00
comments {
2023-04-16 23:58:38 +00:00
id
2023-04-16 22:41:16 +00:00
text
user {
name
}
2023-04-16 23:58:38 +00:00
comments {
id
text
user {
name
}
}
2023-04-16 22:41:16 +00:00
}
2023-04-16 23:58:38 +00:00
ncomments
2023-04-16 22:41:16 +00:00
}
cursor
}
}
`
var items []Item
var cursor string
for {
body := GraphQLPayload{
Query: query,
Variables: map[string]interface{}{
"name": user,
"cursor": cursor,
},
}
resp := MakeStackerNewsRequest(body)
defer resp.Body.Close()
var itemsResp ItemsResponse
err := json.NewDecoder(resp.Body).Decode(&itemsResp)
if err != nil {
log.Fatal("Error decoding items JSON:", err)
}
fetchedItems := itemsResp.Data.Items.Items
for _, item := range fetchedItems {
items = append(items, item)
}
if len(fetchedItems) < 21 {
break
}
cursor = itemsResp.Data.Items.Cursor
}
log.Printf("Fetched %d items\n", len(items))
return &items
}
2023-04-16 23:58:38 +00:00
func StackerNewsItemLink(id int) string {
return fmt.Sprintf("%s/items/%d", StackerNewsUrl, id)
}