Code: Select all
bind pub - .google pub_google
set google(MAX) 10
set google(deny_CHANS) {
"#chan1"
"#chan2"
}
package require http
proc pub_google {nick uhost hand chan arg} {
global google
if {[lsearch [string tolower $google(deny_CHANS)] [string tolower $chan]]!="-1"} {
return 0
}
if {$arg==""} {
putserv "PRIVMSG $chan :$nick Usage: !google <search terms> \[-c1(Results to return)\] \[-a(Boolean And)\]"
return 0
}
set type as_oq
set results 1
foreach flag $arg {
if {$flag=="-a"} { set type as_q ; continue } else {
if {[regexp -- {-c[0-9]*} $flag]} {
set results [string range $flag 2 end]
continue
}
}
lappend search $flag
}
if {$results>$google(MAX)} {
putnot $nick "Max results allowed is \002$google(MAX)\002, omitting extra"
set results $google(MAX)
}
search_google $chan $results $type $search
}
proc search_google {chan results type search} {
regsub -all " " $search "+" search
set url http://www.google.com/search?$type=$search&num=$results
http::config -useragent "Mozilla/5.0"
set conn [http::geturl $url -headers "Referer http://www.google.com"]
set data [http::data $conn]
set count 0
set lcount 0
regsub -all .*<div> $data "" data
regsub -all <div> $data <br> data
regsub -all <br> $data \002 data
foreach line [split $data \002] {
incr lcount
# putlog "$lcount: $line"
if {$count==$results} { break }
set line2 "" ; set line1 ""
if {[regexp {(.*)<p [^>]*>(.*)} $line match line1 line2]} {
if {$line2==""} { break }
set description ""
set desc ""
regsub -all {<[^>]*>} $line2 "" description
if {$description!=""} {
set desc "($description)"
}
foreach word $line2 {
if {[string match href=http://* [string tolower $word]]} {
regsub -all {>[^<]*|<[^<]*|href=} $word "" word
if {$results>1} {
putserv "PRIVMSG $chan :Result([expr $count + 1]): \002$word\002 $desc"
} else { putserv "PRIVMSG $chan :Result: \00304\002$word\002\003 \00302$desc\003" }
incr count 1
break
}
}
}
}
if {$count!=$results} {
putserv "PRIVMSG $chan :Only could find \00302\002$count\002 results\003"
}
}