- Notifications
You must be signed in to change notification settings - Fork2
Hacker News API wrapper for Go
License
NotificationsYou must be signed in to change notification settings
alexferrari88/GoHN
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
GoHN is a wrapper for theHacker News API for Go, inspired by the excellentgo-github library.
It facilitates the use of the API by providing a simple interface to the API endpoints.
- Get the top/new/best/ask/show/job stories
- Retrieve all comments (with metadata) for a story using goroutines to speed up the process
- Retrieve the comments ordered as they appear in the story on the website
- Apply filters to retrieved items (stories, comments)
- Can be used with a custom http.Client instance (to use a proxy, for example)
Refer to theGoDoc for the full API reference.
Refer toexample/main.go for a full example on how to retrieve the top stories' IDs and the all the comments for the first one.
// Instantiate a new client to retrieve data from the Hacker News APIhn,_:=gohn.NewClient(nil)// Use background contextctx:=context.Background()// Get the top 500 stories' IDstopStoriesIds,_:=hn.Stories.GetTopIDs(ctx)varstory*gohn.Item// Retrieve the details of the first oneiflen(topStoriesIds)>0&&topStoriesIds[0]!=nil {story,_=hn.Items.Get(ctx,*topStoriesIds[0]) }ifstory==nil {panic("No story found") }// Print the story's titlefmt.Println("Title:",*story.Title)// Print the story's authorfmt.Println("Author:",*story.By)// Print the story's scorefmt.Println("Score:",*story.Score)// Print the story's URLfmt.Println("URL:",*story.URL)fmt.Println()fmt.Println()ifstory.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 characterscommentsMap,err:=hn.Items.FetchAllKids(ctx,story,processors.UnescapeHTML())iferr!=nil {panic(err) }iflen(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 commentsstoryWithComments:= gohn.Story{Parent:story,CommentsByIdMap:commentsMap, }// Calculate the position of each comment in the storystoryWithComments.SetCommentsPosition()// Get an ordered list of comments' IDs (ordered by position)orderedIDs,err:=storyWithComments.GetOrderedCommentsIDs()iferr!=nil {panic(err) }// Print the commentsfor_,id:=rangeorderedIDs {comment:=commentsMap[id]ifcomment.Text!=nil {fmt.Println(*comment.Text)fmt.Println() } }
As this library is not yet in version 1.0.0, the API may have breaking changes between minor versions.
Feel free to fork this repo and create a PR. I will review them and merge, if ok.
About
Hacker News API wrapper for Go
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.