Google Calculator

By Jethro on Dec 26, 2010

Someone at the forum has posted a snippet relating to calculator. That gives me an idea of utilizing Google's calculator feature to do the calculating of most basic operations.

The command is:> !calc or .calc or @calc

on *:sockopen:calc:{ 
  tokenize 124 $sock(calc).mark 
  if $sockerr { $4 * SockOpen ERROR! }
  else { $1-2 | $1 $3 } 
}
on *:sockread:calc:{ 
  tokenize 124 $sock(calc).mark 
  if $sockerr { $4 * SockRead ERROR! }
  else { 
    var %calc, %x /<b>(.*)</b>/, %y /(.*)</b></h2><div style\="/
    sockread %calc 
    if $regex(%calc,%x) && $regex($regml(1),%y) { 
      $4 $regsubex($+($chr(2),$---($remove($regml(1);,))),$&
        /&#(\d+)|<[^>]*>/g,$chr(\1))) | sockclose calc
    } 
  }
}
on $*:text:/((?<=[!@.]calc\s)-?\d+.*)/iS:*:{
  if !%p { inc -u5 %p 
    if $sock(calc) { sockclose $v1 } 
    sockopen calc www.google.com 80 
    sockmark calc sockwrite -nt calc|GET $+(/search?q=,$&
      $remove($replace($regml(1),+,$+(%,2B),x,*),$&
      $chr(32)),&btnG=Search) HTTP/1.1|Host: $&
      $+($sock(calc).addr,$str($crlf,2)) $+ |.msg $iif(#,#,$nick) $+($chr(15),CALC:)
  }
}
alias -l --- return $regsubex($1,/(\=)(.+)/,$+(\1,$chr(32),$remove(\2,$chr(32))))

Comments

Sign in to comment.
EMC   -  Apr 18, 2014

This script and others like it which pull results from Google don't seem to be working anymore. Thanks Google.

 Respond  
snackled   -  Oct 20, 2011

@Jethro
Thank you for such a quick reply! As soon as I am able to vote I will surely give you a 10 :) 3 more days...

 Respond  
Jethro   -  Oct 20, 2011

Thanks snackled for spotting the bug. I've updated the code and this matter is resolved. Please see to the latest edit.

Oh, please give me your ratings if you will ^_^

 Respond  
snackled   -  Oct 20, 2011

@Jethro Awesome script! I am having an issue where if the input or output is more than 4 digits, it displays some formatting. example:
[15:28] <@snackle> .calc 800+800
[15:28] <@sbr> 800 + 800 = 1 600
[15:28] <@snackle> .calc 8000+8000
[15:28] <@sbr> 8 000 + 8 000 = 16 000

Thanks in advance :)

 Respond  
chachin   -  Sep 10, 2011

ok how do I make it say the ansewr itself instead of this:

<@chachin> .calc -3+4
<+chicken> CALC: (-3) + 4 = 1

i want him to say:
<+chicken> 1

just the answer
it works anyways. but with all that other stuff its a bit spammy :S

 Respond  
thorpe_lawrence   -  Aug 17, 2011

@Jethro

Very useful plugin, but you know google calculator has a conversion feature such as:

£10 in $

or

32kg in g

And it would be great to get this included into it. Thanks.

 Respond  
Jethro   -  Aug 15, 2011

Paranoia, locate this line in the script:

$4 $regsubex($+($chr(2),$remove($regml(1),</sup>,<sup>,;)),/&#(\d+)/g,$chr(\1)))

and change it to:

$4 $regsubex($+($chr(2),$remove($regsubex($regml(1),/\p{S}/g,),</sup>,<sup>,;)),/&#(\d+)/g,$chr(\1)))

and see what happens.

 Respond  
Paranoia   -  Aug 15, 2011

Could this be edited to $remove dollar signs, yen signs, and other currency signs, as well as commas?

 Respond  
MashhitDK   -  Jun 06, 2011

Just wanted to say thanks...
Nice work :D

 Respond  
Known   -  Jan 05, 2011

Pretty nice Jethro_. :)

 Respond  
Jethro   -  Jan 05, 2011

:P I wasn't being grammar conscious.

 Respond  
sunslayer   -  Jan 05, 2011

Again, I'm not saying google calculator is the best, but it surely can do more that mIRC's $calc() can't

 Respond  
Jethro   -  Jan 05, 2011

Let take this math equation for instance:> !calc 30m 360mYours will give you > 30 as an answer, where mine will give you > (30 meters) 360 meters = 10 800 m2as an answer. Again, I'm not saying google calculator is the best, but it surely can do more that mIRC's $calc() can't.

 Respond  
Jethro   -  Jan 05, 2011

Savage_CL, google calculator can do more than mIRC's $calc()

 Respond  
Savage_CL   -  Jan 05, 2011

is this an "I'm doing it because I can" script or is it supposed to be useful?
why not just do something like \

on :TEXT:!calc:*: { notice $nick The solution to your problem is: $calc($2-) }

 Respond  
Jethro   -  Dec 26, 2010

lol Yes, it is. Thank you.

 Respond  
sunslayer   -  Dec 26, 2010

you can improve your regex by using

/((?<=[!@.]calc\s)-?\d+.*)/iS

instead of

/((?<=[!@.]calc\s)\d+.*)/iS

better? :p

 Respond  
Jethro   -  Dec 26, 2010

Ok I changed it to - because the . will match anything. :p

Yes, it wouldn't be hard, but as constructive as you should have been, telling a code will fail doesn't usually help a situation. When you offer your input or example, everybody can learn from you, something they either already know or don't. ^^

 Respond  
sunslayer   -  Dec 26, 2010

i didn't think adding -? would have been that hard lol

 Respond  
Jethro   -  Dec 26, 2010

Edited the regex pattern as per sunslayer's findings.

 Respond  
Jethro   -  Dec 26, 2010

Thanks for spotting the obvious. It'd be nice offering your version of approach so I can improve the regex pattern, rather than telling me it will fail.

 Respond  
sunslayer   -  Dec 26, 2010

your regex will fail on !calc -3+4

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.