This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

gconvert.tcl Update Please! :)

Support & discussion of released scripts, and announcements of new releases.
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

Thanks much!

:D
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

I like this better.

Code: Select all

#############################################
#  gconvert.tcl v2.0       by AkiraX        #
#                    <#AnimeFiends@EFNet>   #
#############################################

####### DESCRIPTION #########################
# Uses the google calculator to perform
# conversions and mathematics
#############################################

####### USAGE ###############################
# !convert <quantity> to <quantity>
# !math <equation> : perform mathematics
# !calc <equation> : same as !math
#############################################

####### CHANGELOG ###########################
# v2.1 : fixed functionality, repaired by Scott Glover 
# v2.0 : allow convert code to perform math
# v1.0 : support for google conversions
#############################################

####### LICENSE ############################# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, U
#############################################

package require http

bind pub -|- !convert gconvert:run
proc gconvert:run { nick host handle chan text } {
	set convert(nick) [join [lrange [split $nick] 0 0]]
	set convert(input) [join [lrange [split $text] 0 end]]

	# create google URL
	set token [http::config -useragent "Mozilla"]
	set convert(url) [gconvert:webstring "convert $convert(input)"]
	set convert(url) "http://www.google.com/search?hl=en&q=$convert(url)"

	set num_tries 0
	set try_again 1
	while { $try_again } {
		incr num_tries
		if { $num_tries > 3 } {
			set try_again 0
			break
		}

		# grab the info from google
		set token [http::geturl $convert(url) -timeout 15000]
		upvar #0 $token state
		if { $state(status) == "timeout" } {
			puthelp "PRIVMSG $chan :Sorry, your request timed out."
			return
		}
		set convert(html) [split [http::data $token] "\n"]
		http::cleanup $token

		# find the answer
		set num_lines 0
		set convert(answer) ""
		foreach line $convert(html) {
			incr num_lines
			if { $num_lines > 100 } {
				set try_again 0
				break
			}
			
			# take suggestions
			if { [regexp {Did you mean:} $line] } {
				# grab the new URL and start over
				regexp {Did you mean: </font><a href=(.*?) class=p>} $line match convert(url)
				set convert(url) "http://www.google.com$convert(url)"
				break
			}

			# find the calculator
			if { [regexp {src=/images/calc_img.gif} $line] } { 
                        regexp "calc_img.+?nowrap>(.+?)/h" $line - convert(answer) 
                        regexp "<b>(.*)</b>" $convert(answer) match convert(answer) 
                        set try_again 0 
                        break 
                  } 
		}
	}

	if { $convert(answer) == "" } {
		puthelp "PRIVMSG $chan :Sorry, didn't work out."
		return
	}

	puthelp "PRIVMSG $chan :[gconvert:gstring $convert(answer)]"

	return
}

bind pub -|- !math gconvert:math
bind pub -|- !calc gconvert:math
proc gconvert:math { nick host handle chan text } {
	set calc(nick) [join [lrange [split $nick] 0 0]]
	set calc(input) [join [lrange [split $text] 0 end]]

	# create google URL
	set token [http::config -useragent "Mozilla"]
	set calc(url) [gconvert:webstring "$calc(input)"]
	set calc(url) "http://www.google.com/search?hl=en&q=$calc(url)"

	set num_tries 0
	set try_again 1
	while { $try_again } {
		incr num_tries
		if { $num_tries > 2 } {
			set try_again 0
			break
		}

		# grab the info from google
		set token [http::geturl $calc(url) -timeout 15000]
		upvar #0 $token state
		if { $state(status) == "timeout" } {
			puthelp "PRIVMSG $chan :Sorry, your request timed out."
			return
		}
		set calc(html) [split [http::data $token] "\n"]
		http::cleanup $token

		# find the answer
		set num_lines 0
		set calc(answer) ""
		foreach line $calc(html) {
			incr num_lines
			if { $num_lines > 100 } {
				set try_again 0
				break
			}

			# take suggestions
			if { [regexp {Did you mean:} $line] } {
				# grab the new URL and start over
				regexp {Did you mean: </font><a href=(.*?) class=p>} $line match calc(url)
				set calc(url) "http://www.google.com$calc(url)"
				break
			}

			# find the calculator
			if { [regexp {src=/images/calc_img.gif} $line] } { 
                       regexp "calc_img.+?nowrap>(.+?)/h" $line - calc(answer) 
                       regexp "<b>(.*)</b>" $calc(answer) match calc(answer) 
                       set try_again 0 
                       break 
                  } 
		}
	}

	if { $calc(answer) == "" } {
		puthelp "PRIVMSG $chan :Sorry, didn't work out."
		return
	}

	puthelp "PRIVMSG $chan :[gconvert:gstring $calc(answer)]"

	return
}

proc gconvert:webstring { input } {
	set input [string map { {%} {%25} } $input]
	set input [string map { {&} {&} } $input]
	set input [string map { {*} {%2A} } $input]
	set input [string map { {+} {%2B} } $input]
	set input [string map { {,} {%2C} } $input]
	set input [string map { {-} {%2D} } $input]
	set input [string map { {/} {%2F} } $input]
	set input [string map { {^} {%5E} } $input]
	set input [string map { {<} {<} } $input]
	set input [string map { {>} {>} } $input]
	#set input [string map { {"} {"} } $input]
	set input [string map { {"} {} } $input]
	set input [string map { {'} {'} } $input]
	set input [string map { { } {+} } $input]

	return $input
}

proc gconvert:gstring { input } {
	set input [string map { {<font size=-2> </font>} {,} } $input]
	set input [string map { {×} {x} } $input]
	set input [string map { {10<sup>} {10^} } $input]
	set input [string map { {</sup>} {} } $input]

	return $input
}

putlog "*Google Convert v2.0* Loaded"
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

holycrap wrote:I like this better.

Code: Select all

snipped
Haw. You prefer the script correct mispellings and not broadcast them allowing the user to requery. You'd rather the bot just go hey, your wrong, and then query using the 'did you mean' instead? I won't do this. So yeah, your better off using that broken script if thats what you want. I prefer having a spell checker with mine.. Yours will not function this way.
<speechles> !spell .it pizzs
<sp33chy> Forse cercavi: pizza?
<speechles> !spell .fr le français Never Surrendr, oui droit
<sp33chy> Essayez avec cette orthographe : le français Never Surrender, oui droit?
<speechles> !spell havign a spll checkr si ncie
<sp33chy> Did you mean: having a spell checker is nice?

Code: Select all

   set convert(nick) [join [lrange [split $nick] 0 0]]
   set convert(input) [join [lrange [split $text] 0 end]] 
Basically, those above do nothing. You split the string, lrange it using the same start/end it already had, then join it back together. Basically doing nothing but returning it to what it already was, why?

Code: Select all

   set convert(nick) $nick
   set convert(input) $text
This has the exact same effect as your code above.
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

Dude, I have no idea what you're asking. I don't know anyting about scripting. I just did what "texke" suggested and it works, so I just use it.

Don't mind my idiocy.

:D
User avatar
Alchera
Revered One
Posts: 3344
Joined: Mon Aug 11, 2003 12:42 pm
Location: Ballarat Victoria, Australia
Contact:

Post by Alchera »

holycrap wrote:Dude, I have no idea what you're asking. I don't know anyting about scripting. I just did what "texke" suggested and it works, so I just use it.

Don't mind my idiocy.

:D
As long as YOU are happy with the way the script now works holycrap is all that matters. :wink:
Add [SOLVED] to the thread title if your issue has been.
Search | FAQ | RTM
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

The script is down again.

:-(
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

holycrap wrote:The script is down again.

:-(
<speechles> !convert 2+2-7*pi - 3.14159^100
<sp33chy> 2 + 2 - (7 * pi) - (3.14159^100) = -5.18741013 × 10^49
<speechles> !convert fturama
<sp33chy> Did you mean: futurama?
<speechles> !convert 2 pounds + 6 euro + the answer to life, the universe and everything $ in peso
<sp33chy> (2 British pounds) + (6 Euros) + (the answer to life, the universe and everything US$) = 570.020365 Mexican pesos
The one I've posted works indeed. The one you posted depends on the </h2> tag which isn't present anymore all on the same line.

Code: Select all

regexp "calc_img.+?nowrap>(.+?)/h" $line - convert(answer) 
Whoever made that line had no idea what they were doing, the reason there is an /h there is because of.. well, because they intended to stop the parsing at </h2> tag. But, since they had no idea what they were doing, they forgot the "<"??! This is why I would rather not touch that script or use any of it's code, it's silly...BTW, the reason it no longer works is that </h2> tag isn't used by google anymore in that spot on that line (and since that script doesn't remove newlines,etc and create one big line of html it fails (it doesnt really fail, what happens is your variable will contain a newline, and any variables with newlines will only be displayed up to that newline everything after it will not). It keeps lines seperate and uses a foreach to step through them.)

Edit: to fix it would require replacing "/h" with "\n" within those "calc_img.+?nowrap" regexp's. This will make the script recognize newline as termination and fulfill the regexp without passing along newlines to the variables. You may get stray html tags this way, but it will work and it's quite easy to adjust the regexp to anticipate them, or conversely construct a cleaning regsub to remove them.
Last edited by speechles on Fri Jun 27, 2008 10:25 am, edited 5 times in total.
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

Thanks speechles, it works like a charm. :) You're the man! I mean... 'Master'.

.chanset #channel +gconvert

:D
t
texke
Voice
Posts: 3
Joined: Thu May 22, 2008 1:33 pm
Location: Eeklo, Belgium
Contact:

Post by texke »

speechles wrote:Whoever made that line had no idea what they were doing, the reason there is an /h there is because of.. well, because they intended to stop the parsing at </h2> tag. But, since they had no idea what they were doing, they forgot the "<"??! This is why I would rather not touch that script or use any of it's code, it's silly
Sorry mate, just made a temporary solution, and since I have no experience with regex, it was trial and error :( :P
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

texke wrote:
speechles wrote:Whoever made that line had no idea what they were doing, the reason there is an /h there is because of.. well, because they intended to stop the parsing at </h2> tag. But, since they had no idea what they were doing, they forgot the "<"??! This is why I would rather not touch that script or use any of it's code, it's silly
Sorry mate, just made a temporary solution, and since I have no experience with regex, it was trial and error :( :P
The text I used to convey my message wasn't intended to have a mean tone to it. I chose the word silly to reflect that fact. I was only explaining the real difference between the script I posted and the script posted with that fix, that was all. If it was you amending the script to add that fix with no experience, then it certainly makes sense why it appeared like it did. I didn't mean for it to come off as some arrogant cocky statement if thats what it sounded like (it sounds like that to me on rereading :().
My apologies back to you sir for misinterpreting things... :oops:
t
texke
Voice
Posts: 3
Joined: Thu May 22, 2008 1:33 pm
Location: Eeklo, Belgium
Contact:

Post by texke »

Heh, no worries :P
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

Google changed something again. Script is down. :cry:
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

Can someone fix this? I know you can "speechles". :) It's ok if you don't have time.

:D
User avatar
speechles
Revered One
Posts: 1398
Joined: Sat Aug 26, 2006 10:19 pm
Location: emerald triangle, california (coastal redwoods)

Post by speechles »

When I get off work tonight I'll get to it :P
If you use the unofficial google, convert does work within it, so of course I know how to fix it.... :lol:
h
holycrap
Op
Posts: 152
Joined: Mon Jan 21, 2008 11:19 pm

Post by holycrap »

Thanks speechles.! :D

The reason I hisitated to use unofficial google is that most people in my channel are familiar with the !calc and !convert in this script which I don't think unofficial google has. I know this is a bit much to ask, but can you tell me which part of unofficial google to copy and paste into this script to fix it, just incase later google changed something again? Or is it even possible to copy and paste to make it work? That way I don't have to keep bothering you. :wink:

Thanks much!

:D
Post Reply