hackernews

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2022 License: Apache-2.0 Imports: 7 Imported by: 1

README

HackerNews API

PkgGoDev codecov Tests CodeQL golangci-lint Go Report Card

Easy to use client for the HackerNews API
Full HackerNews API documentation can be found here: https://github.com/HackerNews/API

See an example of usage in this CLI

Available Functions
GetPaginatedStories(storyType StoryType, storiesPerPage int, pageNumber int) (PaginatedStoriesResponse, error)
GetStories(storyType StoryType) (Stories, error)
GetItem(itemID int) (Item, error)
GetUser(userID string) (User, error)
GetMaxItem() (MaxItem, error)
GetUpates() (Updates, error)
HydrateItems(itemIDs []int) ([]Item, error)
Example
import "github.com/brandenc40/hackernews"

// Grab the item ids of all top stories
topStories, err := hackernews.GetStories(hackernews.StoriesTop)
if err != nil {
    // handle error
}

// Hydrate that list of item ids into a list of Item structs
hydratedStories, err := hackernews.HydrateItems(topStories)
if err != nil {
    // handle error
}

topStory := hydratedStories[0]

// Get the user who posted the top story
user, err := hackernews.GetUser(topStory.By)
if err != nil {
    // handle error
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item struct {
	ID          int      `json:"id"`
	Deleted     string   `json:"deleted"`
	Type        ItemType `json:"type"`
	By          string   `json:"by"`
	Time        int64    `json:"time"`
	Text        string   `json:"text"`
	Dead        bool     `json:"dead"`
	Parent      int      `json:"parent"`
	Poll        int      `json:"poll"`
	Kids        []int    `json:"kids"`
	URL         string   `json:"url"`
	Score       int      `json:"score"`
	Title       string   `json:"title"`
	Parts       []int    `json:"parts"`
	Descendants int      `json:"descendants"`
}

Item - GetItem() response struct. Items can be any of the ItemType options.

func GetItem

func GetItem(itemID int) (Item, error)

GetItem - Get a single item and return as an Item struct. Items can be on of "job", "story", "comment", "poll", or "pollopt". They're identified by their ids, which are unique integers.

API DOC: https://github.com/HackerNews/API#items

func HydrateItems

func HydrateItems(itemIDs []int) ([]Item, error)

HydrateItems concurrently hydrates a slice of item ids into Item structs. Requests that ask for multiple items such as GetStories() or GetUpdates() are returned only a slice of item ids. With this slice we need to pass each item id into GetItem() in order to get the full details. This HydrateItems function will fetch each ID in the slice of item ids concurrently to greatly improve execution time.

type ItemType

type ItemType string

ItemType describes what type of object the Item struct represents

const (
	// ItemTypeJob is a job item
	ItemTypeJob ItemType = "job"
	// ItemTypeStory is a story item
	ItemTypeStory ItemType = "story"
	// ItemTypeComment is a comment item
	ItemTypeComment ItemType = "comment"
	// ItemTypePoll is a poll item
	ItemTypePoll ItemType = "poll"
	// ItemTypePollOpt is a poll option item
	ItemTypePollOpt ItemType = "pollopt"
)

type MaxItem

type MaxItem int

MaxItem response struct. A single item ID.

func GetMaxItem

func GetMaxItem() (MaxItem, error)

GetMaxItem - The current largest item id.

API DOC: https://github.com/HackerNews/API#max-item-id

type PaginatedStoriesResponse

type PaginatedStoriesResponse struct {
	Stories      []Item
	Limit        int
	PageNumber   int
	TotalResults int
}

PaginatedStoriesResponse contains the story Items along with info regarding the page returned and the total possible results that can be accessed.

func GetPaginatedStories

func GetPaginatedStories(storyType StoryType, limit int, pageNumber int) (PaginatedStoriesResponse, error)

GetPaginatedStories returns paginated responses of fully hydrated story Items. If the page requested is larger than the available data, the results will contain an empty slice of story Items.

func (*PaginatedStoriesResponse) HasNextpage

func (p *PaginatedStoriesResponse) HasNextpage() bool

HasNextpage checks to see if there are more pages that can be returned. This will return false if requesting the next incremental page number will return 0 items.

type Stories

type Stories []int

Stories - GetStories() response struct. An array of item ids.

func GetStories

func GetStories(storyType StoryType) (Stories, error)

GetStories - Get a slice of all story ids of the type passed through the storyType argument.

StoryType can be one of the following:

  • StoriesTop
  • StoriesNew
  • StoriesBest
  • StoriesAsk
  • StoriesShow
  • StoriesJob

API DOCS:

type StoryType

type StoryType int

StoryType - Enum value for types of stories available

const (
	StoriesTop StoryType = iota
	StoriesNew
	StoriesBest
	StoriesAsk
	StoriesShow
	StoriesJob
)

Enum for types of stories

func (StoryType) Path

func (s StoryType) Path() string

Path returned the hackernews API path for the given StoryType

type Updates

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

Updates - GetUpdates() response struct

func GetUpates

func GetUpates() (Updates, error)

GetUpates - Item and profile changes.

API DOC: https://github.com/HackerNews/API#changed-items-and-profiles

type User

type User struct {
	ID        string `json:"id"`
	Delay     int    `json:"delay"`
	Created   int64  `json:"created"`
	Karma     int    `json:"karma"`
	About     string `json:"about"`
	Submitted []int  `json:"submitted"`
}

User - GetUser() response struct

func GetUser

func GetUser(userID string) (User, error)

GetUser - Get a single user and return as a User struct. Users are identified by case-sensitive ids.

API DOC: https://github.com/HackerNews/API#users

Jump to

Keyboard shortcuts

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