PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms 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.
  4. package goduckgo
  5. import (
  6. "fmt"
  7. "net/url"
  8. "encoding/json"
  9. "net/http"
  10. "io/ioutil"
  11. "io"
  12. "os"
  13. )
  14. // Type Message is a structure containing all the information returned by
  15. // DDG for a query.
  16. type Message struct {
  17. Definition string
  18. DefinitionSource string
  19. Heading string
  20. AbstractText string
  21. Abstract string
  22. AbstractSource string
  23. Image string
  24. Type string
  25. AnswerType string
  26. Redirect string
  27. DefinitionURL string
  28. Answer string
  29. AbstractURL string
  30. Results Results
  31. RelatedTopics RelatedTopics
  32. }
  33. type Result struct {
  34. Result string
  35. FirstURL string
  36. Text string
  37. }
  38. // Method Show of struct Result writes Result to standard output
  39. func (result *Result) Show(prefix string) {
  40. result.Fshow(os.Stdout, prefix)
  41. }
  42. func (result *Result) Fshow(w io.Writer, prefix string) {
  43. fmt.Fprintln(w, prefix, "Result:", result.Result)
  44. fmt.Fprintln(w, prefix, "First URL:", result.FirstURL)
  45. fmt.Fprintln(w, prefix, "Text:", result.Text)
  46. }
  47. type Results []Result
  48. type RelatedTopics []RelatedTopic
  49. type RelatedTopic struct {
  50. Result string
  51. Icon Icon
  52. FirstURL string
  53. Text string
  54. }
  55. func (topic *RelatedTopic) Show(prefix string) {
  56. topic.Fshow(os.Stdout, prefix)
  57. }
  58. func (topic *RelatedTopic) Fshow(w io.Writer, prefix string) {
  59. fmt.Fprintln(w, prefix, "Result:", topic.Result)
  60. fmt.Fprintln(w, prefix, "Icon:")
  61. topic.Icon.Fshow(w, prefix + prefix)
  62. fmt.Fprintln(w, prefix, "FirstURL:", topic.FirstURL)
  63. fmt.Fprintln(w, prefix, "Text:", topic.Text)
  64. }
  65. type Icon struct {
  66. URL string
  67. Height interface{} // can be string or number ("16" or 16)
  68. Width interface{} // can be string or number ("16" or 16)
  69. }
  70. func (icon *Icon) Show(prefix string) {
  71. icon.Fshow(os.Stdout, prefix)
  72. }
  73. func (icon *Icon) Fshow(w io.Writer, prefix string) {
  74. fmt.Fprintln(w, prefix, "URL:", icon.URL)
  75. fmt.Fprintln(w, prefix, "Height:", icon.Height)
  76. fmt.Fprintln(w, prefix, "Width:", icon.Width)
  77. }
  78. func Query(query string) (*Message, error) {
  79. query_enc := url.QueryEscape(query)
  80. ddgurl := fmt.Sprintf("http://api.duckduckgo.com?q=%s&format=json&pretty=1", query_enc)
  81. resp, err := http.Get(ddgurl)
  82. if err != nil {
  83. return nil, err
  84. }
  85. body, err := ioutil.ReadAll(resp.Body)
  86. if err != nil {
  87. return nil, err
  88. }
  89. var message *Message = &Message{}
  90. if err = json.Unmarshal(body, message); err != nil {
  91. return nil, err
  92. }
  93. return message, nil
  94. }