Bing.tcl – Bing search TCL script

After doing a basic Bing search TCL script, I’ve made a new TCL (Tool command language) script. This version returns the actual results from Bing to the channel.

I don’t have much experience with TCL scripting but if you have found a better way to do the searching or parsing, please let me know.

The script can be used in your eggdrop or windrop.

bing.tcl :

# ----------------------------------
# Bing.com search script version 0.1
# teusje.wordpress.com
#
# v0.1
# Returns the results into the chan
# ----------------------------------

# ------------
# START CONFIG
# ------------
bind pub - !bing pub_bing
# Uncomment if you want to play it dirty
# bind pub - !search pub_bing
# bind pub - !find pub_bing
# bind pub - !google pub_bing

set bing(MAXRRESULTS) 3
set bing(deny_CHANS) {
	#channel1
	#channel2
}
# ------------
# END CONFIG
# ------------

# ------------
# Do not change anymore
# ------------
set bing(VERSION) 0.1
package require http

proc html_replace {quotes} {
	global bing
	regsub -all {‘|‘} $quotes "\`" quotes
	regsub -all {’|’} $quotes "\'" quotes
	# remove bing's <strong></strong> in description
	regsub -all -nocase -- {<strong>|</strong>} $quotes "" quotes
	foreach index [lsort -decreasing [regexp -all -indices -inline {&#[0-9]+;} $quotes]] {
		set binary_value [string range $quotes [lindex $index 0] [lindex $index 1]]
		regexp {[0-9]+} $binary_value match
		set quotes [string replace $quotes [lindex $index 0] [lindex $index 1] [format %c $match]]
	}
	return $quotes
}

proc pub_bing {nick userhost handle channel rest} {
	global bing
	if {[lsearch [string tolower $bing(deny_CHANS)] [string tolower $channel]] != "-1"} {
		return 0
	}
	if {$rest==""} {
		putserv "PRIVMSG $channel :Syntax: !bing <search_terms>"
		return 0
	}
	search_bing $channel $bing(MAXRRESULTS) $rest
}

proc search_bing {channel results search} {
	global bing
	regsub -all " " $search "+" search
	set url http://www.bing.com/search?q=$search
	http::config -useragent "Mozilla/5.0"
	set conn [http::geturl $url -headers "Referer http://www.bing.com"]
	set data [http::data $conn]
	set count 0
	regsub -all {<b>|</b>} $data \002 data
	foreach {match parens1 parens2} [regexp -all -inline -- {<div class="sb_tlst">.*?<h3>.*?<a href="(.*?)" .*?>(.*?)</a>.*?</h3>} $data] {
		set parens2 [html_replace $parens2]
		if {$count==$bing(MAXRRESULTS)} { break }
		incr count 1
		if {$bing(MAXRRESULTS)>1} {
			putserv "PRIVMSG $channel :Result($count): \002$parens1\002 ($parens2)"
		} else {
			putserv "PRIVMSG $channel :Result: \002$parens1\002 ($parens)"
			break
		}
	}
	if {$count!=$bing(MAXRRESULTS)} {
		putserv "PRIVMSG $channel : \002$count\002 results found"
	}
}

putlog "Bing script $bing(VERSION) - teusje.wordpress.com"

Usage:

<nickname> !bing powershell
<bot> Result(1): http://blogs.msdn.com/powershell/ (Windows PowerShell Blog)
<bot> Result(2): http://www.pwrshell.net/ (Free PowerShell tutorials, scripts, resources and files)
<bot> Result(3): http://blogs.technet.com/powershell (PowerShell sur Technet et en fran├žais)

Enjoy! :D

# ———————————-
# Bing.com search script version 0.1
# teusje.wordpress.com
#
# v0.1
# Returns the results into the chan
# ———————————-

# ————
# START CONFIG
# ————
bind pub – !bing pub_bing
# Uncomment if you want to play it dirty
# bind pub – !search pub_bing
# bind pub – !find pub_bing
# bind pub – !google pub_bing

set bing(MAXRRESULTS) 3
set bing(deny_CHANS) {
#channel1
#channel2
}
# ————
# END CONFIG
# ————

# ————
# Do not change anymore
# ————
set bing(VERSION) 0.1
package require http

proc html_replace {quotes} {
global bing
regsub -all {‘|&lsquo;} $quotes “\`” quotes
regsub -all {’|&rsquo;} $quotes “\’” quotes
# remove bing’s <strong></strong> in description
regsub -all -nocase — {<strong>|</strong>} $quotes “” quotes
foreach index [lsort -decreasing [regexp -all -indices -inline {&#[0-9]+;} $quotes]] {
set binary_value [string range $quotes [lindex $index 0] [lindex $index 1]]
regexp {[0-9]+} $binary_value match
set quotes [string replace $quotes [lindex $index 0] [lindex $index 1] [format %c $match]]
}
return $quotes
}

proc pub_bing {nick userhost handle channel rest} {
global bing
if {[lsearch [string tolower $bing(deny_CHANS)] [string tolower $channel]] != “-1″} {
return 0
}
if {$rest==””} {
putserv “PRIVMSG $channel :Syntax: !bing <search_terms>”
return 0
}
search_bing $channel $bing(MAXRRESULTS) $rest
}

proc search_bing {channel results search} {
global bing
regsub -all ” ” $search “+” search
set url http://www.bing.com/search?q=$search
http::config -useragent “Mozilla/5.0″
set conn [http::geturl $url -headers "Referer http://www.bing.com"]
set data [http::data $conn]
set count 0
regsub -all {<b>|</b>} $data 02 data
foreach {match parens1 parens2} [regexp -all -inline -- {<div class="sb_tlst">.*?<h3>.*?<a href="(.*?)" .*?>(.*?)</a>.*?</h3>} $data] {
set parens2 [html_replace $parens2]
if {$count==$bing(MAXRRESULTS)} { break }
incr count 1
if {$bing(MAXRRESULTS)>1} {
putserv “PRIVMSG $channel :Result($count): 02$parens102 ($parens2)”
} else {
putserv “PRIVMSG $channel :Result: 02$parens102 ($parens)”
break
}
}
if {$count!=$bing(MAXRRESULTS)} {
putserv “PRIVMSG $channel : 02$count02 results found”
}
}

putlog “Bing script $bing(VERSION) – teusje.wordpress.com”