diff --git a/README.md b/README.md new file mode 100644 index 0000000..a3312e7 --- /dev/null +++ b/README.md @@ -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. diff --git a/dupes.go b/dupes.go index 3b1b158..4f9a524 100644 --- a/dupes.go +++ b/dupes.go @@ -5,6 +5,7 @@ import ( "fmt" ) +// Fetch dupes func Dupes(url string) (*[]Dupe, error) { body := GraphQLPayload{ Query: ` diff --git a/main.go b/main.go index 02c723f..bbd3dcc 100644 --- a/main.go +++ b/main.go @@ -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) } diff --git a/notes.go b/notes.go index d7fef52..80e42a8 100644 --- a/notes.go +++ b/notes.go @@ -5,7 +5,8 @@ import ( "fmt" ) -func HasNewNotes() (bool, error) { +// Check for new notifications +func CheckNotifications() (bool, error) { body := GraphQLPayload{ Query: ` { diff --git a/post.go b/post.go index 3aaf994..14713d2 100644 --- a/post.go +++ b/post.go @@ -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: `