Code: Select all
set vEtymologyMaxWords 40
package require http
setudef flag etymology
set vEtymologyTimeout 10
set vEtymologyUrl http://www.etymonline.com/index.php
bind PUB - !etymology pEtymologySearch
proc pEtymologySearch {nick uhost hand channel txt} {
global vEtymologyTimeout vEtymologyUrl vEtymologyMaxWords
if {[channel get $channel etymology]} {
set arguments [string trim $txt]
if {[llength [split $arguments]] == 1} {
set agent [::http::config -useragent "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"]
if {![catch {set http [::http::geturl $vEtymologyUrl\?search=$txt -timeout [expr {$vEtymologyTimeout * 1000}]]}]} {
switch -- [::http::status $http] {
"timeout" {putserv "PRIVMSG $channel :attempt to scrape $vEtymologyUrl timed out after $vEtymologyTimeout seconds"}
"error" {putserv "PRIVMSG $channel :attempt to scrape $vEtymologyUrl returned error [::http::error $http]"}
"ok" {
switch -- [::http::ncode $http] {
200 {
regexp -- {<dd class=\"highlight\">(.+?)</dd>} [::http::data $http] -> data
if {[info exists data]} {
set data [regsub -all -- {<span class=\"foreign\">([^<]*?)</span>} $data "\00305\\1\003"]
set data [regsub -all -- {\(see.*<a href=\"/index\.php([^"]*?)\".*\)} $data "\[see \00312$vEtymologyUrl\\1\003\]" ]
set data [regsub -all -- {\(([^)]*?)\)} $data \(\00314\\1\003\)]
set char \00303[encoding convertto utf-8 \u25CF]\003
set data "\00304$txt\003 [join [lrange [split $data] 0 [expr {$vEtymologyMaxWords - 1}]]] [string repeat $char 5]"
putserv "PRIVMSG $channel :$data"
putserv "PRIVMSG $channel :$vEtymologyUrl\?search=$txt"
} else {putserv "PRIVMSG $channel :$txt not found at $vEtymologyUrl"}
}
default {putserv "PRIVMSG $channel :attempt to scrape $vEtymologyUrl returned ncode [::http::ncode $http]"}
}
}
}
::http::cleanup $http
} else {putserv "PRIVMSG $channel :attempted connection to $vEtymologyUrl failed"}
} else {putserv "PRIVMSG $channel :correct syntax is !etymology <word>"}
}
return 0
}