opinions

package module
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2023 License: MIT Imports: 9 Imported by: 0

README

opinions

Quality check status

Find opinions about a given phrase (also URL) on social news websites:

It can be used to include discussions on static websites/blogs.

opinions is a command-line replacement of discu.eu service. It directly calls search engines on underlying websites.

Application is developed with a security-first approach:

  • functionality is limited by design
  • access to the OS is restricted by application-level sandboxing (with pledge and seccomp).

Usage

$ opinions 'https://grugbrain.dev'
Lemmy	https://lemmy.world/post/5189937	The Grug Brained Developer - A layman's guide to thinking like the self-aware smol brained	https://grugbrain.dev/
Lemmy	https://lemmy.world/post/750451	The Grug Brained Developer	https://grugbrain.dev/
Lemmy	https://lemmy.world/post/685510	The Grug Brained Developer	https://grugbrain.dev/
Hacker News	https://news.ycombinator.com/item?id=31840331	The Grug Brained Developer	https://grugbrain.dev/
Hacker News	https://news.ycombinator.com/item?id=38076886	The Grug Brained Developer (2022)	https://grugbrain.dev/
Lobsters	https://lobste.rs/s/ifaar4/grug_brained_developer	The Grug Brained Developer	https://grugbrain.dev/
Lobsters	https://lobste.rs/s/pmpc9v/grug_brained_developer	The Grug Brained Developer	http://grugbrain.dev
Reddit	https://reddit.com/r/programming/comments/16gt8w4/the_grug_brained_developer/	The grug brained developer	https://grugbrain.dev
Reddit	https://reddit.com/r/brdev/comments/14jhm17/the_grug_brained_developer/	The Grug Brained Developer	https://grugbrain.dev

The result is printed to stdout as rows in format: <service_name><tab><URL><tab><title><tab><source_domain>.

Websites are queried with User-Agent: opinions/<version_number> (<os>; +https://github.com/macie/opinions).

Static Site Generators

opinions can be used to extend static websites with comments-like feature. You can search for active discussions about your article, filter out false positive results (eg. get only articles from your domain) and generate HTML links with standard Unix commands:

opinions 'Grug Brained' | grep 'grugbrain.dev' | awk -F '\t' '
BEGIN { print "<ul>" }
{ print "  <li><a href=\""$2"\" title=\"["$1"] "$3"\">"$1"</a></li>" }
END { print "</ul>" }
'

Installation

Download latest stable release from GitHub .

You can also build it manually with commands: make && make build (or without sandboxing: make && make unsafe).

Development

Use make (GNU or BSD):

  • make - install dependencies
  • make test - runs test
  • make e2e - runs e2e tests for CLI
  • make check - static code analysis
  • make build - compile binary from latest commit
  • make unsafe - compile binary from latest commit without security sandbox
  • make dist - compile binaries from latest commit for supported OSes (with proper version number)
  • make clean - removes compilation artifacts
  • make cli-release - tag latest commit as a new release of CLI
  • make info - print system info (useful for debugging).

Bugs

Results depend on search engines of underlying social news websites. They may be different than expected.

TODO

License

MIT

Documentation

Overview

Package opinions helps finding links to discussions about given topics/URLs on social news websites.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Discussion

type Discussion struct {
	Service  string
	URL      string
	Title    string
	Source   string
	Comments int
}

Discussion is a representation of discussion inside social media service.

func SearchHackerNews

func SearchHackerNews(ctx context.Context, client http.Client, query string) ([]Discussion, error)

SearchHackerNews query HN Search API for given prompt. It returns list of discussions sorted by relevance, then popularity, then number of comments.

See: https://hn.algolia.com/api

Example
client := http.Client{}
query := "https://grugbrain.dev"

opinions := ensure.MustReturn(SearchHackerNews(context.TODO(), client, query))

fmt.Println(opinions[0])
Output:
Hacker News	https://news.ycombinator.com/item?id=31840331	The Grug Brained Developer	https://grugbrain.dev/

func SearchLemmy added in v1.3.0

func SearchLemmy(ctx context.Context, client http.Client, query string) ([]Discussion, error)

SearchLemmy query Lemmy Search API for given prompt. It returns list of discussions sorted by rank based on the score and time of the latest comment, with decay over time.

See: https://join-lemmy.org/docs/users/03-votes-and-ranking.html

Example
client := http.Client{}
query := "https://grugbrain.dev/"

opinions := ensure.MustReturn(SearchLemmy(context.TODO(), client, query))

fmt.Println(opinions[0])
Output:
Lemmy	https://lemmy.world/post/7563451	The Grug Brained Developer (2022)	https://grugbrain.dev/

func SearchLobsters

func SearchLobsters(ctx context.Context, client http.Client, query string) ([]Discussion, error)

SearchLobsters query Lobsters search engine for given prompt. It returns list of discussions sorted by relevance.

Example
client := http.Client{}
query := "https://grugbrain.dev"

opinions := ensure.MustReturn(SearchLobsters(context.TODO(), client, query))

fmt.Println(opinions[0])
Output:
Lobsters	https://lobste.rs/s/ifaar4/grug_brained_developer	The Grug Brained Developer	https://grugbrain.dev/

func SearchReddit added in v1.4.0

func SearchReddit(ctx context.Context, client http.Client, query string) ([]Discussion, error)

SearchReddit searches Reddit for given query and returns list of discussions sorted by relevance.

See: https://www.reddit.com/dev/api#GET_search

Example
client := http.Client{}
query := "https://grugbrain.dev/"

opinions := ensure.MustReturn(SearchReddit(context.TODO(), client, query))

fmt.Println(opinions[0])
Output:
Reddit	https://reddit.com/r/hypeurls/comments/17k6i1l/the_grug_brained_developer_2022/	The Grug Brained Developer (2022)	https://grugbrain.dev/
Example (Unknown)
client := http.Client{}
query := "https://invalid.domain/query"

opinions := ensure.MustReturn(SearchReddit(context.TODO(), client, query))

fmt.Println(len(opinions))
Output:
0

func (Discussion) String

func (d Discussion) String() string

String returns string representation of discussion metadata.

type HackerNewsResponse

type HackerNewsResponse struct {
	Hits []struct {
		CreatedAt   time.Time `json:"created_at"`
		Title       string    `json:"title"`
		URL         string    `json:"url"`
		NumComments int       `json:"num_comments"`
		ObjectID    string    `json:"objectID"`
	} `json:"hits"`
}

HackerNewsResponse represents some interesting fields of response from HN Search API.

See: https://hn.algolia.com/api

type LemmyResponse added in v1.3.0

type LemmyResponse struct {
	Posts []struct {
		Post struct {
			ID   int    `json:"id"`
			Name string `json:"name"`
			URL  string `json:"url"`
		} `json:"post"`
		Counts struct {
			Comments int `json:"comments"`
		} `json:"counts"`
	} `json:"posts"`
}

LemmyResponse represents some interesting fields of response from Lemmy Search API.

See: https://join-lemmy.org/api/interfaces/SearchResponse.html

type RedditResponse added in v1.4.0

type RedditResponse struct {
	Data struct {
		Children []struct {
			Data struct {
				ID          string `json:"permalink"`
				Title       string `json:"title"`
				URL         string `json:"url"`
				NumComments int    `json:"num_comments"`
			} `json:"data"`
		} `json:"children"`
	} `json:"data"`
}

RedditResponse represents some interesting fields of response from Reddit API.

func (*RedditResponse) UnmarshalJSON added in v1.5.1

func (r *RedditResponse) UnmarshalJSON(b []byte) error

UnmarshalJSON deserialize inconsistent JSON responses to RedditResponse. Reddit returns empty object ("{}") when there are no search results.

Directories

Path Synopsis
Package ensure contains helpers for testable examples.
Package ensure contains helpers for testable examples.
Package http implements cancellable REST requests with custom User-Agent.
Package http implements cancellable REST requests with custom User-Agent.

Jump to

Keyboard shortcuts

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