Comment SN posts with HN info
This commit is contained in:
parent
56a33da94a
commit
8cc4b42094
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.20
|
|||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/joho/godotenv v1.5.1 // indirect
|
||||
github.com/namsral/flag v1.7.4-pre // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
|
|
2
go.sum
2
go.sum
|
@ -1,6 +1,8 @@
|
|||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs=
|
||||
|
|
8
hn.go
8
hn.go
|
@ -78,3 +78,11 @@ func FetchStoryById(id ItemID) Story {
|
|||
|
||||
return story
|
||||
}
|
||||
|
||||
func HackerNewsUserLink(user string) string {
|
||||
return fmt.Sprintf("%s/user?id=%s", HackerNewsUrl, user)
|
||||
}
|
||||
|
||||
func HackerNewsItemLink(id int) string {
|
||||
return fmt.Sprintf("%s/item?id=%d", HackerNewsUrl, id)
|
||||
}
|
||||
|
|
46
sn.go
46
sn.go
|
@ -6,7 +6,9 @@ import (
|
|||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/dustin/go-humanize"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/namsral/flag"
|
||||
)
|
||||
|
@ -28,6 +30,16 @@ type DupesResponse struct {
|
|||
} `json:"data"`
|
||||
}
|
||||
|
||||
type Item struct {
|
||||
Id int `json:"id,string"`
|
||||
}
|
||||
|
||||
type UpsertLinkResponse struct {
|
||||
Data struct {
|
||||
UpsertLink Item `json:"upsertLink"`
|
||||
} `json:"data"`
|
||||
}
|
||||
|
||||
var (
|
||||
SnApiUrl string
|
||||
SnApiToken string
|
||||
|
@ -123,4 +135,38 @@ func PostStoryToStackerNews(story *Story) {
|
|||
}
|
||||
resp := MakeStackerNewsRequest(body)
|
||||
defer resp.Body.Close()
|
||||
|
||||
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
|
||||
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)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue