hnbot/sn.go

127 lines
2.5 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 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 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 {
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()
}