hn

package
v0.0.0-...-c470439 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2019 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package hn implements a really basic Hacker News client

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

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

Example
package main

import (
	"fmt"

	"github.com/gophercises/quiet_hn/hn"
)

func main() {
	var client hn.Client
	ids, err := client.TopItems()
	if err != nil {
		panic(err)
	}
	for i := 0; i < 5; i++ {
		item, err := client.GetItem(ids[i])
		if err != nil {
			panic(err)
		}
		fmt.Printf("%s (by %s)\n", item.Title, item.By)
	}
}
Output:

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"`
}

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.

Jump to

Keyboard shortcuts

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