Urban Dictionary script for eggdrop

By FordLawnmower on Jun 17, 2012

Gathers results from urbandictionary.com and returns them to irc.
Triggers:

  • Default trigger is "!" (This can be changed in the setting at the top)
  • Both !slang and !ud will trigger the script.

Syntax:

  • !ud test (returns the first result for test)
  • !ud 15 test (returns the 15th result for test)
  • !ud (returns a random result from urbandictionary.com)

ScreenShot:
Image

Loading:

  • Save this code to a file called urbandictionary.tcl
  • Place the file in your eggdrop scripts directory
  • Add this line to eggdrop.conf -->> source scripts/urbandictionary.tcl
  • Rehash or restart your eggdrop
  • (In the console) type .chanset #channelname +ud "where #channelname is the channel you want the script on in"

See the setup at the top of the script.

##############################################################################################
##  ##  urbandictionary.tcl for eggdrop by Ford_Lawnmower irc.geekshed.net #Script-Help ##  ##
##############################################################################################
## To use this script you must set channel flag +ud (ie .chanset #chan +ud)                 ##
##############################################################################################
##############################################################################################
##  ##                             Start Setup.                                         ##  ##
##############################################################################################
namespace eval urbandictionary {
## Edit logo to change the logo displayed at the start of the line                      ##  ##
  variable logo "\017\00308,07\002UD\017"
## Edit textf to change the color/state of the text shown                               ##  ##
  variable textf "\017\00304"
## Edit linkf to change the color/state of the links                                    ##  ##
  variable linkf "\017\037\00304"
## Edit tagf to change the color/state of the Tags:                                     ##  ##
  variable tagf "\017\002"
## Edit line1, line2, line3, line4 to change what is displayed on each line             ##  ##
## Valid items are: word, definition, example, author, link                             ##  ##
## Do not remove any variables here! Just change them to "" to suppress display         ##  ##
  variable line1 "word"
  variable line2 "definition"
  variable line3 "example"
  variable line4 "author link"
## Edit cmdchar to change the !trigger used to for this script                          ##  ##
  variable cmdchar "!"
##############################################################################################
##  ##                           End Setup.                                              ## ##
##############################################################################################
  setudef flag ud
  bind pub -|- [string trimleft $urbandictionary::cmdchar]ud urbandictionary::main
  bind pub -|- [string trimleft $urbandictionary::cmdchar]slang urbandictionary::main
}
proc urbandictionary::main {nick host hand chan text} {
  if {[lsearch -exact [channel info $chan] +ud] != -1} {
    set text [strip $text]
    set number ""
    set author ""
    set definition ""
    set example ""
    set word ""
    set term ""
    set class ""
    set udurl ""
    set page ""
    set link ""
    set count 1
    set item 1
    if {[regexp {^(?:[\d]{1,}\s)(.*)} $text match term]} {
      set term [urlencode $term]
      #set item [expr {[lindex $text 0] % 7}]
      set item [lindex $text 0]
      if {$item == 0} { set item 7 }
      set page [expr {int(ceil(double([lindex $text 0]) / double(7)))}]
      set page "&page=${page}"
      set udurl "/define.php?term=${term}${page}"
    } elseif {[lindex $text 0] == "redirect"} {
      set term [lindex $text 1]
      set udurl "/define.php?term=${term}"
    } elseif {[lindex $text 0] != "${urbandictionary::cmdchar}ud"} {
      set term [urlencode $text]
      set udurl [iif $term "/define.php?term=${term}" "/random.php"]
      set class "" 
    }
    set udsite "www.urbandictionary.com"
    if {[catch {set udsock [socket -async $udsite 80]} sockerr] && $udurl != ""} {
      return 0
    } else {
      puts $udsock "GET $udurl HTTP/1.0"
      puts $udsock "Host: $udsite"
      puts $udsock ""
      flush $udsock
      while {![eof $udsock]} {
        set udvar " [string map {<![CDATA[ "" ]]> "" \} "" \{ "" \[ "" \] "" \$ ""} [gets $udsock]] "
        if {[string match -nocase "*class=?word?*" $udvar]} {
          set word [striphtml $udvar]
          set class ""
        } elseif {[string match -nocase "*class=?meaning?*" $udvar]} {
          set class "definition"
        } elseif {[string match -nocase "*class=?example?*" $udvar]} {
          set class "example"
        } elseif {[string match -nocase "*ribbon-without-header*" $udvar]} {
          set class "itemnumber"
        } elseif {$class == "itemnumber"} {
          regexp -nocase {(\d+)\.?} $udvar match count
          set class ""
        } elseif {$class == "definition" && $count == $item} {
          if {[regexp -nocase {<div\sclass=["']meaning["']>(.*?)<\/div>} $udvar match definition]} {
            set definition [striphtml $definition]
            if {[regexp -nocase {<div class=["']example["']>(.*)} $udvar match example]} {
              set example [striphtml $example]
              set class "example"
            }
          } else {
            set definition "$definition [striphtml $udvar]"
            if {[string match -nocase "*</div>*" $udvar]} {
              set class ""
            }
          }
        } elseif {$class == "example" && $count == $item} {
          if {[regexp -nocase {<div class=["']example["']>(.*)(?:<\/div>)?} $udvar match example]} {
            regexp -nocase {(.*)<div class=["']example["']>} $udvar match definitionend
            set definition "$definition [striphtml $definitionend]"
            set example [striphtml $example]
          } else {
            set example "$example [striphtml $udvar]"
            if {[string match "*</div>*" $udvar]} { set class "" }
          }
        } elseif {[regexp -nocase {class=["']author["'][^>]*>(.*)<\/a>} $udvar match author] || [regexp -nocase {^\s?by\s([\w]*)} $udvar match author]} {
          if {$count == $item && $term != ""} {
            set wordfix [string map {" " +} [string trimleft [string trimright $word " "] " "]]
            set word "${urbandictionary::tagf}Word: ${urbandictionary::textf}[striphtml $word]"
            set link "${urbandictionary::tagf}Link: ${urbandictionary::linkf}http://www.urbandictionary.com/define.php?term=${wordfix}${page}\017"
            set author "${urbandictionary::tagf}Author: ${urbandictionary::textf}[striphtml $author]"
            set definition "${urbandictionary::tagf}Definition: ${urbandictionary::textf}[striphtml $definition]"
            set example "${urbandictionary::tagf}Example: ${urbandictionary::textf}[striphtml $example]"
            if {$urbandictionary::line1 != ""} {
              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line1 {$\1}]]
            }
            if {$urbandictionary::line2 != ""} {
              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line2 {$\1}]]
            }
            if {$urbandictionary::line3 != ""} {
              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line3 {$\1}]]
            }
            if {$urbandictionary::line4 != ""} {
              msg $chan $urbandictionary::logo $urbandictionary::textf [subst [regsub -all -nocase {(\S+)} $urbandictionary::line4 {$\1}]]
            }
            close $udsock
            return 0
          } else {
            #incr count
          }
        } elseif {[regexp -nocase {Location:\s(.*)} $udvar match redirect]} {
          set udredir ""
          regexp {term\=(.*)} $redirect match udredir
          urbandictionary::main $nick $host $hand $chan "redirect $udredir"
          break
          return 0
        } elseif {[string match -nocase "*<div id='not_defined_yet'>*" $udvar] || [string match -nocase "*</body>*" $udvar]} {
          putserv "PRIVMSG $chan :Nothing found!"
          close $udsock
          return
        }
      }
    }
  }
}
proc urbandictionary::striphtml {string} {
  return [string trimright [string trimleft [dehex [string map {&quot; \" &lt; < &rt; >} [regsub -all {(<[^<^>]*>)} $string ""]]]]]
}
proc urbandictionary::replacestring {string found replace} {
  set found [escape $found]
  putlog "found: $found"
  return [regsub -all $found $string $replace]

}
proc urbandictionary::escape {string} {
  return [subst [regsub -all {([\[\]\(\)\{\}\.\?\:\^])} $string "\\1"]]
}
proc urbandictionary::iif {test do elsedo} {
   if {$test != 0 && $test != ""} {
     return $do
   } else {
     return "$elsedo"
   }
}
proc urbandictionary::urlencode {string} {
  set string [string trimleft [string trimright $string]]
  return [subst [regsub -nocase -all {([^a-z0-9])} $string {%[format %x [scan "\\&" %c]]}]]
}
proc urbandictionary::strip {text} {
  regsub -all {\002|\031|\015|\037|\017|\003(\d{1,2})?(,\d{1,2})?} $text "" text
    return $text
}
proc urbandictionary::msg {chan logo textf text} {
  set text [textsplit $text 50]
  set counter 0
  while {$counter <= [llength $text]} {
    if {[lindex $text $counter] != ""} {
      putserv "PRIVMSG $chan :${logo} ${textf}[string map {\\\" \"} [lindex $text $counter]]"
    }
    incr counter
  }
}
proc urbandictionary::textsplit {text limit} {
  set text [split $text " "]
  set tokens [llength $text]
  set start 0
  set return ""
  while {[llength [lrange $text $start $tokens]] > $limit} {
    incr tokens -1
    if {[llength [lrange $text $start $tokens]] <= $limit} {
      lappend return [join [lrange $text $start $tokens]]
      set start [expr $tokens + 1]
      set tokens [llength $text]
    }
  }
  lappend return [join [lrange $text $start $tokens]]
  return $return
}
proc urbandictionary::hex {decimal} { return [format %x $decimal] }
proc urbandictionary::decimal {hex} { return [expr 0x$hex] }
proc urbandictionary::dehex {string} {
  regsub -all {^\{|\}$} $string "" string
  set string [subst [regsub -nocase -all {\\u([a-f0-9]{4})} $string {[format %c [decimal \1]]}]]
  set string [subst [regsub -nocase -all {\%([a-f0-9]{2})} $string {[format %c [decimal \1]]}]]
  set string [subst [regsub -nocase -all {\&#([0-9]{2});} $string {[format %c \1]}]]
  set string [subst [regsub -nocase -all {\&#x([0-9]{2});} $string {[format %c [decimal \1]]}]]
  set string [string map {&quot; \" &middot; · &amp; & <b> \002 </b> \002} $string]
  return $string
}
putlog "\002*Loaded* \00308,07\002UrbanDictionary\002\003 v1.02 \002by Ford_Lawnmower irc.GeekShed.net #Script-Help"

Comments

Sign in to comment.
Josefius   -  Oct 08, 2015

I am getting this error when using !ud or !slang : [11:27:33] Tcl error [urbandictionary::main]: invalid command name "sub"

System:
OS: OpenBSD 5.7
Tcl library: /usr/local/lib/tcl/tcl8.5
Tcl version: 8.5.16 (header version 8.5.16)

Maybe something fubared when I copied over the text?

 Respond  
FordLawnmower   -  Nov 05, 2014

Updated

Hawkee  -  Nov 05, 2014

Good to see you keeping things up to date Ford!

Sign in to comment

play4free2   -  Jan 27, 2014

@FordLawnmower They just changed the website again. Definition is not showing, the rest seems to be fine.

I did get it to work by replacing definition with meaning on lines: 81 82 90 and the first definition on line 91.

Not sure if this is correct... I just started writing .tcl but it worked with no errors I can see.

FordLawnmower  -  Jan 29, 2014

Thanks @play4free2 . The script is updated and working again.

Sign in to comment

prashant179   -  Jan 12, 2014

Mr. FordLawnmower ~ your Script not work Proper See Below Image i tag it.... i hope toy Fix it Soon!

FordLawnmower  -  Jan 13, 2014

Updated

prashant179  -  Jan 14, 2014

Mr. FordLawnmower ~ Huge Thanksyou :D

Sign in to comment

FordLawnmower   -  Oct 24, 2013

Updated

 Respond  
FordLawnmower   -  Aug 20, 2013

Updated

 Respond  
FordLawnmower   -  Jan 30, 2013

Updated to fix a &#x bug

 Respond  
FordLawnmower   -  Jun 17, 2012

Updated again to fix a bug with the results.

 Respond  
FordLawnmower   -  Jun 17, 2012

Updated because some of the examples were blending into the definition.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.