ruby-search-engine 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.rdoc +43 -0
- data/bin/ruby_search_engine.rb +34 -0
- data/lib/search_engine.rb +80 -0
- metadata +66 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3c4a45342de9fc5d8db00a492dd094719e015771
|
4
|
+
data.tar.gz: c4be0e00f42b4c7b62f7df33fe33ce9aa9edcad1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 693f8ae4b696e1a61d3a7dece5c2039359561947c18b86d628fc02acbe4968faa20bceee4abab5a6e85111e1002dbb44988176b91a0a12d125a1e1b532d0ba57
|
7
|
+
data.tar.gz: 6eb7c2062353ecd711722a39c7c0ce986b2152aff942ecb6318c3ed29868b44ec83cc7d7cfed569d32a80185a88a908bb339f18126a440697de0503c9ee28cc4
|
data/README.rdoc
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
= ruby-search-engine
|
2
|
+
|
3
|
+
Author:: Martin Velez, Michael Yen, Matthew Le
|
4
|
+
Copyright:: Copyright (c) 2016 Authors
|
5
|
+
License:: MIT
|
6
|
+
|
7
|
+
= Description
|
8
|
+
|
9
|
+
# Coming soon...
|
10
|
+
Search the web using Ruby.
|
11
|
+
|
12
|
+
= Design
|
13
|
+
|
14
|
+
# Coming soon...
|
15
|
+
|
16
|
+
= Installation
|
17
|
+
gem install ruby-search-engines
|
18
|
+
|
19
|
+
= Alternative Tools
|
20
|
+
|
21
|
+
* google-search[https://github.com/tj/google-search]
|
22
|
+
|
23
|
+
= Usage
|
24
|
+
|
25
|
+
You can simply provide input via STDIN.
|
26
|
+
ruby ruby_search_engine.rb QUERY
|
27
|
+
|
28
|
+
You can also run the executable via the ruby_cli gem.
|
29
|
+
./ruby_search_engine.rb c++ how to initialize an array
|
30
|
+
./ruby_search_engine.rb -p 2 c++ how to initialize an array
|
31
|
+
|
32
|
+
= Dependencies
|
33
|
+
|
34
|
+
* Ruby 1.9.1 or greater
|
35
|
+
* ruby_cli[https://github.com/martinvelez/ruby_cli] to run the gem executable
|
36
|
+
|
37
|
+
= TODO
|
38
|
+
|
39
|
+
* Test to determine limits of current approach which parses and stores JSON object.
|
40
|
+
|
41
|
+
= Source Code
|
42
|
+
https://github.com/martinvelez/ruby-search-engine
|
43
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib','search_engine.rb'))
|
4
|
+
require 'ruby_cli'
|
5
|
+
|
6
|
+
class App
|
7
|
+
include RubyCLI
|
8
|
+
|
9
|
+
def initialize_command_options
|
10
|
+
@options = {:pages => 1}
|
11
|
+
end
|
12
|
+
def define_command_option_parsing
|
13
|
+
@opt_parser.on('-p', '--pages RANGE', Integer, 'what pages you want') do |pages|
|
14
|
+
@options[:pages] = pages
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def command
|
19
|
+
query = ARGV.join(" ")
|
20
|
+
search_engine = SearchEngine.new
|
21
|
+
|
22
|
+
begin
|
23
|
+
results = search_engine.search(query, :url, pages: @options[:pages])
|
24
|
+
rescue Exception => e
|
25
|
+
puts e
|
26
|
+
end
|
27
|
+
|
28
|
+
puts results
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
app = App.new(ARGV, __FILE__)
|
34
|
+
app.run
|
@@ -0,0 +1,80 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'open-uri'
|
3
|
+
|
4
|
+
class SearchEngine
|
5
|
+
attr_accessor :uri
|
6
|
+
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
self.uri = "http://www.google.com/uds/GwebSearch?start=0"\
|
10
|
+
"&rsz=large&hl=en&key=notsupplied&v=1.0&"
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
def search(query, *fields, pages: 1)
|
15
|
+
results = []
|
16
|
+
raise "No query given" if query.empty?
|
17
|
+
|
18
|
+
for i in 1..pages
|
19
|
+
uri = build_url(query, i)
|
20
|
+
file = open(uri)
|
21
|
+
my_hash = JSON.parse(file.read, symbolize_names: true)
|
22
|
+
raise "Page is out of bounds" unless !my_hash[:responseData].nil?
|
23
|
+
hash_results = my_hash[:responseData][:results]
|
24
|
+
results << extract_fields(fields, hash_results)
|
25
|
+
end
|
26
|
+
|
27
|
+
return results
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
def build_url(query, page)
|
32
|
+
start = (page - 1) * 8
|
33
|
+
self.uri.chomp! "&rsz=large&hl=en&key=notsupplied&v=1.0&"
|
34
|
+
|
35
|
+
while self.uri[-1, 1] != "=" do
|
36
|
+
self.uri.chop!
|
37
|
+
end
|
38
|
+
|
39
|
+
self.uri << "#{start}&rsz=large&hl=en&key=notsupplied&v=1.0&"
|
40
|
+
|
41
|
+
query.strip!
|
42
|
+
q_uri = URI.encode_www_form('q' => query)
|
43
|
+
|
44
|
+
return self.uri + q_uri + "&filter=1"
|
45
|
+
end
|
46
|
+
|
47
|
+
|
48
|
+
def extract_fields(fields, hash_results)
|
49
|
+
extracted_results = []
|
50
|
+
if fields.empty?
|
51
|
+
raise "No fields given."
|
52
|
+
else
|
53
|
+
fields.each do |f|
|
54
|
+
index = 0
|
55
|
+
|
56
|
+
hash_results.each do |hr|
|
57
|
+
if hr.has_key?(f)
|
58
|
+
extracted_results << hash_results[index][f]
|
59
|
+
index += 1
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
return extracted_results
|
66
|
+
end
|
67
|
+
|
68
|
+
|
69
|
+
def get_result_count(query)
|
70
|
+
cursor_hash = open_file(query)
|
71
|
+
return cursor_hash[:responseData][:cursor][:resultCount]
|
72
|
+
end
|
73
|
+
|
74
|
+
|
75
|
+
def get_search_time(query)
|
76
|
+
cursor_hash = open_file(query)
|
77
|
+
return cursor_hash[:responseData][:cursor][:searchResultTime]
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ruby-search-engine
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Martin Velez
|
8
|
+
- Michael Yen
|
9
|
+
- Mathew Le
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2016-03-29 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: ruby_cli
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - ">="
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.2.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 0.2.0
|
29
|
+
description: Search the web using Ruby with this fast and lightweight gem.
|
30
|
+
email:
|
31
|
+
- 'marvelez@ucdavis.edu '
|
32
|
+
- mkyen@ucdavis.edu
|
33
|
+
- matle@ucdavis.edu
|
34
|
+
executables:
|
35
|
+
- ruby_search_engine.rb
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- README.rdoc
|
40
|
+
- bin/ruby_search_engine.rb
|
41
|
+
- lib/search_engine.rb
|
42
|
+
homepage: http://github.com/martinvelez/ruby-search-engine
|
43
|
+
licenses: []
|
44
|
+
metadata: {}
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- bin
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 2.5.1
|
63
|
+
signing_key:
|
64
|
+
specification_version: 4
|
65
|
+
summary: Search the web using Ruby
|
66
|
+
test_files: []
|