hackernews

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 9, 2024 License: MIT Imports: 10 Imported by: 4

README

hackernews

GoDoc

Simple Hacker News client for Go. Currently only supports reading from Hacker News.

Examples

List stories from the front page
hn := hackernews.New()
ctx := context.Background()
stories, err := hn.FrontPage(ctx)
Get the newest stories
hn := hackernews.New()
ctx := context.Background()
stories, err := hn.Newest(ctx)
Find a story and its comments
hn := hackernews.New()
ctx := context.Background()
story, err := hn.Find(ctx, 1)

Install

go get -u github.com/matthewmueller/hackernews

Authors

License

MIT

Documentation

Overview

Package hackernews is a simple HTTP client for Hacker News.

Algolia graciously provided an API for working with Hacker News over at: https://hn.algolia.com/api

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Children

type Children struct {
	ID         int        `json:"id,omitempty"`
	CreatedAt  time.Time  `json:"created_at,omitempty"`
	CreatedAtI int        `json:"created_at_i,omitempty"`
	Type       string     `json:"type,omitempty"`
	Author     *string    `json:"author,omitempty"`
	Title      *string    `json:"title,omitempty"`
	URL        *string    `json:"url,omitempty"`
	Text       *string    `json:"text,omitempty"`
	Points     *int       `json:"points,omitempty"`
	ParentID   int        `json:"parent_id,omitempty"`
	StoryID    int        `json:"story_id,omitempty"`
	Children   []Children `json:"children"`
}

Children are the comments.

type Client

type Client struct {
	*http.Client
}

Client for HackerNews. The HTTP Client can be overriden with your own.

Example
package main

import (
	"context"
	"fmt"

	"github.com/matthewmueller/hackernews"
)

func main() {
	ctx := context.Background()
	hn := hackernews.New()
	stories, _ := hn.FrontPage(ctx)
	fmt.Println(len(stories) >= 10)
}
Output:

true

func New

func New() *Client

New HackerNews Client with defaults

func (*Client) AskHN

func (c *Client) AskHN(ctx context.Context) ([]*Story, error)

AskHN is a convenience function for getting the results on https://news.ycombinator.com/ask

func (*Client) Find

func (c *Client) Find(ctx context.Context, id int) (*Story, error)

Find a Story by its id.

func (*Client) FrontPage

func (c *Client) FrontPage(ctx context.Context) ([]*Story, error)

FrontPage is a convenience function for getting the results on https://hackernews.com

func (*Client) Newest

func (c *Client) Newest(ctx context.Context) ([]*Story, error)

Newest is a convenience function for getting the results on https://news.ycombinator.com/newest

func (*Client) Search

func (c *Client) Search(ctx context.Context, search *SearchRequest) (*SearchResponse, error)

Search for Stories. Sorted by relevance, then points, then number of comments.

func (*Client) SearchRecent

func (c *Client) SearchRecent(ctx context.Context, search *SearchRequest) (*SearchResponse, error)

Search for Stories. Sorted by date, more recent first.

func (*Client) ShowHN

func (c *Client) ShowHN(ctx context.Context) ([]*Story, error)

ShowHN is a convenience function for getting the results on https://news.ycombinator.com/show

type Highlight

type Highlight struct {
	Value        string   `json:"value,omitempty"`
	MatchLevel   string   `json:"matchLevel,omitempty"`
	MatchedWords []string `json:"matchedWords,omitempty"`
}

Highlight indicates the words that matched the search query

type Hit added in v0.6.0

type Hit struct {
	ID             string    `json:"objectID,omitempty"`
	Title          string    `json:"title,omitempty"`
	URL            string    `json:"url,omitempty"`
	Author         string    `json:"author,omitempty"`
	CreatedAt      time.Time `json:"created_at,omitempty"`
	Points         int       `json:"points,omitempty"`
	StoryText      *string   `json:"story_text,omitempty"`
	CommentText    *string   `json:"comment_text,omitempty"`
	NumComments    *int      `json:"num_comments,omitempty"`
	StoryID        *int      `json:"story_id,omitempty"`
	StoryTitle     *string   `json:"story_title,omitempty"`
	StoryURL       *string   `json:"story_url,omitempty"`
	ParentID       *int      `json:"parent_id,omitempty"`
	CreatedAtI     int       `json:"created_at_i,omitempty"`
	RelevancyScore *int      `json:"relevancy_score,omitempty"`
	Tags           []string  `json:"_tags,omitempty"`
	Highlights     struct {
		Title     Highlight `json:"title,omitempty"`
		URL       Highlight `json:"url,omitempty"`
		Author    Highlight `json:"author,omitempty"`
		StoryText Highlight `json:"story_text,omitempty"`
	} `json:"_highlightResult,omitempty"`
	Children []int `json:"children"`
}

Hit is an individual search result (story or comment)

type SearchRequest added in v0.6.0

type SearchRequest struct {
	// Full-text query to search for (e.g. Duo)
	Query string

	// Tags filters the search on a specific tag.
	//
	// The available tags are:
	//   - story
	//   - comment
	//   - poll
	//   - pollopt
	//   - show_hn
	//   - ask_hn,
	//   - front_page
	//   - author_:USERNAME
	//   - story_:ID
	//
	// Tags are ANDed by default, can be ORed if between parenthesis. For example,
	// `author_pg,(story,poll)` filters on `author=pg AND (type=story OR type=poll)`
	Tags string

	// Filter by points. Points is a conditional query, so you can request stories
	// that have more than 500 points with "points > 500".
	Points string

	// Filter by date. CreatedAt is a conditional query, so you can request
	// stories between a time period wtih "created_at_i>X,created_at_i<Y", where
	// X and Y are timestamps in seconds.
	CreatedAt string

	// Filter by the number of comments. Comments is a conditional query, so you
	// can request stories that have more than 10 comments with "comments > 10".
	NumComments string

	// The page number
	Page int

	// ResultsPerPage is the number of results. Defaults to 34.
	ResultsPerPage int
}

SearchRequest query and filters

type SearchResponse added in v0.6.0

type SearchResponse struct {
	Stories              []*Story `json:"stories,omitempty"`
	Hits                 []*Hit   `json:"hits,omitempty"`
	NumResults           int      `json:"nbHits,omitempty"`
	Page                 int      `json:"page,omitempty"`
	NumPages             int      `json:"nbPages,omitempty"`
	ResultsPerPage       int      `json:"hitsPerPage,omitempty"`
	ExhaustiveNumResults bool     `json:"exhaustiveNbHits,omitempty"`
	Query                string   `json:"query,omitempty"`
	Params               string   `json:"params,omitempty"`
	ProcessingTimeMS     int      `json:"processingTimeMS,omitempty"`
}

SearchResponse of a search

type Story

type Story struct {
	ID          int        `json:"id,omitempty"`
	CreatedAt   time.Time  `json:"created_at,omitempty"`
	CreatedAtI  int        `json:"created_at_i,omitempty"`
	Type        string     `json:"type,omitempty"`
	Author      string     `json:"author,omitempty"`
	Title       string     `json:"title,omitempty"`
	URL         string     `json:"url,omitempty"`
	Text        *string    `json:"text,omitempty"`
	NumComments *int       `json:"num_comments,omitempty"`
	Points      int        `json:"points,omitempty"`
	ParentID    *int       `json:"parent_id,omitempty"`
	StoryID     *int       `json:"story_id,omitempty"`
	Children    []Children `json:"children"`
}

Story is an individual entry on HackerNews.

Jump to

Keyboard shortcuts

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