Google Calculator command line tool

Discussion in 'Ruby' started by m4dc4p, Oct 7, 2005.

  1. m4dc4p

    m4dc4p Guest

    I whipped a quick script to send queries to Google and scrape results
    if it's a valid "calculator" query. If you don't know what I mean, put
    "5 + 5" into the Google search box. I love the feature but hate having
    to open a web browser each time I want to use it. Thus, this script!

    I'm pretty much a n00b to Ruby so any feedback, improvements, comments
    are appreciated. Enjoy!

    --- cut here ---
    require 'net/http'
    require 'cgi'

    format_result = Proc.new { |s|
    s.gsub("<font size=-2>
    </font>",",").gsub("×","x").gsub("<sup>","^").gsub("</sup>", "")
    }

    matchExp = Regexp.new("<td><img
    src=/images/calc_img.gif></td><td>&nbsp;</td><td nowrap>(.*?) =
    (.*?)</b>")
    while ((! (print "Enter an expression (exit to quit): "; line =
    gets).nil?) && line.strip.upcase != "EXIT")
    resp =
    Net::HTTP.get_response(URI.parse("http://www.google.com/search?q=#{CGI.escape(line.strip)}"))
    if resp.code == "200"
    begin
    matches = matchExp.match(resp.body)
    puts "==>" + format_result.call(matches[2])
    rescue NoMethodError
    puts "==> Expression not understood."
    rescue
    puts "==> Expression not understood. (#{$!.class.inspect},
    #{$!.inspect})"
    end
    else
    puts "==> Response error: #{resp.code}"
    end
    end
    m4dc4p, Oct 7, 2005
    #1
    1. Advertising

  2. m4dc4p

    m4dc4p Guest

    I should have explained the format_result proc a little. Google's
    responses have spaces (using '<font size=-2> </font>') instead of
    commas, so that is the first replacement. The second two deal with
    scientific notation. Try entering a query like "400 lightyears * 10
    parsecs" and you will see the characters its trying to replace.
    m4dc4p, Oct 7, 2005
    #2
    1. Advertising

  3. On 10/6/05, m4dc4p <> wrote:
    > I should have explained the format_result proc a little. Google's
    > responses have spaces (using '<font size=3D-2> </font>') instead of
    > commas, so that is the first replacement. The second two deal with
    > scientific notation. Try entering a query like "400 lightyears * 10
    > parsecs" and you will see the characters its trying to replace.


    For what it's worth, it's probably better to look at reimplementing
    this using the Google WS API, because Google looks down (harshly) on
    screen scraping.

    -austin
    --
    Austin Ziegler *
    * Alternate:
    Austin Ziegler, Oct 7, 2005
    #3
  4. m4dc4p

    James Britt Guest

    Austin Ziegler wrote:
    >
    > For what it's worth, it's probably better to look at reimplementing
    > this using the Google WS API, because Google looks down (harshly) on
    > screen scraping.


    Is that what this is?

    I believe Google is concerned with the extraction and reuse of search
    data to drive (part of) another site or application. I see the Google
    Calculator app to be an implementation of a Web browser: Make an HTTP
    request, get back the text, and render it.


    James
    James Britt, Oct 7, 2005
    #4
  5. Hi,

    m4dc4p wrote:

    >format_result = Proc.new { |s|
    > s.gsub("<font size=-2>
    ></font>",",").gsub("×","x").gsub("<sup>","^").gsub("</sup>", "")
    >}
    >
    >

    Why not just def format_result(s) ... end?

    D3v1n
    Devin Mullins, Oct 7, 2005
    #5
  6. m4dc4p

    m4dc4p Guest

    I thought I had tried that but obviously I did something wrong. Thanks
    for the suggestion - its a little clearer now.
    m4dc4p, Oct 7, 2005
    #6
  7. m4dc4p

    m4dc4p Guest

    The needs for this script are so lightweight, it just didn't seem worth
    it to go through the pain of figuring out how to call Google's WS API.
    I did look into it, and if it had allowed me to make calculator
    specific queries I would have used it. Unfortunately, it does not. I'm
    not sure what results it would return for this kind of query but it
    seemed a lot simpler just to extract the results from their results
    page.
    m4dc4p, Oct 7, 2005
    #7
  8. m4dc4p

    Phil Tomson Guest

    In article <>,
    m4dc4p <> wrote:
    >I whipped a quick script to send queries to Google and scrape results
    >if it's a valid "calculator" query. If you don't know what I mean, put
    >"5 + 5" into the Google search box. I love the feature but hate having
    >to open a web browser each time I want to use it. Thus, this script!


    Google is now a calculator?! Uh, errr, umm, this is getting a little bit out
    of hand, no?

    You could also use irb as a calculator:
    irb(main):009:0> 5+5
    => 10

    >
    >I'm pretty much a n00b to Ruby so any feedback, improvements, comments
    >are appreciated. Enjoy!
    >
    >--- cut here ---
    >require 'net/http'
    >require 'cgi'
    >
    >format_result = Proc.new { |s|
    > s.gsub("<font size=-2>
    ></font>",",").gsub("×","x").gsub("<sup>","^").gsub("</sup>", "")
    >}
    >
    >matchExp = Regexp.new("<td><img
    >src=/images/calc_img.gif></td><td>&nbsp;</td><td nowrap>(.*?) =
    >(.*?)</b>")
    >while ((! (print "Enter an expression (exit to quit): "; line =
    >gets).nil?) && line.strip.upcase != "EXIT")
    > resp =
    >Net::HTTP.get_response(URI.parse("http://www.google.com/search?q=#{CGI.escape(line.strip)}"))
    > if resp.code == "200"
    > begin
    > matches = matchExp.match(resp.body)
    > puts "==>" + format_result.call(matches[2])
    > rescue NoMethodError
    > puts "==> Expression not understood."
    > rescue
    > puts "==> Expression not understood. (#{$!.class.inspect},
    >#{$!.inspect})"
    > end
    > else
    > puts "==> Response error: #{resp.code}"
    > end
    >end
    >


    Well, that's, uh, cool, but there's gotta be easier ways to add numbers ;-)

    Phil
    Phil Tomson, Oct 7, 2005
    #8
  9. m4dc4p

    Phil Tomson Guest

    In article <>,
    m4dc4p <> wrote:
    >I whipped a quick script to send queries to Google and scrape results
    >if it's a valid "calculator" query. If you don't know what I mean, put
    >"5 + 5" into the Google search box. I love the feature but hate having
    >to open a web browser each time I want to use it. Thus, this script!
    >
    >I'm pretty much a n00b to Ruby so any feedback, improvements, comments
    >are appreciated. Enjoy!
    >
    >--- cut here ---
    >require 'net/http'
    >require 'cgi'
    >
    >format_result = Proc.new { |s|
    > s.gsub("<font size=-2>
    ></font>",",").gsub("×","x").gsub("<sup>","^").gsub("</sup>", "")
    >}
    >
    >matchExp = Regexp.new("<td><img
    >src=/images/calc_img.gif></td><td>&nbsp;</td><td nowrap>(.*?) =
    >(.*?)</b>")
    >while ((! (print "Enter an expression (exit to quit): "; line =
    >gets).nil?) && line.strip.upcase != "EXIT")
    > resp =
    >Net::HTTP.get_response(URI.parse("http://www.google.com/search?q=#{CGI.escape(line.strip)}"))
    > if resp.code == "200"
    > begin
    > matches = matchExp.match(resp.body)
    > puts "==>" + format_result.call(matches[2])
    > rescue NoMethodError
    > puts "==> Expression not understood."
    > rescue
    > puts "==> Expression not understood. (#{$!.class.inspect},
    >#{$!.inspect})"
    > end
    > else
    > puts "==> Response error: #{resp.code}"
    > end
    >end
    >


    Sorry for two replies to the same post, but I tried your code and I always
    get Expression not understood:

    Enter an expression (exit to quit): 20 + 20
    ==> Expression not understood.

    Oh, and as a former boss of mine used to say often: "Rube Goldberg would be
    proud" (of this very convoluted method for adding numbers. ;-)

    Phil
    Phil Tomson, Oct 7, 2005
    #9
  10. Actually, it's a little cooler than anything built into IRB. For example:
    http://www.google.com/search?hl=3Den&q=3D121 feet in furlongs&btnG=3DGoogle=
    +Search

    or:
    http://www.google.com/search?hl=3Den&lr=3D&safe=3Doff&c2coff=3D1&q=3D15 pou=
    nd+feet+in+newton+meters&btnG=3DSearch

    On 10/7/05, Phil Tomson <> wrote:
    > In article <>,
    > m4dc4p <> wrote:
    > >I whipped a quick script to send queries to Google and scrape results
    > >if it's a valid "calculator" query. If you don't know what I mean, put
    > >"5 + 5" into the Google search box. I love the feature but hate having
    > >to open a web browser each time I want to use it. Thus, this script!

    >
    > Google is now a calculator?! Uh, errr, umm, this is getting a little bit =

    out
    > of hand, no?
    >
    > You could also use irb as a calculator:
    > irb(main):009:0> 5+5
    > =3D> 10
    >
    > >
    > >I'm pretty much a n00b to Ruby so any feedback, improvements, comments
    > >are appreciated. Enjoy!
    > >
    > >--- cut here ---
    > >require 'net/http'
    > >require 'cgi'
    > >
    > >format_result =3D Proc.new { |s|
    > > s.gsub("<font size=3D-2>
    > ></font>",",").gsub("×","x").gsub("<sup>","^").gsub("</sup>", "")
    > >}
    > >
    > >matchExp =3D Regexp.new("<td><img
    > >src=3D/images/calc_img.gif></td><td>&nbsp;</td><td nowrap>(.*?) =3D
    > >(.*?)</b>")
    > >while ((! (print "Enter an expression (exit to quit): "; line =3D
    > >gets).nil?) && line.strip.upcase !=3D "EXIT")
    > > resp =3D
    > >Net::HTTP.get_response(URI.parse("http://www.google.com/search?q=3D#{CGI=

    escape(line.strip)}"))
    > > if resp.code =3D=3D "200"
    > > begin
    > > matches =3D matchExp.match(resp.body)
    > > puts "=3D=3D>" + format_result.call(matches[2])
    > > rescue NoMethodError
    > > puts "=3D=3D> Expression not understood."
    > > rescue
    > > puts "=3D=3D> Expression not understood. (#{$!.class.inspect},
    > >#{$!.inspect})"
    > > end
    > > else
    > > puts "=3D=3D> Response error: #{resp.code}"
    > > end
    > >end
    > >

    >
    > Well, that's, uh, cool, but there's gotta be easier ways to add numbers ;=

    -)
    >
    > Phil
    >
    >
    Wilson Bilkovich, Oct 7, 2005
    #10
  11. Ryan Leavengood, Oct 7, 2005
    #11
  12. m4dc4p

    m4dc4p Guest

    Phil,

    Comment out all the exception handling and you should see the error you
    are getting. I imagine it's either some weird newline/whitespace that
    is getting into the input (though all of that should be stripped ...)
    or Google is returning a result format the script doesn't expect. It's
    pretty fragile.

    Note to self - get decent newsreader software. This google groups stuff
    is really lacking.
    m4dc4p, Oct 7, 2005
    #12
  13. m4dc4p

    Greg Brown Guest

    m4dc4p wrote:

    > Note to self - get decent newsreader software. This google groups stuff
    > is really lacking.


    Yeah, it is in some regard but if you mean you can't snip posts, I
    recently found out that if you hit options and then hit reply there
    instead of directly under the post it lets you do snipping properly.
    Greg Brown, Oct 10, 2005
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TWFuZHk=?=

    Running command line tool from web application

    =?Utf-8?B?TWFuZHk=?=, Jan 14, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    3,642
    =?Utf-8?B?TWFuZHk=?=
    Jan 17, 2005
  2. Corno
    Replies:
    8
    Views:
    1,885
  3. Dave
    Replies:
    9
    Views:
    16,665
    RichardShepherd
    Jan 1, 2012
  4. Trung Chinh Nguyen
    Replies:
    1
    Views:
    15,706
    Trung Chinh Nguyen
    Jul 27, 2006
  5. Edvard Majakari
    Replies:
    4
    Views:
    533
    Edvard Majakari
    May 27, 2004
Loading...

Share This Page