ddg

package module
v0.0.0-...-b360a4f Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2012 License: ISC Imports: 8 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

ddg - A Go package for DuckDuckGo Zero-click API access

Install

go get github.com/whee/ddg

Use

package main

import (
	"fmt"
	"github.com/whee/ddg"
	"os"
)

func main() {
	for _, s := range os.Args[1:] {
		if r, err := ddg.ZeroClick(s); err == nil {
			fmt.Printf("%s: %s\n", s, r.Abstract)
		} else {
			fmt.Printf("Error looking up %s: %v\n", s, err)
		}
	}
}

See godoc documentation for more information.

Documentation

Overview

DuckDuckGo Zero-click API client. See https://duckduckgo.com/api.html for information about the API.

Example command-line program to show abstracts:

package main

import (
	"fmt"
	"github.com/whee/ddg"
	"os"
)

func main() {
	for _, s := range os.Args[1:] {
		if r, err := ddg.ZeroClick(s); err == nil {
			fmt.Printf("%s: %s\n", s, r.Abstract)
		} else {
			fmt.Printf("Error looking up %s: %v\n", s, err)
		}
	}
}

Index

Constants

View Source
const (
	Article        = "A"
	Disambiguation = "D"
	Category       = "C"
	Name           = "N"
	Exclusive      = "E"
	None           = ""
)
View Source
const (
	DDG_API_BASE_URL   = "api.duckduckgo.com"
	DDG_API_URL        = "http://api.duckduckgo.com/?q=%s&format=json"
	DDG_API_URL_SECURE = "https://api.duckduckgo.com/?q=%s&format=json"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CategoryType

type CategoryType string

type Client

type Client struct {
	// Secure specifies whether HTTPS is used.
	Secure bool
	// NoHtml will remove HTML from the response text
	NoHtml bool `parameter:"no_html"`
	// SkipDisambiguation will prevent Disambiguation type responses
	SkipDisambiguation bool `parameter:"skip_disambig"`
	// NoRedirect skips HTTP redirects (for !bang commands)
	NoRedirect bool `parameter:"no_redirect"`
	// BaseURL specifies where to send API requests. If zero-value,
	// "api.duckduckgo.com" is used.
	BaseURL string
}

A Client is a DDG Zero-click client.

func (*Client) ZeroClick

func (c *Client) ZeroClick(query string) (res Response, err error)

ZeroClick queries DuckDuckGo's zero-click API for the specified query and returns the Response.

type Icon

type Icon struct {
	URL    string // URL of icon
	Height int    `json:"-"` // height of icon (px)
	Width  int    `json:"-"` // width of icon (px)

	// The height and width can be "" (string; we treat as 0) or an int. Unmarshal here, then populate the above two.
	RawHeight interface{} `json:"Height"`
	RawWidth  interface{} `json:"Width"`
}

type Response

type Response struct {
	Abstract       string // topic summary (can contain HTML, e.g. italics)
	AbstractText   string // topic summary (with no HTML)
	AbstractSource string // name of Abstract source
	AbstractURL    string // deep link to expanded topic page in AbstractSource
	Image          string // link to image that goes with Abstract
	Heading        string // name of topic that goes with Abstract

	Answer     string // instant answer
	AnswerType string // type of Answer, e.g. calc, color, digest, info, ip, iploc, phone, pw, rand, regexp, unicode, upc, or zip (see goodies & tech pages for examples).

	Definition       string // dictionary definition (may differ from Abstract)
	DefinitionSource string // name of Definition source
	DefinitionURL    string // deep link to expanded definition page in DefinitionSource

	RelatedTopics         []Result         // array of internal links to related topics associated with Abstract
	RelatedTopicsSections []SectionResults // disambiguation types will populate this

	Results []Result // array of external links associated with Abstract

	Type CategoryType // response category, i.e. A (article), D (disambiguation), C (category), N (name), E (exclusive), or nothing.

	Redirect string // !bang redirect URL
}

Response represents the response from a zero-click API request via the ZeroClick function.

func Query

func Query(query string) (*Response, error)

func ZeroClick

func ZeroClick(query string) (res Response, err error)

ZeroClick queries DuckDuckGo's zero-click API for the specified query and returns the Response. This helper function uses a zero-value Client.

type Result

type Result struct {
	Result   string // HTML link(s) to external site(s)
	FirstURL string // first URL in Result
	Icon     Icon   // icon associated with FirstURL
	Text     string // text from FirstURL
}

type SectionResults

type SectionResults struct {
	Name   string
	Topics []Result
}

SectionResults are results grouped by a topic name.

Jump to

Keyboard shortcuts

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