goog 0.0.1

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.
Files changed (2) hide show
  1. data/bin/goog +76 -0
  2. metadata +47 -0
data/bin/goog ADDED
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+ require 'nokogiri'
4
+ require 'cgi'
5
+ require 'yaml'
6
+ require 'uri'
7
+
8
+
9
+ if ARGV[0] =~ /^-h|--help/
10
+ puts <<END
11
+ Usage: goog [-n pages] [query]
12
+
13
+ Synopsis:
14
+ -n [pages] Return n pages of results
15
+ -h, --help Show this message
16
+
17
+ goog 0.1.0
18
+ http://github.com/danchoi/goog
19
+ Author: Daniel Choi <dhchoi@gmail.com>
20
+ END
21
+ exit
22
+ end
23
+
24
+ pages = ARGV[0] =~ /^-n/ ? (ARGV.shift && ARGV.shift[/\d+/,0].to_i) : 1
25
+
26
+ query = ARGV.join(' ')
27
+ unless query
28
+ abort "Please provide a search query"
29
+ end
30
+ CACHE = "#{ENV['HOME']}/.goog.cache.yml"
31
+ unless `which tidy` =~ /tidy/
32
+ abort "No tidy found. Please install tidy."
33
+ end
34
+ if RUBY_VERSION !~ /^1.9/
35
+ abort "Requires Ruby 1.9"
36
+ end
37
+ query = CGI.escape query
38
+
39
+ (1..pages).each do |page|
40
+ q = page == 1 ? query : "#{query}&start=#{page * 10}"
41
+ curl = "curl -s -A Mozilla http://www.google.com/search?q=#{q} | tidy --wrap 0 -indent -ashtml --merge-divs yes 2>/dev/null"
42
+ resp = %x{#{curl}}
43
+ doc = Nokogiri::HTML resp
44
+ doc.search('ol li.g').each_with_index {|li, index|
45
+ next unless li.at('h3 a')
46
+ link = li.at('h3 a')['href'].sub(/^\/url\?q=/, '')
47
+ if link !~ /^http:/
48
+ link = "http://google.com#{link}"
49
+ end
50
+ title = li.at('h3 a').inner_text
51
+ description = li.at('div.s')
52
+ excerpt = if description
53
+ description.search('span').remove
54
+ excerpt = begin
55
+ s = description.inner_text.strip
56
+ s.force_encoding('iso-8859-1')
57
+ s.encode!('utf-8', undef: :replace, invalid: :replace)
58
+ s.gsub(/\s{2,}/, ' ')
59
+ rescue
60
+ puts "ERROR"
61
+ puts description
62
+ puts $!
63
+ end
64
+ end
65
+ number = (page - 1) * 10 + (index + 1)
66
+ res = if STDOUT.tty?
67
+ ["#{number}. \e[36m#{title}\e[0m", excerpt, "\e[35m#{link }\e[0m"]
68
+ else
69
+ ["#{number}. #{title}", excerpt, link]
70
+ end
71
+ puts res.compact
72
+ puts
73
+ }
74
+ end
75
+
76
+
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: goog
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Daniel Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-05 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A command line interface to Google search
15
+ email:
16
+ - dhchoi@gmail.com
17
+ executables:
18
+ - goog
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - bin/goog
23
+ homepage: http://danielchoi.com/software/goog.html
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: 1.9.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project: goog
43
+ rubygems_version: 1.8.19
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: A command line interface to Google search
47
+ test_files: []