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" "fmt"
) )
// Fetch dupes
func Dupes(url string) (*[]Dupe, error) { func Dupes(url string) (*[]Dupe, error) {
body := GraphQLPayload{ body := GraphQLPayload{
Query: ` Query: `

11
main.go
View File

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

View File

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

View File

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