Documentation
¶
Overview ¶
Package hackernews is the library behind the hn command: the HTTP client, request shaping, and the typed data models for Hacker News.
Two APIs: the official Firebase endpoint at hacker-news.firebaseio.com for live data, and the Algolia endpoint at hn.algolia.com for full-text search. Both are open, no key required.
Index ¶
- Constants
- Variables
- func ParseItemID(s string) (int, error)
- func ParseUsername(s string) (string, error)
- type Change
- type Client
- func (c *Client) Item(ctx context.Context, id int, depth int) (Story, []Comment, error)
- func (c *Client) MaxItem(ctx context.Context) (int, error)
- func (c *Client) Search(ctx context.Context, opts SearchOptions) ([]SearchHit, error)
- func (c *Client) StoryList(ctx context.Context, endpoint string, limit int) ([]Story, error)
- func (c *Client) Updates(ctx context.Context) (Updates, error)
- func (c *Client) User(ctx context.Context, username string) (User, []int, error)
- func (c *Client) UserSubmissions(ctx context.Context, ids []int, limit int) ([]Story, error)
- type Comment
- type Config
- type SearchHit
- type SearchOptions
- type Story
- type Updates
- type User
Constants ¶
const DefaultUserAgent = "hn/dev (+https://github.com/tamnd/hackernews-cli)"
DefaultUserAgent identifies the client to both APIs.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when the Firebase API returns null for an id or user.
Functions ¶
func ParseItemID ¶
ParseItemID extracts a numeric HN item id from a bare integer string, a news.ycombinator.com/item?id=N URL, or a full https URL.
func ParseUsername ¶
ParseUsername extracts a HN username from a bare name or a news.ycombinator.com/user?id=name URL.
Types ¶
type Change ¶ added in v0.1.1
type Change struct {
Kind string `json:"kind"` // "item" or "profile"
Value string `json:"value"` // item id (decimal) or username
URL string `json:"url"` // canonical HN link
}
Change is one entry from the updates firehose: either a changed item or a changed profile. It is the flat record the updates command renders, so the firehose prints in any output format like every other surface.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client talks to the HN Firebase and Algolia APIs.
func (*Client) Item ¶
Item fetches a single item and its comment tree to depth levels. depth=0 returns the item only; depth=-1 returns the full tree.
func (*Client) StoryList ¶
StoryList fetches a named story list and resolves the top limit ids to stories. endpoint is one of: topstories, beststories, newstories, askstories, showstories, jobstories.
type Comment ¶
type Comment struct {
ID int `json:"id"`
By string `json:"by"`
Parent int `json:"parent"`
Time int64 `json:"time"`
Date string `json:"date"`
Depth int `json:"depth"`
Text string `json:"text"`
HNURL string `json:"hn_url"`
}
Comment is the record emitted for comment items.
type Config ¶
type Config struct {
UserAgent string
Rate time.Duration
Retries int
Workers int
Timeout time.Duration
}
Config holds constructor parameters.
type SearchHit ¶
type SearchHit struct {
Rank int `json:"rank"`
ID int `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
By string `json:"by"`
Score int `json:"score"`
Comments int `json:"comments"`
Time int64 `json:"time"`
Date string `json:"date"`
Text string `json:"text"`
StoryID int `json:"story_id,omitempty"`
StoryTitle string `json:"story_title,omitempty"`
HNURL string `json:"hn_url"`
}
SearchHit is the record for an Algolia search result (story or comment).
type SearchOptions ¶
type SearchOptions struct {
Query string
Tags string
Sort string
Since string
MinPoints int
MinComments int
Limit int
}
SearchOptions controls the Algolia query.
type Story ¶
type Story struct {
Rank int `json:"rank"`
ID int `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
URL string `json:"url"`
By string `json:"by"`
Score int `json:"score"`
Comments int `json:"comments"`
Time int64 `json:"time"`
Date string `json:"date"`
Text string `json:"text"`
HNURL string `json:"hn_url"`
}
Story is the record emitted for stories, Ask HN, Show HN, and job posts.