Documentation ¶
Overview ¶
Package GoHN is a wrapper for the Hacker News API: https://github.com/HackerNews/API
Example usage: package main import ( "context" "fmt" "github.com/alexferrari88/gohn/pkg/gohn" "github.com/alexferrari88/gohn/pkg/processors" ) func main() { // Instantiate a new client to retrieve data from the Hacker News API hn := gohn.NewClient(nil) // Use background context ctx := context.Background() // Get the top 500 stories' IDs topStoriesIds, _ := hn.Stories.GetTopIDs(ctx) var story *gohn.Item // Retrieve the details of the first one if len(topStoriesIds) > 0 && topStoriesIds[0] != nil { story, _ = hn.Items.Get(ctx, *topStoriesIds[0]) } if story == nil { panic("No story found") } // Print the story's title fmt.Println("Title:", *story.Title) // Print the story's author fmt.Println("Author:", *story.By) // Print the story's score fmt.Println("Score:", *story.Score) // Print the story's URL fmt.Println("URL:", *story.URL) fmt.Println() fmt.Println() if story.Kids == nil { fmt.Println("No comments found") return } // Retrieve all the comments for that story // UnescapeHTML is applied to each retrieved item to unescape HTML characters commentsMap, err := hn.Items.FetchAllDescendants(ctx, story, processors.UnescapeHTML()) if err != nil { panic(err) } if len(commentsMap) == 0 { fmt.Println("No comments found") return } fmt.Printf("Comments found: %d\n", len(commentsMap)) fmt.Println() // Create a Story struct to hold the story and its comments storyWithComments := gohn.Story{ Parent: story, CommentsByIdMap: commentsMap, } // Calculate the position of each comment in the story storyWithComments.SetCommentsPosition() // Get an ordered list of comments' IDs (ordered by position) orderedIDs, err := storyWithComments.GetOrderedCommentsIDs() if err != nil { panic(err) } // Print the comments for _, id := range orderedIDs { comment := commentsMap[id] if comment.Text != nil { fmt.Println(*comment.Text) fmt.Println() } } }
Index ¶
- Constants
- func CheckResponse(r *http.Response) error
- type Client
- type InvalidItemError
- type Item
- type ItemProcessor
- type ItemsIndex
- type ItemsService
- func (s *ItemsService) FetchAllDescendants(ctx context.Context, item *Item, fn ItemProcessor) (ItemsIndex, error)
- func (s *ItemsService) Get(ctx context.Context, id int) (*Item, error)
- func (s *ItemsService) GetIDsFromURL(ctx context.Context, url string) ([]*int, error)
- func (s *ItemsService) GetMaxID(ctx context.Context) (*int, error)
- func (s *ItemsService) GetStoryIdFromComment(ctx context.Context, item *Item) (*int, error)
- type ResponseError
- type StoriesService
- func (s *StoriesService) GetAskIDs(ctx context.Context) ([]*int, error)
- func (s *StoriesService) GetBestIDs(ctx context.Context) ([]*int, error)
- func (s *StoriesService) GetIDsFromURL(ctx context.Context, url string) ([]*int, error)
- func (s *StoriesService) GetJobIDs(ctx context.Context) ([]*int, error)
- func (s *StoriesService) GetNewIDs(ctx context.Context) ([]*int, error)
- func (s *StoriesService) GetShowIDs(ctx context.Context) ([]*int, error)
- func (s *StoriesService) GetTopIDs(ctx context.Context) ([]*int, error)
- type Story
- type Update
- type UpdatesService
- type User
- type UsersService
Constants ¶
const ( BASE_URL = "https://hacker-news.firebaseio.com/v0/" USER_AGENT = "GoHN/" + Version )
const ( ITEM_URL = "item/%d.json" MAX_ITEM_ID_URL = "maxitem.json" )
ITEM_URL is the URL to retrieve an item given an ID. MAX_ITEM_ID_URL is the URL that retrieves the current largest item id.
const ( TOP_STORIES_URL = "topstories.json" BEST_STORIES_URL = "beststories.json" NEW_STORIES_URL = "newstories.json" ASK_STORIES_URL = "askstories.json" SHOW_STORIES_URL = "showstories.json" JOB_STORIES_URL = "jobstories.json" )
URLs to retrieve the IDs of the top stories, best stories, new stories, ask stories, show stories and job stories.
const (
UPDATES_URL = "updates.json"
)
UPDATES_URL is the URL for the updates endpoint.
const (
USER_URL = "user/%s.json"
)
USER_URL is the URL for the user endpoint.
const (
Version = "0.8.0"
)
Variables ¶
This section is empty.
Functions ¶
func CheckResponse ¶ added in v0.6.0
CheckResponse checks the API response for errors, and returns them if present.
Types ¶
type Client ¶ added in v0.6.0
type Client struct { BaseURL *url.URL Items *ItemsService Stories *StoriesService Users *UsersService Updates *UpdatesService UserAgent string // contains filtered or unexported fields }
Client manages communication with the Hacker News API.
func NewClient ¶
NewClient returns a new Client that will be used to make requests to the Hacker News API. If a nil httpClient is provided, http.Client will be used.
func (*Client) Do ¶ added in v0.6.0
Do sends an API request and returns the API response. The Hacker News API returns JSON which is decoded and stored in the value pointed to by v, or returned as an error if an API error has occurred.
func (*Client) GetHTTPClient ¶ added in v0.6.0
GetHTTPClient returns the HTTP client used by the Client.
type InvalidItemError ¶ added in v0.8.0
type InvalidItemError struct {
Message string
}
func (InvalidItemError) Error ¶ added in v0.8.0
func (e InvalidItemError) Error() string
type Item ¶
type Item struct { ID *int `json:"id,omitempty"` Deleted *bool `json:"deleted,omitempty"` Type *string `json:"type,omitempty"` By *string `json:"by,omitempty"` Time *int `json:"time,omitempty"` Text *string `json:"text,omitempty"` Dead *bool `json:"dead,omitempty"` Parent *int `json:"parent,omitempty"` Poll *int `json:"poll,omitempty"` Kids *[]int `json:"kids,omitempty"` Position *int URL *string `json:"url,omitempty"` Score *int `json:"score,omitempty"` Title *string `json:"title,omitempty"` Parts *[]int `json:"parts,omitempty"` Descendants *int `json:"descendants,omitempty"` }
Item represents a single item from the Hacker News API. https://github.com/HackerNews/API#items
type ItemProcessor ¶
ItemProcessor is used by ItemsService.Get and ItemsService.FetchAllKids to process items after they are retrieved. The package itemprocessor provides some common implementations.
type ItemsIndex ¶ added in v0.4.0
ItemsIndex is a map of Items indexed by their ID.
type ItemsService ¶ added in v0.6.0
type ItemsService service
ItemService handles retrieving items from the Hacker News API.
func (*ItemsService) FetchAllDescendants ¶ added in v0.6.0
func (s *ItemsService) FetchAllDescendants(ctx context.Context, item *Item, fn ItemProcessor) (ItemsIndex, error)
FetchAllDescendants returns a map of all the comments for a given Item. The map key is the ID of the item. The map value is a pointer to the item itself. The map can be used to retrieve the comments for a given item by traversing the Kids slice of the item recursively (N-ary tree preorder traversal). See an implementation in the example directory. If the ItemProcessor returns an error, the item will not be added to the map. Its kids will be added to the queue only if the ItemProcessors returns false, together with the error. For more information on the ItemProcessor, check the gohn/processors package.
func (*ItemsService) GetIDsFromURL ¶ added in v0.6.0
GetIDsFromURL returns a slice of Items' IDs for the given URL.
func (*ItemsService) GetMaxID ¶ added in v0.6.0
func (s *ItemsService) GetMaxID(ctx context.Context) (*int, error)
GetMaxID returns the ID of the most recent item. https://github.com/HackerNews/API#max-item-id
func (*ItemsService) GetStoryIdFromComment ¶ added in v0.6.0
GetStoryIdFromComment returns the ID of the story for a given comment.
type ResponseError ¶ added in v0.8.0
func (*ResponseError) Error ¶ added in v0.8.0
func (r *ResponseError) Error() string
type StoriesService ¶ added in v0.6.0
type StoriesService service
StoriesService provides access to the stories endpoints of the Hacker News API.
func (*StoriesService) GetAskIDs ¶ added in v0.6.0
func (s *StoriesService) GetAskIDs(ctx context.Context) ([]*int, error)
GetAskIDs returns the IDs of up to 200 of the latest Ask stories on Hacker News.
func (*StoriesService) GetBestIDs ¶ added in v0.6.0
func (s *StoriesService) GetBestIDs(ctx context.Context) ([]*int, error)
GetBestIDs returns the IDs of up to 500 of the best stories on Hacker News.
func (*StoriesService) GetIDsFromURL ¶ added in v0.6.0
GetIDsFromURL returns a slice of IDs from a Hacker News API endpoint.
func (*StoriesService) GetJobIDs ¶ added in v0.6.0
func (s *StoriesService) GetJobIDs(ctx context.Context) ([]*int, error)
GetJobIDs returns the IDs of up to 200 of the latest Job stories on Hacker News.
func (*StoriesService) GetNewIDs ¶ added in v0.6.0
func (s *StoriesService) GetNewIDs(ctx context.Context) ([]*int, error)
GetNewIDs returns the IDs of up to 500 of the newest stories on Hacker News.
func (*StoriesService) GetShowIDs ¶ added in v0.6.0
func (s *StoriesService) GetShowIDs(ctx context.Context) ([]*int, error)
GetShowIDs returns the IDs of up to 200 of the latest Show stories on Hacker News.
type Story ¶ added in v0.4.0
type Story struct { Parent *Item CommentsByIdMap ItemsIndex }
Story represents a story on Hacker News. It contains the pointer to the story item as Parent and a map of comments by ID as CommentsByIdMap.
func (*Story) GetOrderedCommentsIDs ¶ added in v0.5.0
GetOrderedCommentsIDs orders the comments in a Story by their Position field and returns a slice of comments IDs in that order.
func (*Story) IsTopLevelComment ¶ added in v0.4.0
IsTopLevelComment checks if an Item is a top level comment in a story.
func (*Story) SetCommentsPosition ¶ added in v0.5.0
func (s *Story) SetCommentsPosition()
SetCommentsPosition calculates the order of the comments in a story. The order is calculated by traversing the Kids slice of the Story.Parent item recursively (N-ary tree preorder traversal). The order is stored in the Position field of the Item struct.
type Update ¶
Update represents a items and profiles changes from the Hacker News API. https://github.com/HackerNews/API#changed-items-and-profiles
type UpdatesService ¶ added in v0.6.0
type UpdatesService service
UpdatesService handles communication with the updates related methods of the Hacker News API.
func (UpdatesService) Get ¶ added in v0.6.0
func (s UpdatesService) Get(ctx context.Context) (*Update, error)
Get items and profiles changes. https://github.com/HackerNews/API#changed-items-and-profiles
type User ¶
type User struct { ID *string `json:"id"` Created *int `json:"created"` Karma *int `json:"karma"` About *string `json:"about"` Submitted *[]int `json:"submitted"` }
User represents a single user from the Hacker News API. https://github.com/HackerNews/API#users
type UsersService ¶ added in v0.6.0
type UsersService service
UsersService handles communication with the user related methods of the Hacker News API.
func (*UsersService) GetByUsername ¶ added in v0.6.0
GetByUsername returns a User given a username.