Add doc and README

This commit is contained in:
ekzyis 2023-06-01 03:39:07 +02:00
parent b3208af7c8
commit 51d0d1d811
5 changed files with 34 additions and 4 deletions

19
README.md Normal file
View File

@ -0,0 +1,19 @@
# sn-goapi
Package for [stacker.news](https://stacker.news) API access
Install via `go get github.com/ekzyis/sn-goapi`
Supports:
- [ ] Post of type ...
- [ ] ... Discussion
- [x] ... Link
- [ ] ... Bounty
- [ ] ... Poll
- [x] Reply (comments)
- [ ] Tips
- [x] Checking dupes
- [x] Checking notifications
`SN_AUTH_COOKIE` must be set with a valid session cookie.

View File

@ -5,6 +5,7 @@ import (
"fmt"
)
// Fetch dupes
func Dupes(url string) (*[]Dupe, error) {
body := GraphQLPayload{
Query: `

13
main.go
View File

@ -1,3 +1,4 @@
// Package for stacker.news API access
package sn
import (
@ -13,11 +14,14 @@ import (
)
var (
SnUrl = "https://stacker.news"
// stacker.news URL
SnUrl = "https://stacker.news"
// stacker.news API URL
SnApiUrl = "https://stacker.news/api/graphql"
// stacker.news session cookie
SnAuthCookie string
// TODO add API key support
// SnApiKey string
SnAuthCookie string
)
func init() {
@ -25,13 +29,14 @@ func init() {
if err != nil {
log.Fatal("error loading .env file")
}
flag.StringVar(&SnAuthCookie, "SN_AUTH_COOKIE", "", "Cookie required for authorizing requests to stacker.news/api/graphql")
flag.StringVar(&SnAuthCookie, "SN_AUTH_COOKIE", "", "Cookie required for authentication requests to stacker.news/api/graphql")
flag.Parse()
if SnAuthCookie == "" {
log.Fatal("SN_AUTH_COOKIE not set")
}
}
// Make GraphQL request using raw payload
func MakeStackerNewsRequest(body GraphQLPayload) (*http.Response, error) {
bodyJSON, err := json.Marshal(body)
if err != nil {
@ -58,6 +63,7 @@ func MakeStackerNewsRequest(body GraphQLPayload) (*http.Response, error) {
return resp, nil
}
// Returns error if any error was found
func CheckForErrors(graphqlErrors []GraphQLError) error {
if len(graphqlErrors) > 0 {
errorMsg, marshalErr := json.Marshal(graphqlErrors)
@ -69,6 +75,7 @@ func CheckForErrors(graphqlErrors []GraphQLError) error {
return nil
}
// Format item id as link
func FormatLink(id int) string {
return fmt.Sprintf("%s/items/%d", SnUrl, id)
}

View File

@ -5,7 +5,8 @@ import (
"fmt"
)
func HasNewNotes() (bool, error) {
// Check for new notifications
func CheckNotifications() (bool, error) {
body := GraphQLPayload{
Query: `
{

View File

@ -5,6 +5,7 @@ import (
"fmt"
)
// Create a new LINK post
func PostLink(url string, title string, sub string) (int, error) {
body := GraphQLPayload{
Query: `
@ -39,6 +40,7 @@ func PostLink(url string, title string, sub string) (int, error) {
return itemId, nil
}
// Create a new comment
func CreateComment(parentId int, text string) (int, error) {
body := GraphQLPayload{
Query: `