duckgo 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d0d934faf234d88f43a383ecfa3e20dd1aa1e58f
4
+ data.tar.gz: 7314de47148059cd100b1b7ac61121213a490757
5
+ SHA512:
6
+ metadata.gz: c81412466434d637c53743ca276415f49c0c4b0a6a4c55b0ae601fa55d7df053df4bae1a62827824138fdc6854f9719723ffc7e0f286aefd77b8f49ac5e53ac8
7
+ data.tar.gz: 53ca76ff164324590185fd019fd92771ba064cbf03f46031bda5164037bdaea1b7ac1be2775fc7e600acc881ab841126a07f59b4d8fbc1c210ca37ecdbf2d79f
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/.rake_tasks~ ADDED
@@ -0,0 +1,6 @@
1
+ build
2
+ clean
3
+ clobber
4
+ install
5
+ install:local
6
+ release[remote]
@@ -0,0 +1,11 @@
1
+ {
2
+ "version": "0.2.0",
3
+ "configurations": [
4
+ {
5
+ "name": "Debug Ruby",
6
+ "type": "Ruby",
7
+ "request": "launch",
8
+ "program": "${file}"
9
+ }
10
+ ]
11
+ }
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2016 Daniel E
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # DuckGo - API wrapper for DuckDuckGo
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/duckgo.svg)](https://badge.fury.io/rb/duckgo)
4
+
5
+ ## What it does
6
+
7
+ DuckGo is a RubyGem library and command line tool for searching with DuckDuckGo.
8
+ By default, it puts out only the relevant information, but when told, it will dump
9
+ all the data you could ask for. This tool aims to give access to most of the
10
+ features from the official API.
11
+
12
+ ## Installation
13
+
14
+ Install with RubyGems:
15
+
16
+ + `sudo gem install duckgo`
17
+
18
+ Then include this in your scripts:
19
+
20
+ ```ruby
21
+ require 'duckgo'
22
+ include DuckGo
23
+
24
+ # Your stuff here
25
+ ```
26
+
27
+
28
+ ## API and Usage
29
+
30
+ Check out the [documentation](https://github.com/wlib/duckgo/tree/master/docs)
31
+ ## Contribute
32
+
33
+ If you use and enjoy DuckGo...
34
+
35
+ [Fork the project](https://github.com/wlib/duckgo/fork)
36
+
37
+ ## [MIT License](https://github.com/wlib/duckgo/blob/master/LICENSE)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/duckgo ADDED
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ # DuckGo is a RubyGem library and command line tool for searching with DuckDuckGo.
3
+ # By default, it puts out only the relevant information, but when told, it will dump
4
+ # all the data you could ask for. This tool aims to give access to most of the
5
+ # features from the official API.
6
+ # Daniel Ethridge
7
+
8
+ require "duckgo"
9
+ include DuckGo
10
+ require "duckgo/version"
11
+
12
+ # If there are no arguments...
13
+ if ARGV.length == 0
14
+ puts "Hmm..."
15
+ exit
16
+ end
17
+
18
+ help = """DuckGo lets you search duckduckgo.com for instant answers
19
+
20
+ duckgo [--help | -h] : Display this help
21
+ duckgo [--version | -V] : Display version
22
+ duckgo something searchable : Auto-handle the search
23
+ duckgo \!gem duckgo : Use !bang syntax to search a specific site
24
+
25
+ View docs and API on https://github.com/wlib/duckgo
26
+ Made by Daniel Ethridge | git.io/de
27
+ """
28
+
29
+ # Argument "parser" if you want to call it that
30
+ case ARGV[0]
31
+ when "--help", "-h"
32
+ puts help
33
+ exit
34
+ when "--version", "-V"
35
+ puts "DuckGo v#{VERSION}"
36
+ exit
37
+ else
38
+ handle(ARGV[0..-1].join(" "))
39
+ exit
40
+ end
data/bin/setup ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
data/docs/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Basic API Usage and Quick Start
2
+
3
+ ## Example
4
+
5
+ Here's the output of a topic summary request for ["unix"](http://api.duckduckgo.com/?q=unix&o=json&pretty=1), there is a huge amount of extra noise.
6
+
7
+ Now with automatic relevant data handling with `handle()`, we can extract the information
8
+ we want. Here's how:
9
+ ```Ruby
10
+ require 'yaml'
11
+ require 'duckgo'
12
+ include DuckGo
13
+
14
+ result = handle("unix")
15
+ puts result.to_yaml
16
+ ```
17
+
18
+ And this is the *now filtered* result:
19
+
20
+ ```YAML
21
+ ---
22
+ Heading: Unix
23
+ Entity: os
24
+ Type: Article
25
+ Description: Unix is a family of multitasking, multiuser computer operating systems
26
+ that derive from the original AT&T Unix, developed starting in the 1970s at the
27
+ Bell Labs research center by Ken Thompson, Dennis Ritchie, and others.
28
+ Further Reading: https://en.wikipedia.org/wiki/Unix
29
+ Related:
30
+ - Unix Category
31
+ - Market share of operating systems - The usage share of operating systems is the
32
+ [...]
33
+
34
+ Infobox:
35
+ Company / developer: Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy,
36
+ and Joe Ossanna at Bell Labs
37
+ Written in: C and assembly language
38
+ [...]
39
+
40
+ Results:
41
+ Official site: http://unix.org
42
+ ```
@@ -0,0 +1,93 @@
1
+ # Getting Data
2
+
3
+ ## get()
4
+
5
+ `get()` is the function used to recieve data from any page with any paramaters on any site,
6
+ but by default knows to just call duckduckgo's api with a paramater hash as the first argument.
7
+
8
+ ### Arguments
9
+
10
+ `get(params, path="/", domain="https://api.duckduckgo.com")`
11
+
12
+ + `params`
13
+ - A hash with query paramaters
14
+ + `path`
15
+ - The endpoint path
16
+ + `domain`
17
+ - The domain of the website called
18
+
19
+ ### Usage
20
+
21
+ ```Ruby
22
+ duckduckgo = {
23
+ "q" => "search string",
24
+ "format" => "json"
25
+ }
26
+
27
+ wikipedia = {
28
+ "action" => "opensearch",
29
+ "search" => "wiki"
30
+ }
31
+
32
+ puts get(duckduckgo) #=> "{"DefinitionSource":..."
33
+ puts get(wikipedia, "/w/api.php", "https://en.wikipedia.org") #=> "["wiki",["Wiki","Wikipedia","Wikimedia..."
34
+ ```
35
+
36
+ ## get_data()
37
+
38
+ This is a wrapper around `get()` to recieve duckduckgo's instant answers data.
39
+
40
+ ### Arguments
41
+
42
+ `get_data(keywords, format=0, skip_disambig=1, no_redirect=1, parse=true)`
43
+
44
+ + `keywords`
45
+ - The keywords passed into the search
46
+ + `format`
47
+ - The returned format
48
+ - 0 : json (default)
49
+ - 1 : xml
50
+ + `skip_disambig`
51
+ - Whether or not to skip a disambiguation result
52
+ - 0 : don't skip
53
+ - 1 : skip (default)
54
+ + `no_redirect`
55
+ - If this is a [!bang redirect](https://duckduckgo.com/bang), decide if we should follow the redirect
56
+ - 0 : follow redirect
57
+ - 1 : don't redirect (default)
58
+ + `parse`
59
+ - Return a parsed hash instead of a string, only parses if format is json
60
+ - true : return a hash (default)
61
+ - false : return a string
62
+
63
+ ### Usage
64
+
65
+ ```Ruby
66
+ require 'yaml'
67
+
68
+ data = get_data("something")
69
+ puts data.to_yaml
70
+ #=> "---
71
+ # DefinitionSource: ....
72
+ ```
73
+
74
+ ## get_favicon()
75
+
76
+ Another wrapper that just gets a website's favicon
77
+
78
+ ### Arguments
79
+
80
+ `get_favicon(page)`
81
+
82
+ + `page`
83
+ - The webpage or site domain to get the favicon from
84
+
85
+ ### Usage
86
+
87
+ ```Ruby
88
+ site = "google.com"
89
+ file = File.open("#{site}-favicon.ico", "w")
90
+ favicon = get_favicon(site)
91
+ file.write(favicon)
92
+ file.close
93
+ ```
data/duckgo.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'duckgo/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "duckgo"
8
+ gem.version = DuckGo::VERSION
9
+ gem.authors = ["Daniel Ethridge"]
10
+ gem.email = ["danielethridge@icloud.com"]
11
+ gem.license = 'MIT'
12
+
13
+ gem.summary = %q{DuckGo is a RubyGem library and command line tool for searching with DuckDuckGo.
14
+ By default, it puts out only the relevant information, but when told, it will dump
15
+ all the data you could ask for. This tool aims to give access to most of the
16
+ features from the official API.}
17
+ gem.homepage = "https://github.com/wlib/duckgo"
18
+
19
+ gem.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+ gem.bindir = "bin"
23
+ gem.executables = ["duckgo"]
24
+ gem.require_paths = ["lib"]
25
+
26
+ gem.add_development_dependency "bundler", "~> 1.13"
27
+ gem.add_development_dependency "rake", "~> 10.0"
28
+ end
data/lib/duckgo.rb ADDED
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env ruby
2
+ # Daniel Ethridge
3
+
4
+ require 'uri'
5
+ require 'open-uri'
6
+ require 'json'
7
+ require 'yaml'
8
+
9
+ module DuckGo
10
+ # Do a raw api request based on paramaters
11
+ def get(params, path="/", domain="https://api.duckduckgo.com") # domain can also just be plain duckduckgo.com
12
+ if params.nil?
13
+ endpoint = "#{domain}/#{path}"
14
+ else
15
+ endpoint = "#{domain}/#{path}?#{URI.encode_www_form(params)}"
16
+ end
17
+ body = open(endpoint).read
18
+ return body
19
+ end
20
+
21
+ # Dump the useful data straight from DuckDuckGo's API
22
+ # If format is XML, return it as a string even if parse is true
23
+ def get_data(keywords, format=0, skip_disambig=1, no_redirect=1, parse=true)
24
+ params = {
25
+ "q" => keywords,
26
+ "format" => ["json", "xml"][format],
27
+ "skip_disambig" => [0, 1][skip_disambig],
28
+ "no_redirect" => [0, 1][no_redirect]
29
+ }
30
+ keywords.split(" ").each do |word|
31
+ if word.start_with?("!")
32
+ puts "Warning: '!x word' syntax results in a bang redirect"
33
+ end
34
+ end
35
+ body = get(params)
36
+ if parse
37
+ if format == 0
38
+ return JSON.parse(body)
39
+ else
40
+ return body
41
+ end
42
+ else
43
+ return body
44
+ end
45
+ end
46
+
47
+ # Get a website/page's favicon through duckduckgo's proxy
48
+ def get_favicon(page)
49
+ favicon = get(nil, "/i/#{page}.ico")
50
+ if favicon == "GIF89a\x01\x00\x01\x00\x80\x01\x00\x00\x00\x00\xFF\xFF\xFF!\xF9\x04\x01\x00\x00\x01\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x02L\x01\x00;"
51
+ puts "Response is 200-OK, but content is a generic response"
52
+ return nil
53
+ else
54
+ return favicon
55
+ end
56
+ end
57
+
58
+ # Return data describing the extra info fields
59
+ def find_extras(data)
60
+ extras = {}
61
+ unless data["Infobox"].empty?
62
+ extras["Infobox"] = {
63
+ "Total" => data["Infobox"]["content"].length
64
+ }
65
+ end
66
+ unless data["Results"].empty?
67
+ extras["Results"] = {
68
+ "Total" => data["Results"].length
69
+ }
70
+ end
71
+ unless data["Answer"].empty?
72
+ extras["Answer"] = {
73
+ "Answer" => data["Answer"],
74
+ "Type" => data["AnswerType"]
75
+ }
76
+ end
77
+ unless data["Definition"].empty?
78
+ extras["Definition"] = {
79
+ "Definition" => data["Definition"],
80
+ "Source" => data["DefinitionSource"],
81
+ "URL" => data["DefinitionURL"]
82
+ }
83
+ end
84
+ unless data["Redirect"].empty?
85
+ extras["Bang Redirect"] = data["Redirect"]
86
+ end
87
+ return extras
88
+ end
89
+
90
+ # Extract extra info based on an input extras_data
91
+ def extract_extras(data, extras_data)
92
+ extras = {}
93
+ if extras_data["Infobox"]
94
+ extras["Infobox"] = extract_infobox(data)
95
+ end
96
+ if extras_data["Results"]
97
+ extras["Results"] = extract_results(data)
98
+ end
99
+ if extras_data["Answer"]
100
+ extras["Answer"] = extras_data["Answer"]
101
+ end
102
+ if extras_data["Definition"]
103
+ extras["Definition"] = extras_data["Definition"]
104
+ end
105
+ if extras_data["Bang Redirect"]
106
+ extras["Bang Redirect"] = extras_data["Bang Redirect"]
107
+ end
108
+ return extras
109
+ end
110
+
111
+ # Extract common info
112
+ def extract_common(data)
113
+ output = {
114
+ "Heading" => data["Heading"],
115
+ "Entity" => data["Entity"],
116
+ "Type" => "",
117
+ "Description" => data["AbstractText"],
118
+ "Further Reading" => data["AbstractURL"],
119
+ "Related" => []
120
+ }
121
+ case data["Type"]
122
+ when "A"
123
+ type = "Article"
124
+ when "C"
125
+ type = "Category"
126
+ when "D"
127
+ type = "Disambiguation"
128
+ when "E"
129
+ type = "Exclusive"
130
+ when "N"
131
+ type = "Name"
132
+ else
133
+ type = "Nothing or unknown"
134
+ end
135
+ output["Type"] = type
136
+ data["RelatedTopics"].each do |topic|
137
+ unless topic["Text"].nil?
138
+ output["Related"] << topic["Text"]
139
+ end
140
+ end
141
+ output.delete_if do |key, value|
142
+ value.empty?
143
+ end
144
+ return output
145
+ end
146
+
147
+ # Extract results from hash
148
+ def extract_results(data)
149
+ output = {}
150
+ data["Results"].each do |result|
151
+ output[result["Text"]] = result["FirstURL"]
152
+ end
153
+ return output
154
+ end
155
+
156
+ # Extract infobox from hash
157
+ # Official example : http://api.duckduckgo.com/?q=valley+forge+national+park&format=json&pretty=1
158
+ def extract_infobox(data)
159
+ output = {}
160
+ tsums = data["Infobox"]["content"]
161
+ tsums.each do |section|
162
+ output[ section["label"] ] = section["value"]
163
+ end
164
+ return output
165
+ end
166
+
167
+ # Handle a common search automatically
168
+ def handle(keywords)
169
+ data = get_data(keywords)
170
+ common = extract_common(data)
171
+ extras_data = find_extras(data)
172
+ extras = extract_extras(data, extras_data)
173
+ result = common.merge(extras)
174
+ puts result.to_yaml
175
+ end
176
+ end
@@ -0,0 +1,3 @@
1
+ module DuckGo
2
+ VERSION = "1.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duckgo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Ethridge
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description:
42
+ email:
43
+ - danielethridge@icloud.com
44
+ executables:
45
+ - duckgo
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rake_tasks~"
51
+ - ".vscode/launch.json"
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/duckgo
56
+ - bin/setup
57
+ - docs/README.md
58
+ - docs/getting_data.md
59
+ - duckgo.gemspec
60
+ - lib/duckgo.rb
61
+ - lib/duckgo/version.rb
62
+ homepage: https://github.com/wlib/duckgo
63
+ licenses:
64
+ - MIT
65
+ metadata: {}
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 2.5.1
83
+ signing_key:
84
+ specification_version: 4
85
+ summary: DuckGo is a RubyGem library and command line tool for searching with DuckDuckGo.
86
+ By default, it puts out only the relevant information, but when told, it will dump
87
+ all the data you could ask for. This tool aims to give access to most of the features
88
+ from the official API.
89
+ test_files: []