wik 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8010372cb4c94c11e2e50fab27a2b58ac5b79fd6
4
+ data.tar.gz: 38bee032bfda2048e3dce6f3fa9459a0e14ed4b9
5
+ SHA512:
6
+ metadata.gz: 2b775f5234a53db595ade5f00ffce3f939d0f2954f9ec192b8ee3656f3bd63688b87325d1e1f959efd23d721621336fc2a00f5f7dbd0eba98e046d9ea05883b8
7
+ data.tar.gz: c315b58dd5aa29880cd85354825a93f8e735119f195ce595a051a13b1aea07469d5ee53aad7908599323f013c59ae60efa3929322148d58445ef3d90346dd16b
@@ -0,0 +1 @@
1
+ pkg
@@ -0,0 +1,6 @@
1
+ build
2
+ clean
3
+ clobber
4
+ install
5
+ install:local
6
+ release[remote]
data/API.md ADDED
@@ -0,0 +1,123 @@
1
+ # Wik's Ruby API
2
+
3
+ ## Getting Started
4
+
5
+ ### Install
6
+ Getting started is simple, first install the gem:
7
+
8
+ + `sudo gem install wik`
9
+
10
+ ### Trying it out
11
+ Now that we have Wik installed, lets play around with it in `irb`:
12
+
13
+ ```
14
+ daniel@pancake:~/wik$ irb
15
+ irb(main):001:0> require "wik" # Get the modules from the wik gem
16
+ => true
17
+ irb(main):002:0> include Wik # Load in the module "Wik"
18
+ => Object
19
+ irb(main):003:0> find("api", 2) # Query Wikipedia for the first two results under "api"
20
+ Total hits : 11280
21
+ Displaying 2 results:
22
+
23
+ 'Application programming interface'
24
+
25
+ 'API gravity'
26
+ => {"batchcomplete"=>"", "continue"=>{"sroffset"=>2, "continue"=>"-||"}, "query"=>{"searchinfo"=>...
27
+ ```
28
+
29
+ Great, we just ran a simple find function for "api", we limited the output, and the function
30
+ returned to us a hash with all the raw data we could ever want, straight from Wikipedia.
31
+
32
+ ### Simple scripting
33
+ Now lets see how we can script our own little search tool.
34
+
35
+ ```
36
+ # wiki_search.rb
37
+
38
+ # Load all the good stuff
39
+ require "wik"
40
+ include Wik
41
+
42
+ # Ask the user what to do
43
+ puts "Should we [1] Find based on topic, [2] Search for a page, [3] Describe a page, or [4] View by page title?"
44
+ print "[1/2/3] > "
45
+ choice = gets.chomp.to_i
46
+
47
+ # Run the magic
48
+ case choice
49
+ when 1
50
+ print "Find what? > "
51
+ find(gets.chomp)
52
+ when 2
53
+ print "Search what? > "
54
+ search(gets.chomp)
55
+ when 3
56
+ print "View what? > "
57
+ view(gets.chomp)
58
+ else
59
+ puts "Did't get that..."
60
+ exit
61
+ end
62
+ ```
63
+
64
+ Nice, now we have a simple search tool we can run on our own.
65
+
66
+ ## search(phrase, limit=5, description=true, display=true)
67
+
68
+ ### Usage
69
+ Searching Wikipedia is done easily with `search()`, for example, running
70
+ `search("page")` will search for "page" and return a hash with raw data,
71
+ but also print the useful data to console.
72
+
73
+ ### Arguments
74
+ + `phrase`
75
+ - A string to search for, don't worry, whitespace is delt with automatically
76
+ + `limit`
77
+ - An integer telling the maximum number of results allowed back, default 5
78
+ + `description`
79
+ - A boolean deciding if a short description should be printed out or not, default true
80
+ + `display`
81
+ - A boolean that decides if any output should be put to the console, default true
82
+
83
+ ### Output
84
+
85
+ ```
86
+ irb> search("ruby!", 1)
87
+ Displaying 1 results:
88
+
89
+ 'Ruby (programming language)' : Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro "Matz" Matsumoto in Japan.
90
+ => ["ruby!", ["Ruby (programming language)"], ["Ruby is a dynamic, reflective, object-oriented, general-purpose programming language. It was designed and developed in the mid-1990s by Yukihiro \"Matz\" Matsumoto in Japan."], ["https://en.wikipedia.org/wiki/Ruby_(programming_language)"]]
91
+ ```
92
+
93
+ ## find(phrase, limit=15, snippet=false, display=true)
94
+
95
+ ### Usage
96
+
97
+ ### Arguments
98
+
99
+ ### Output
100
+
101
+ ## info(titles=nil, ids=nil, display=true)
102
+
103
+ ### Usage
104
+
105
+ ### Arguments
106
+
107
+ ### Output
108
+
109
+ ## view(title=nil, id=nil)
110
+
111
+ ### Usage
112
+
113
+ ### Arguments
114
+
115
+ ### Output
116
+
117
+ ## describe(title=nil, id=nil)
118
+
119
+ ### Usage
120
+
121
+ ### Arguments
122
+
123
+ ### Output
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.
@@ -0,0 +1,43 @@
1
+ # Wik - Quick and easy Wikipedia searches
2
+
3
+ ## Wik makes it quick
4
+
5
+ With Wik, you have access to all of Wikipedia, in your terminal.
6
+ If you have to know something **NOW**, it's as simple as....
7
+
8
+ ▶ `wik wiki`
9
+
10
+ Control is important, so a common action like searching is available with `wik -s keyword`.
11
+ Once you know the name of a page, type `wik -v "page name"` for a full page view.
12
+ To get just a summed up description of a page, run `wik -d "page name"`.
13
+ If you ever just want to find a list of entries relating to a keyword, `wik -f topic` will do just that.
14
+
15
+ ## Installation
16
+
17
+ Pretty easy, you only need to have Ruby installed.
18
+
19
+ + `sudo gem install wik`
20
+
21
+ ## CLI Usage
22
+
23
+ `wik hello, world!` - Automatically handle everything
24
+
25
+ `wik -s keyword` - Search Wikipedia
26
+
27
+ `wik -f topic` - Find a list of available pages about a topic
28
+
29
+ `wik -v page name` - View "page name"
30
+
31
+ `wik -d page name` - Get a short description of "page name"
32
+
33
+ ## [API](https://github.com/wlib/wik/blob/master/API.md)
34
+
35
+ Wik has a comfortably simple API, have a look at [API.md](https://github.com/wlib/wik/blob/master/API.md)
36
+
37
+ ## Contribute
38
+
39
+ Help me out if you have nothing better to do:
40
+
41
+ [Fork the project](https://github.com/wlib/wik/fork)
42
+
43
+ ## [MIT License](https://github.com/wlib/wik/blob/master/LICENSE)
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -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/bin/wik ADDED
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+ # Wik - Quick and easy Wikipedia searches
3
+ # Daniel Ethridge
4
+
5
+ require "wik"
6
+ include Wik
7
+ require "wik/version"
8
+
9
+ # If there are no arguments, display a message to help you remember what the program does
10
+ if ARGV.length == 0
11
+ puts "Try to tell me what you need...`wik wiki`"
12
+ exit
13
+ end
14
+
15
+ help = """Wik is a quick and easy Wikipedia search tool for the command line, try it out:
16
+ hello, world! - Automatically handle everything
17
+ -s keyword - Search Wikipedia
18
+ -f topic - Find a list of available pages about a topic
19
+ -v api - View API
20
+ -d ruby - Get a short description of ruby
21
+ """
22
+
23
+ # Argument "parser" if you want to call it that
24
+ case ARGV[0]
25
+ when "--help", "-h"
26
+ puts help
27
+ exit
28
+ when "--version", "-V"
29
+ puts "Wik v#{VERSION}"
30
+ exit
31
+ when "--search", "-s"
32
+ search(ARGV[1..-1].join(" "))
33
+ exit
34
+ when "--find", "-f"
35
+ find(ARGV[1..-1].join(" "))
36
+ exit
37
+ when "--view", "-v"
38
+ view(ARGV[1..-1].join(" "))
39
+ exit
40
+ when "--describe", "-d"
41
+ describe(ARGV[1..-1].join(" "))
42
+ exit
43
+ else
44
+ handle(ARGV[0..-1].join(" "))
45
+ exit
46
+ end
@@ -0,0 +1,149 @@
1
+ #!/usr/bin/env ruby
2
+ # Daniel Ethridge
3
+
4
+ require 'uri'
5
+ require 'open-uri'
6
+ require 'json'
7
+ require 'yaml'
8
+ require 'io/console'
9
+
10
+ module Wik
11
+ # Do a get request and, by default, return the response as parsed json
12
+ def get(endpoint, parse=true)
13
+ ua = "Wik-RubyGem (https://github.com/wlib/wik; danielethridge@icloud.com) Command Line Wikipedia"
14
+ body = open(endpoint, "User-Agent" => ua).read
15
+ if parse
16
+ return JSON.parse(body)
17
+ else
18
+ return body
19
+ end
20
+ end
21
+
22
+ # Do a search by phrase, returns a hash with full search data, but first prints the parsed search data
23
+ def find(phrase, limit=15, snippet=false, display=true)
24
+ search = phrase.split.join("_")
25
+ if snippet
26
+ endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=#{search}&srlimit=#{limit}"
27
+ else
28
+ endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&srsearch=#{search}&srlimit=#{limit}&srprop"
29
+ end
30
+ hash = get(endpoint)
31
+ info = hash["query"]["searchinfo"]
32
+ results = hash["query"]["search"]
33
+ if display
34
+ puts "Total hits : #{ info["totalhits"] }"
35
+ if info["suggestion"]
36
+ puts "Suggestion : #{ info["suggestion"] }"
37
+ end
38
+ puts "Displaying #{results.length} results:"
39
+ results.each do |result|
40
+ # https://stackoverflow.com/a/1732454
41
+ if snippet
42
+ snip = result["snippet"].gsub( /<.*?>/, "" ).gsub( /&\w+;/, "" ).split.join(" ")
43
+ puts "\n'#{result["title"]}' : #{snip}..."
44
+ else
45
+ puts "\n'#{result["title"]}'"
46
+ end
47
+ end
48
+ end
49
+ return hash
50
+ end
51
+
52
+ # Open Search is a different way to search Wikipedia
53
+ # It returns descriptions automatically and has a more consistent typo-correct
54
+ def search(phrase, limit=5, description=true, display=true)
55
+ search = phrase.split.join("_")
56
+ endpoint = "https://en.wikipedia.org/w/api.php?action=opensearch&format=json&search=#{search}&limit=#{limit}"
57
+ hash = get(endpoint)
58
+ results = hash[1]
59
+ descriptions = hash[2]
60
+ if display
61
+ puts "Displaying #{results.length} results:"
62
+ for i in 0...results.length
63
+ if description
64
+ if ! descriptions[i].empty?
65
+ puts "\n'#{results[i]}' : #{descriptions[i]}"
66
+ else
67
+ puts "\n'#{results[i]}' : No Immediate Description Available. Try `wik -d #{results[i]}`"
68
+ end
69
+ else
70
+ puts "\n'#{results[i]}'"
71
+ end
72
+ end
73
+ end
74
+ return hash
75
+ end
76
+
77
+ # Get info for titles
78
+ def info(titles=nil, ids=nil, display=true)
79
+ if titles
80
+ if titles.is_a?(Array)
81
+ encoded = titles.join("|").split.join("_")
82
+ elsif titles.is_a?(String)
83
+ encoded = titles
84
+ else
85
+ puts "Titles should be a string or array"
86
+ return false
87
+ end
88
+ endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info&inprop=url&titles=#{encoded}&redirects"
89
+ elsif ids
90
+ if ids.is_a?(Array)
91
+ encoded = ids.join("|").split.join("_")
92
+ elsif ids.is_a?(String)
93
+ encoded = ids
94
+ else
95
+ puts "ID's should be a string or array"
96
+ return false
97
+ end
98
+ endpoint = "https://en.wikipedia.org/w/api.php?action=query&format=json&prop=info&inprop=url&pageids=#{encoded}&redirects"
99
+ end
100
+ hash = get(endpoint)
101
+ pages = hash["query"]["pages"]
102
+ redirects = hash["query"]["redirects"]
103
+ if display
104
+ if redirects
105
+ redirects.each do |redirect|
106
+ puts "Redirected from '#{redirect["from"]}' to '#{redirect["to"]}'"
107
+ end
108
+ end
109
+ pages.keys.each do |id|
110
+ page = pages[id]
111
+ if page["missing"]
112
+ puts "The page '#{page["title"]}' is missing"
113
+ else
114
+ puts "The page '#{page["title"]}' has the ID: #{page["pageid"]}, and is written in #{page["pagelanguage"]}"
115
+ end
116
+ end
117
+ end
118
+ return hash
119
+ end
120
+
121
+ # Get the entire page of an entry
122
+ def view(title=nil, id=nil)
123
+ if title
124
+ encoded = title.split.join("_")
125
+ endpoint = "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=wikitext&page=#{encoded}&redirects"
126
+ elsif id
127
+ encoded = title.split.join("_")
128
+ endpoint = "https://en.wikipedia.org/w/api.php?action=parse&format=json&prop=wikitext&pageid=#{encoded}"
129
+ end
130
+ hash = get(endpoint)
131
+ page = hash["parse"]["wikitext"].to_s.gsub( '\n', "\n" )
132
+ tmp = Tempfile.new("wik-#{encoded}-")
133
+ tmp.puts page
134
+ system "less #{tmp.path}"
135
+ tmp.close
136
+ tmp.unlink
137
+ return hash
138
+ end
139
+
140
+ # Just give a description of an entry
141
+ def describe(title=nil, id=nil)
142
+ puts "In development..."
143
+ end
144
+
145
+ # Automatically handle the process of searching, redirecting, and viewing
146
+ def handle(input)
147
+ search(input)
148
+ end
149
+ end
@@ -0,0 +1,3 @@
1
+ module Wik
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wik/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "wik"
8
+ gem.version = Wik::VERSION
9
+ gem.authors = ["Daniel Ethridge"]
10
+ gem.email = ["danielethridge@icloud.com"]
11
+ gem.license = 'MIT'
12
+
13
+ gem.summary = %q{Wik - Quick and easy Wikipedia searches}
14
+ gem.homepage = "https://github.com/wlib/wik"
15
+
16
+ gem.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ gem.bindir = "bin"
20
+ gem.executables = ["wik"]
21
+ gem.require_paths = ["lib"]
22
+
23
+ gem.add_development_dependency "bundler", "~> 1.13"
24
+ gem.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wik
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Ethridge
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-01-02 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
+ - wik
46
+ extensions: []
47
+ extra_rdoc_files: []
48
+ files:
49
+ - ".gitignore"
50
+ - ".rake_tasks~"
51
+ - API.md
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - bin/setup
56
+ - bin/wik
57
+ - lib/wik.rb
58
+ - lib/wik/version.rb
59
+ - wik.gemspec
60
+ homepage: https://github.com/wlib/wik
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Wik - Quick and easy Wikipedia searches
84
+ test_files: []