Created
August 26, 2008 14:02
-
-
Save leahneukirchen/7263 to your computer and use it in GitHub Desktop.
Command line tools for google
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/env ruby | |
# gcalc <google calculator string> / 26aug2008 chneukirchen / public domain | |
require 'open-uri' | |
q = ARGV.join(" ").gsub(/./m) { "%%%02X" % $&[0] } | |
html = open("http://www.google.com/search?q=#{q}").read | |
puts (html[%r{<font size=\+1><b>(.*?)</b></h2>}, 1] || 'not a calculation'). | |
gsub(%r{\240|<font size=-2> </font>}, "").gsub("×", "x"). | |
gsub(%r{<sup>(.*?)</sup>}, '^\\1 ') |
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/env ruby -s | |
# goog [-q] [num] <string> / 26aug2008 chneukirchen / public domain | |
require 'open-uri' | |
n = ARGV[0] =~ /^\d+$/ ? ARGV.shift : 5 | |
q = ARGV.join(" ").gsub(/./m) { "%%%02X" % $&[0] } | |
open("http://www.google.com/search?ie=utf-8&oe=utf-8&q=#{q}&num=#{n}").read. | |
scan(%r{<h3 class=r><a href="(.*?)" class=l>(.*?)</a>}) { |url, desc| | |
puts url | |
puts " " + desc[0..70].gsub(%r{</?em>}, '') unless $q | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment