PageRenderTime 1ms CodeModel.GetById 608ms app.highlight 9ms RepoModel.GetById 172ms app.codeStats 0ms

/duckduck.go

http://github.com/ajanicij/goduckgo
Go | 111 lines | 88 code | 17 blank | 6 comment | 6 complexity | e150d5cf0f1e7b95ca2d026d695b2227 MD5 | raw file
  1// Package goduckgo provides the functionality for using
  2// DuckDuckGo API. For the description of the API, visit
  3// http://duckduckgo.com/api.html.
  4package goduckgo
  5
  6import (
  7	"fmt"
  8	"net/url"
  9	"encoding/json"
 10	"net/http"
 11	"io/ioutil"
 12	"io"
 13	"os"
 14)
 15
 16// Type Message is a structure containing all the information returned by
 17// DDG for a query.
 18type Message struct {
 19	Definition string
 20	DefinitionSource string
 21	Heading string
 22	AbstractText string
 23	Abstract string
 24	AbstractSource string
 25	Image string
 26	Type string
 27	AnswerType string
 28	Redirect string
 29	DefinitionURL string
 30	Answer string
 31	AbstractURL string
 32	Results Results
 33	RelatedTopics RelatedTopics
 34}
 35
 36type Result struct {
 37	Result string
 38	FirstURL string
 39	Text string
 40}
 41
 42// Method Show of struct Result writes Result to standard output
 43func (result *Result) Show(prefix string) {
 44	result.Fshow(os.Stdout, prefix)
 45}
 46
 47func (result *Result) Fshow(w io.Writer, prefix string) {
 48	fmt.Fprintln(w, prefix, "Result:", result.Result)
 49	fmt.Fprintln(w, prefix, "First URL:", result.FirstURL)
 50	fmt.Fprintln(w, prefix, "Text:", result.Text)
 51}
 52
 53
 54type Results []Result
 55
 56type RelatedTopics []RelatedTopic
 57
 58type RelatedTopic struct {
 59	Result string
 60	Icon Icon
 61	FirstURL string
 62	Text string
 63}
 64
 65func (topic *RelatedTopic) Show(prefix string) {
 66	topic.Fshow(os.Stdout, prefix)
 67}
 68
 69func (topic *RelatedTopic) Fshow(w io.Writer, prefix string) {
 70	fmt.Fprintln(w, prefix, "Result:", topic.Result)
 71	fmt.Fprintln(w, prefix, "Icon:")
 72	topic.Icon.Fshow(w, prefix + prefix)
 73	fmt.Fprintln(w, prefix, "FirstURL:", topic.FirstURL)
 74	fmt.Fprintln(w, prefix, "Text:", topic.Text)
 75}
 76
 77type Icon struct {
 78	URL string
 79	Height interface{} // can be string or number ("16" or 16)
 80	Width interface{} // can be string or number ("16" or 16)
 81}
 82
 83func (icon *Icon) Show(prefix string) {
 84	icon.Fshow(os.Stdout, prefix)
 85}
 86
 87func (icon *Icon) Fshow(w io.Writer, prefix string) {
 88	fmt.Fprintln(w, prefix, "URL:", icon.URL)
 89	fmt.Fprintln(w, prefix, "Height:", icon.Height)
 90	fmt.Fprintln(w, prefix, "Width:", icon.Width)
 91}
 92
 93func Query(query string) (*Message, error) {
 94	query_enc := url.QueryEscape(query)
 95	ddgurl := fmt.Sprintf("http://api.duckduckgo.com?q=%s&format=json&pretty=1", query_enc)
 96	resp, err := http.Get(ddgurl)
 97	if err != nil {
 98		return nil, err
 99	}
100	body, err := ioutil.ReadAll(resp.Body)
101	if err != nil {
102		return nil, err
103	}
104	var message *Message = &Message{}
105
106	if err = json.Unmarshal(body, message); err != nil {
107		return nil, err
108	}
109	return message, nil
110}
111