hackernews

package
v0.0.0-...-94bc88a Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2024 License: ISC Imports: 7 Imported by: 0

Documentation

Overview

Package hackernews provides an API wrapper around the HackerNews API. Some of these codes are taken from https://github.com/peterhellberg/hn. The API reference is in https://github.com/HackerNews/API.

Index

Constants

View Source
const BaseEndpoint = "https://hacker-news.firebaseio.com/v0"

BaseEndpoint is the base endpoint used by the Client.

Variables

View Source
var DefaultClient = &Client{
	Client: *http.DefaultClient,
}

DefaultClient is the default HackerNews client.

Functions

This section is empty.

Types

type Client

type Client struct {
	http.Client
}

Client is a HackerNews client.

func (*Client) Get

func (c *Client) Get(ctx context.Context, path string, jsonVal interface{}) error

Get gets the given path (concatenated after BaseEndpoint) and unmarshals the body into jsonVal if it's not nil.

func (*Client) Item

func (c *Client) Item(ctx context.Context, id ItemID) (*Item, error)

Item fetches a single item by its ID.

func (*Client) Stories

func (c *Client) Stories(ctx context.Context, file StoryFile) ([]ItemID, error)

Stories fetches a list of stories, returning their item IDs.

type Item

type Item struct {
	ID      ItemID `json:"id"`
	Deleted bool   `json:"deleted"`
	// Type is the item's type.
	Type ItemType `json:"type"`
	// By is the author's username.
	By string `json:"by"`
	// Time is the creation time.
	Time UnixTime `json:"time"`
	// Text is the comment, story or poll text in HTML.
	Text string `json:"text"`
	// Dead is true if the item is dead.
	Dead bool `json:"dead"`
	// Parent is the comment's parent, which is another comment or relevant
	// story.
	Parent ItemID `json:"parent"`
	// Kids is the IDs of the item's comments in ranked display order.
	Kids []ItemID `json:"kids"`
	// URL is the URL of the story.
	URL string `json:"url"`
	// Score is the story's score or the votes for a pollopt.
	Score int `json:"score"`
	// Title is the title of the story in HTML.
	Title string `json:"title"`
	// Parts is the list of relateed pollopts, in display order.
	Parts []ItemID `json:"parts"`
	// Descendants is the total comment count in case of stories or polls.
	Descendants int `json:"descendants"`
}

Item represents an item.

type ItemID

type ItemID int

ItemID is the ID if a post (item).

type ItemType

type ItemType string

ItemType describes the type of an item.

const (
	JobItem     ItemType = "job"
	StoryItem   ItemType = "story"
	CommentItem ItemType = "comment"
	PollItem    ItemType = "poll"
	PollOptItem ItemType = "pollopt"
)

type StoryFile

type StoryFile string

StoryFile enumerates the possible types of stories to be fetched.

const (
	NewStories  StoryFile = "newstories.json"
	TopStories  StoryFile = "topstories.json"
	BestStories StoryFile = "beststories.json"
	AskStories  StoryFile = "askstories.json"
	ShowStories StoryFile = "showstories.json"
	JobStories  StoryFile = "jobstories.json"
)

func (StoryFile) Name

func (f StoryFile) Name() string

Name returns the human-friendly name of the story file instead of the JSON path name.

type UnixTime

type UnixTime int64

UnixTime is a timestamp number in Unix epoch time.

func (UnixTime) Time

func (t UnixTime) Time() time.Time

Time returns the Unix time as a time.Time.

Directories

Path Synopsis
Package hnhtml implements an HTML-to-Pango-markup translator that translates a small subset of HTML, enough to view HackerNews posts.
Package hnhtml implements an HTML-to-Pango-markup translator that translates a small subset of HTML, enough to view HackerNews posts.

Jump to

Keyboard shortcuts

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