Wikepedia Search
Platform: TCL
Published Jun 22, 2012
Updated Jun 22, 2012
Searches Wikipedia and displays result for search.
Based on FordLawnmower's version
bind pub - !wiki wiki
proc wiki {nick host hand chan arg} {
package require http
set arg [string map {{ } \%20} $arg]
set url "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=large&q=$arg+site:en.wikipedia.org"
set page [http::data [http::geturl $url]]
if {[regexp {\"url\":\"([^\"]*)\"} $page a link]} {
if {[regexp {wikipedia\.org} $link]} { putserv "PRIVMSG $chan :[wikipedia $link]" }
} else { putserv "PRIVMSG $chan :Nothing found on Wikipedia! Please refine your search and check your spelling." }
}
proc wikipedia {url} {
package require http
set page [http::data [http::geturl $url]]
if {[regexp {may refer to:} $page] || [regexp {HTTP\/1\.0 403} $page]} {
return "Nothing found on Wikipedia! Please refine your search and check your spelling."
} elseif {[regexp -line {<b>(.*)</p>} $page a result] || [regexp -line {<p>(.*)</p>} $page a result]} {
set result [dehex [strip $result]]
set result [shorten $result 50]
return "Wikipedia: $result : $url"
}
}
proc shorten {text limit} {
foreach i $text { incr start
lappend wiki $i
if {$start == $limit} { return $wiki }
}
return $wiki
}
proc dehex {string} {
regsub -all {[\[\]]} $string "" string
set string [subst [regsub -nocase -all {\&#([0-9]{3});} $string {[format %c \1]}]]
return [string map {&quo\te; \"} $string]
}
proc strip {string} {
regsub -all {<[^<>]+>} $string "" string
regsub -all {\[\d+\]} $string "" string
return $string
}
putlog "WikiPedia Search"