Module: DuckGo
- Defined in:
- lib/duckgo.rb,
lib/duckgo/version.rb
Constant Summary collapse
- VERSION =
"1.2.1"
Instance Method Summary collapse
-
#extract_common(data) ⇒ Object
Extract common info.
-
#extract_extras(data, extras_data) ⇒ Object
Extract extra info based on an input extras_data.
-
#extract_infobox(data) ⇒ Object
Extract infobox from hash Official example : api.duckduckgo.com/?q=valley+forge+national+park&format=json&pretty=1.
-
#extract_results(data) ⇒ Object
Extract results from hash.
-
#find_extras(data) ⇒ Object
Return data describing the extra info fields.
-
#get(params, path = "/", domain = "https://api.duckduckgo.com") ⇒ Object
Do a raw api request based on paramaters.
-
#get_data(keywords, format = 0, skip_disambig = 1, no_redirect = 1, parse = true) ⇒ Object
Dump the useful data straight from DuckDuckGo’s API If format is XML, return it as a string even if parse is true.
-
#get_favicon(page) ⇒ Object
Get a website/page’s favicon through duckduckgo’s proxy.
-
#handle(keywords) ⇒ Object
Handle a common search automatically.
Instance Method Details
#extract_common(data) ⇒ Object
Extract common info
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/duckgo.rb', line 114 def extract_common(data) output = { "Heading" => data["Heading"], "Entity" => data["Entity"], "Type" => "", "Description" => data["AbstractText"], "Further Reading" => data["AbstractURL"], "Related" => [] } case data["Type"] when "A" type = "Article" when "C" type = "Category" when "D" type = "Disambiguation" when "E" type = "Exclusive" when "N" type = "Name" else type = "Nothing or unknown" end output["Type"] = type data["RelatedTopics"].each do |topic| unless topic["Text"].nil? output["Related"] << topic["Text"] end end output.delete_if do |key, value| value.empty? end return output end |
#extract_extras(data, extras_data) ⇒ Object
Extract extra info based on an input extras_data
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/duckgo.rb', line 93 def extract_extras(data, extras_data) extras = {} if extras_data["Infobox"] extras["Infobox"] = extract_infobox(data) end if extras_data["Results"] extras["Results"] = extract_results(data) end if extras_data["Answer"] extras["Answer"] = extras_data["Answer"] end if extras_data["Definition"] extras["Definition"] = extras_data["Definition"] end if extras_data["Bang Redirect"] extras["Bang Redirect"] = extras_data["Bang Redirect"] end return extras end |
#extract_infobox(data) ⇒ Object
Extract infobox from hash Official example : api.duckduckgo.com/?q=valley+forge+national+park&format=json&pretty=1
160 161 162 163 164 165 166 167 |
# File 'lib/duckgo.rb', line 160 def extract_infobox(data) output = {} tsums = data["Infobox"]["content"] tsums.each do |section| output[ section["label"] ] = section["value"] end return output end |
#extract_results(data) ⇒ Object
Extract results from hash
150 151 152 153 154 155 156 |
# File 'lib/duckgo.rb', line 150 def extract_results(data) output = {} data["Results"].each do |result| output[result["Text"]] = result["FirstURL"] end return output end |
#find_extras(data) ⇒ Object
Return data describing the extra info fields
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/duckgo.rb', line 61 def find_extras(data) extras = {} unless data["Infobox"].empty? extras["Infobox"] = { "Total" => data["Infobox"]["content"].length } end unless data["Results"].empty? extras["Results"] = { "Total" => data["Results"].length } end unless data["Answer"].empty? extras["Answer"] = { "Answer" => data["Answer"], "Type" => data["AnswerType"] } end unless data["Definition"].empty? extras["Definition"] = { "Definition" => data["Definition"], "Source" => data["DefinitionSource"], "URL" => data["DefinitionURL"] } end unless data["Redirect"].empty? extras["Bang Redirect"] = data["Redirect"] end return extras end |
#get(params, path = "/", domain = "https://api.duckduckgo.com") ⇒ Object
Do a raw api request based on paramaters
11 12 13 14 15 16 17 18 19 |
# File 'lib/duckgo.rb', line 11 def get(params, path="/", domain="https://api.duckduckgo.com") # domain can also just be plain duckduckgo.com if params.nil? endpoint = "#{domain}/#{path}" else endpoint = "#{domain}/#{path}?#{URI.encode_www_form(params)}" end body = open(endpoint).read return body end |
#get_data(keywords, format = 0, skip_disambig = 1, no_redirect = 1, parse = true) ⇒ Object
Dump the useful data straight from DuckDuckGo’s API If format is XML, return it as a string even if parse is true
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/duckgo.rb', line 23 def get_data(keywords, format=0, skip_disambig=1, no_redirect=1, parse=true) params = { "q" => keywords, "format" => ["json", "xml"][format], "skip_disambig" => [0, 1][skip_disambig], "no_redirect" => [0, 1][no_redirect] } keywords.split(" ").each do |word| if word.start_with?("!") puts "Warning: '!x word' syntax results in a bang redirect" end end body = get(params) if parse if format == 0 return JSON.parse(body) else return body end else return body end end |
#get_favicon(page) ⇒ Object
Get a website/page’s favicon through duckduckgo’s proxy
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/duckgo.rb', line 48 def get_favicon(page) favicon = get(nil, "/i/#{page}.ico") require 'digest' generic_resp = "ad4b0f606e0f8465bc4c4c170b37e1a3" if Digest::MD5.hexdigest(favicon) == generic_resp puts "Warning: response is 200-OK, but content is a generic response\nThis probably means the favicon is unreachable" return nil else return favicon end end |
#handle(keywords) ⇒ Object
Handle a common search automatically
170 171 172 173 174 175 176 177 |
# File 'lib/duckgo.rb', line 170 def handle(keywords) data = get_data(keywords) common = extract_common(data) extras_data = find_extras(data) extras = extract_extras(data, extras_data) result = common.merge(extras) puts result.to_yaml end |