Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Hacker News API wrapper for Go

License

NotificationsYou must be signed in to change notification settings

alexferrari88/GoHN

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Reference

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.

Features 🚀

  • 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)

Usage 💻

Refer to theGoDoc for the full API reference.

Example

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()        }    }

Semantic Versioning 🥚

As this library is not yet in version 1.0.0, the API may have breaking changes between minor versions.

Contributing 🤝🏼

Feel free to fork this repo and create a PR. I will review them and merge, if ok.

License 📝

MIT


[8]ページ先頭

©2009-2025 Movatter.jp