2023-04-16 15:14:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2023-05-11 21:37:48 +00:00
|
|
|
"errors"
|
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-05-11 21:37:48 +00:00
|
|
|
type GraphQLError struct {
|
|
|
|
Message string `json:"message"`
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-11 21:37:48 +00:00
|
|
|
Errors []GraphQLError `json:"errors"`
|
|
|
|
Data struct {
|
2023-04-16 15:53:49 +00:00
|
|
|
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-05-11 21:37:48 +00:00
|
|
|
|
|
|
|
type CreateCommentsResponse struct {
|
|
|
|
Errors []GraphQLError `json:"errors"`
|
|
|
|
Data struct {
|
|
|
|
CreateComment Comment `json:"createComment"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
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 {
|
2023-05-11 21:37:48 +00:00
|
|
|
Errors []GraphQLError `json:"errors"`
|
|
|
|
Data struct {
|
2023-04-16 19:05:10 +00:00
|
|
|
UpsertLink Item `json:"upsertLink"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2023-04-16 22:41:16 +00:00
|
|
|
type ItemsResponse struct {
|
2023-05-11 21:37:48 +00:00
|
|
|
Errors []GraphQLError `json:"errors"`
|
|
|
|
Data struct {
|
2023-04-16 22:41:16 +00:00
|
|
|
Items struct {
|
|
|
|
Items []Item `json:"items"`
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
} `json:"items"`
|
|
|
|
} `json:"data"`
|
|
|
|
}
|
|
|
|
|
2023-05-31 23:58:07 +00:00
|
|
|
type HasNewNotesResponse struct {
|
|
|
|
Errors []GraphQLError `json:"errors"`
|
|
|
|
Data struct {
|
|
|
|
HasNewNotes bool `json:"hasNewNotes"`
|
|
|
|
} `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-05-11 21:37:48 +00:00
|
|
|
func CheckForErrors(graphqlErrors []GraphQLError) error {
|
|
|
|
if len(graphqlErrors) > 0 {
|
|
|
|
errorMsg, marshalErr := json.Marshal(graphqlErrors)
|
|
|
|
if marshalErr != nil {
|
|
|
|
return marshalErr
|
|
|
|
}
|
|
|
|
return errors.New(fmt.Sprintf("error fetching SN dupes: %s", string(errorMsg)))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-05-11 21:37:48 +00:00
|
|
|
err = CheckForErrors(dupesResp.Errors)
|
|
|
|
if err != nil {
|
|
|
|
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-05-11 20:57:52 +00:00
|
|
|
mutation upsertLink($url: String!, $title: String!, $sub: String!) {
|
|
|
|
upsertLink(url: $url, title: $title, sub: $sub) {
|
2023-04-16 18:00:13 +00:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}`,
|
2023-04-16 15:14:44 +00:00
|
|
|
Variables: map[string]interface{}{
|
|
|
|
"url": story.Url,
|
|
|
|
"title": story.Title,
|
2023-05-11 20:57:52 +00:00
|
|
|
"sub": "bitcoin",
|
2023-04-16 15:14:44 +00:00
|
|
|
},
|
|
|
|
}
|
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
|
|
|
}
|
2023-05-11 21:37:48 +00:00
|
|
|
err = CheckForErrors(upsertLinkResp.Errors)
|
|
|
|
if err != nil {
|
|
|
|
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-05-11 21:37:48 +00:00
|
|
|
var createCommentsResp CreateCommentsResponse
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&createCommentsResp)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error decoding SN upsertLink: %w", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = CheckForErrors(createCommentsResp.Errors)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-05-31 23:58:07 +00:00
|
|
|
|
|
|
|
func SendNotificationsEmbedToDiscord() {
|
|
|
|
Timestamp := time.Now().Format(time.RFC3339)
|
|
|
|
color := 0xffc107
|
|
|
|
embed := discordgo.MessageEmbed{
|
|
|
|
Title: "new notifications",
|
|
|
|
URL: "https://stacker.news/hn/posts",
|
|
|
|
Color: color,
|
|
|
|
Footer: &discordgo.MessageEmbedFooter{
|
|
|
|
Text: "Stacker News",
|
|
|
|
IconURL: "https://stacker.news/favicon-notify.png",
|
|
|
|
},
|
|
|
|
Timestamp: Timestamp,
|
|
|
|
}
|
|
|
|
SendEmbedToDiscord(&embed)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FetchHasNewNotes() (bool, error) {
|
|
|
|
log.Println("Checking notifications ...")
|
|
|
|
|
|
|
|
body := GraphQLPayload{
|
|
|
|
Query: `
|
|
|
|
{
|
|
|
|
hasNewNotes
|
|
|
|
}`,
|
|
|
|
}
|
|
|
|
resp, err := MakeStackerNewsRequest(body)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var hasNewNotesResp HasNewNotesResponse
|
|
|
|
err = json.NewDecoder(resp.Body).Decode(&hasNewNotesResp)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("error decoding SN hasNewNotes: %w", err)
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
err = CheckForErrors(hasNewNotesResp.Errors)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hasNewNotes := hasNewNotesResp.Data.HasNewNotes
|
|
|
|
|
|
|
|
msg := "Checking notifications ... OK - "
|
|
|
|
if hasNewNotes {
|
|
|
|
msg += "NEW"
|
|
|
|
} else {
|
|
|
|
msg += "NONE"
|
|
|
|
}
|
|
|
|
log.Println(msg)
|
|
|
|
|
|
|
|
return hasNewNotes, nil
|
|
|
|
}
|