-
Notifications
You must be signed in to change notification settings - Fork 2
/
rank.rb
executable file
·44 lines (34 loc) · 922 Bytes
/
rank.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/ruby
# Dependencies:
# - google-search
require 'uri'
require 'rubygems'
require 'google-search'
#====================================
def print_usage
print <<TEXT
Checks ranking of domain for a single google search term
usage: #{$0} [domain] [keyword]
TEXT
exit
end
print_usage if ARGV.count != 2
#====================================
def find_item uri, query
search = Google::Search::Web.new do |search|
search.query = query
search.size = :large
search.each_response { print '.'; $stdout.flush }
end
search.find { |item| item.uri =~ uri }
end
#====================================
domain = ARGV[0]#URI.parse( ARGV[0] ).host
keyword = ARGV[1].downcase.gsub(/[^a-z\d\- ]/,'')
puts "Checking rank for #{domain} on keyword '#{keyword}':\t"
print "%-35s " % keyword
if item = find_item(Regexp.compile(domain), keyword)
puts " #%d" % (item.index + 1)
else
puts " Not found"
end