Skip to content

Instantly share code, notes, and snippets.

@m040601
Forked from rummelonp/ggrks
Created October 14, 2010 10:08
Show Gist options
  • Save m040601/625971 to your computer and use it in GitHub Desktop.
Save m040601/625971 to your computer and use it in GitHub Desktop.
goog link/tit - only mech needed, shebang
#!/usr/bin/ruby -Ku
# only needs mech/noko retrievs goog link/titf
args = ARGV
if args.length == 0
puts "Usage: ggrks [-n num] [-p page] [query]"
puts "Try `gglks --help' for more information."
exit
end
if args.any? {|arg| arg =~ /^-(h|-help)$/i}
puts "ググレカス"
exit
end
if args.any? {|arg| arg =~ /^(-d)$/i}
puts "どうぞGoogleで検索くださいカス"
exit
end
if args.any? {|arg| arg =~ /^redbull$/i}
puts "この ggrks にはスーパー赤牛さんパワーなどはありません"
exit
end
options = args.find_all do |arg|
arg =~ /^-\w+$/
end.collect do |option|
val = args.at(args.index(option) + 1)
if val =~ /^\d+$/
{:key => option, :val => val}
else
null
end
end.find_all do |arg|
!arg.nil?
end.each do |option|
args.delete(option[:key])
args.delete(option[:val])
end
require 'rubygems'
require 'mechanize'
SEARCH_URL = "http://www.google.co.jp/search"
query = URI.encode(args.join(' '))
page = options.find {|option| option[:key] =~ /^-(p|-page)$/}
page = unless page.nil? then page[:val].to_i else 1 end - 1
num = options.find {|option| option[:key] =~ /^-(n|-num)$/}
num = unless num.nil? then num[:val].to_i else 10 end
url = "#{SEARCH_URL}?q=#{query}&num=#{num}"
if page > 0
url += "&start=#{page * 10}"
end
agent = Mechanize.new
page = agent.get(url)
doc = page.root
doc.css('li[class*="g"]').each do |result|
link = result.css('a[class*="l"]')[0];
unless link.nil?
puts link.text
puts link['href']
puts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment