hn

package
v0.0.0-...-4df7676 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package hn implements a really basic Hacker News client

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	ExpirationDuration time.Duration
	// contains filtered or unexported fields
}

Cache Hacker News stories

func NewCache

func NewCache(expirationDuration time.Duration) *Cache

NewCache instanciates a new cache

func NewDefaultCache

func NewDefaultCache() *Cache

NewDefaultCache instanciates a new cache with default expiration duration

func (*Cache) Add

func (c *Cache) Add(i Item)

Add the story into the cache

func (*Cache) Get

func (c *Cache) Get(id int) *Item

Get the item from cache

func (*Cache) Invalidate

func (c *Cache) Invalidate(id int)

Invalidate cache from a given id

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an API client used to interact with the Hacker News API

func (*Client) GetItem

func (c *Client) GetItem(id int) (Item, error)

GetItem will return the Item defined by the provided ID.

func (*Client) TopItems

func (c *Client) TopItems() ([]int, error)

TopItems returns the ids of roughly 450 top items in decreasing order. These should map directly to the top 450 things you would see on HN if you visited their site and kept going to the next page.

TopItems does not filter out job listings or anything else, as the type of each item is unknown without further API calls.

type Item

type Item struct {
	By          string `json:"by"`
	Descendants int    `json:"descendants"`
	ID          int    `json:"id"`
	Kids        []int  `json:"kids"`
	Score       int    `json:"score"`
	Time        int    `json:"time"`
	Title       string `json:"title"`
	Type        string `json:"type"`

	// Only one of these should exist
	Text string `json:"text"`
	URL  string `json:"url"`
	// specific attribute
	Host string
}

Item represents a single item returned by the HN API. This can have a type of "story", "comment", or "job" (and probably more values), and one of the URL or Text fields will be set, but not both.

For the purpose of this exercise, we only care about items where the type is "story", and the URL is set.

func GetTopStories

func GetTopStories(numStories int, timeout time.Duration) ([]Item, error)

GetTopStories fetches the HN top stories

Jump to

Keyboard shortcuts

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