Created
September 19, 2010 11:09
-
-
Save rummelonp/586673 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby -Ku | |
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