hnbot/sn.go

179 lines
3.8 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-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 19:05:10 +00:00
type Item struct {
Id int `json:"id,string"`
}
type UpsertLinkResponse struct {
Data struct {
UpsertLink Item `json:"upsertLink"`
} `json:"data"`
}
2023-04-16 17:43:48 +00:00
var (
2023-04-16 17:51:17 +00:00
SnApiUrl string
2023-04-16 17:43:48 +00:00
SnApiToken string
)
func init() {
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")
}
flag.StringVar(&SnApiToken, "NEXT_AUTH_CSRF_TOKEN", "", "Token required for authorizing requests to stacker.news/api/graphql")
flag.Parse()
if SnApiToken == "" {
log.Fatal("NEXT_AUTH_CSRF_TOKEN not set")
}
}
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")
req.Header.Set("Cookie", fmt.Sprintf("__Host-next-auth.csrf-token=%s", SnApiToken))
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
}