hackernews

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 14, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

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

View Source
const DefaultUserAgent = "hn/dev (+https://github.com/tamnd/hackernews-cli)"

DefaultUserAgent identifies the client to both APIs.

Variables

View Source
var ErrNotFound = errors.New("not found")

ErrNotFound is returned when the Firebase API returns null for an id or user.

Functions

func ParseItemID

func ParseItemID(s string) (int, error)

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

func ParseUsername(s string) (string, error)

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 NewClient

func NewClient(cfg Config) *Client

NewClient returns a Client with the given config.

func (*Client) Item

func (c *Client) Item(ctx context.Context, id int, depth int) (Story, []Comment, error)

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

func (c *Client) MaxItem(ctx context.Context) (int, error)

MaxItem returns the current maximum item id.

func (*Client) Search

func (c *Client) Search(ctx context.Context, opts SearchOptions) ([]SearchHit, error)

Search searches HN via Algolia.

func (*Client) StoryList

func (c *Client) StoryList(ctx context.Context, endpoint string, limit int) ([]Story, error)

StoryList fetches a named story list and resolves the top limit ids to stories. endpoint is one of: topstories, beststories, newstories, askstories, showstories, jobstories.

func (*Client) Updates

func (c *Client) Updates(ctx context.Context) (Updates, error)

Updates returns recently changed item ids and profiles.

func (*Client) User

func (c *Client) User(ctx context.Context, username string) (User, []int, error)

User fetches a user profile. Returns the profile, the raw submitted ids, and any error.

func (*Client) UserSubmissions

func (c *Client) UserSubmissions(ctx context.Context, ids []int, limit int) ([]Story, error)

UserSubmissions resolves a user's submitted ids to Story records.

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.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns sensible defaults.

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.

type Updates

type Updates struct {
	Items    []int    `json:"items"`
	Profiles []string `json:"profiles"`
}

Updates is returned by the /updates endpoint.

func (Updates) Changes added in v0.1.1

func (u Updates) Changes() []Change

Changes flattens the updates payload into a single ordered record slice, items first then profiles, matching the order HN returns them.

type User

type User struct {
	Username    string `json:"username"`
	Karma       int    `json:"karma"`
	Created     int64  `json:"created"`
	CreatedDate string `json:"created_date"`
	About       string `json:"about"`
	Submitted   int    `json:"submitted"`
	URL         string `json:"url"`
}

User is the record emitted for HN user profiles.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL