All pastes #984004 Raw Edit

Unnamed

public text v1 · immutable
·published 2008-04-13 20:45 UTC
rendered paste body
#---------------------------------------------------------------------#
# TechJoose:WordoftheDay                                         v1.0 #
# Based on perpleXa's Urban Dictionary Script                         #
#    Original script Copyright (C) 2006-2007 perpleXa                 #
#    http://perplexa.ugug.org / #perpleXa on QuakeNet                 #
#                                                                     #
# Grabs the word of the day from Urban Dictionary                     #
#                                                                     #
# Usage:                                                              #
#   !wordoftheday [id] (1=today, 2=yesterday, etc up to 100)          #
#                                                                     #
# ChangeLog:                                                          #
#   1.0  - Modified perpleXa's original script, released              #
#                                                                     #
# TODO:                                                               #
#   - Ideas? Email me.                                                #
#                                                                     #
# http://www.techjoose.net                                            #
# macbrando@gmail.com                                                 #
# BeBoo @ irc.techjoose.net / #bar                                    #
#---------------------------------------------------------------------#

#---------------------------------------------------------------------#
# Redistribution, with or without modification, are permitted provided#
# that redistributions retain the above copyright notice, this        #
# condition and the following disclaimer.                             #
#                                                                     #
# This program is distributed in the hope that it will be useful,     #
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without   #
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A       #
# PARTICULAR PURPOSE.                                                 #
#---------------------------------------------------------------------#
# fsck is available at http://perplexa.ugug.co.uk

package require fsck 1.10;
package require http;

namespace eval urbandict_wotd {
  variable version 1.0;
  variable encoding "utf-8";
  variable client "Mozilla/5.0 (compatible; Y!J; for robot study; keyoshid)";
  bind pub -|- "!word" [namespace current]::pub;
  namespace export pub;
}

proc urbandict_wotd::getdefinition {url} {
  variable client;
  http::config -useragent $client;
  if {[catch {http::geturl $url -timeout 20000} token]} {
    return [list 0 "Warning: Couldn't connect to \[$url\]"];
  }
  upvar 0 $token state;
  if {![string equal -nocase $state(status) "ok"]} {
    return [list 0 "Warning: Couldn't connect to \[$url\] (connection $state(status))."];
  }
  set data [http::data $token];
  http::cleanup $token;
  set matches [regexp -all -inline {<td class=\"def_word\" nowrap><span.*?>(.*?)</span>.*?<td class=\"def_word\"><a.*?>(.*?)</a></td>.*?<div class=\"def_p\">.*?<p>(.*?)</p>} $data];
  set list [list];
  foreach {null date word definition} $matches {
    regsub -all {<[^>]*?>} [decode $definition] "" definition;
    regsub -all {[\r\n\s\t]+} $definition " " definition;
    regsub -all {<[^>]*?>} [decode $word] "" word;
    regsub -all {[\r\n\s\t]+} $word " " word;
    lappend list "\002\037${date}\037, ${word}\002 - ${definition}";
  }
  return [concat [llength $list] $list];
}

proc urbandict_wotd::urlencode {i} {
  variable encoding
  set index 0;
  set i [encoding convertto $encoding $i]
  set length [string length $i]
  set n ""
  while {$index < $length} {
    set activechar [string index $i $index]
    incr index 1
    if {![regexp {^[a-zA-Z0-9]$} $activechar]} {
      append n %[format "%02X" [scan $activechar %c]]
    } else {
      append n $activechar
    }
  }
  return $n
}

proc urbandict_wotd::pub {nick host hand chan argv} {
set id [lindex $argv 0];
if {$id eq ""} {
    set id 1;
} elseif {$id eq 0} {
    set id 1;
}
if {$id < 11} {
set url "http://www.urbandictionary.com/daily.php?page=1";
} elseif {$id < 21} {
set id [expr ${id}-10]
set url "http://www.urbandictionary.com/daily.php?page=2";
} elseif {$id < 31} {
set id [expr ${id}-20]
set url "http://www.urbandictionary.com/daily.php?page=3";
} elseif {$id < 41} {
set id [expr ${id}-30]
set url "http://www.urbandictionary.com/daily.php?page=4";
} elseif {$id < 51} {
set id [expr ${id}-40]
set url "http://www.urbandictionary.com/daily.php?page=5";
} elseif {$id < 61} {
set id [expr ${id}-50]
set url "http://www.urbandictionary.com/daily.php?page=6";
} elseif {$id < 71} {
set id [expr ${id}-60]
set url "http://www.urbandictionary.com/daily.php?page=7";
} elseif {$id < 81} {
set id [expr ${id}-70]
set url "http://www.urbandictionary.com/daily.php?page=8";
} elseif {$id < 91} {
set id [expr ${id}-80]
set url "http://www.urbandictionary.com/daily.php?page=9";
} elseif {$id < 101} {
set id [expr ${id}-90]
set url "http://www.urbandictionary.com/daily.php?page=10";
} else {
puthelp "PRIVMSG $chan :\002\00305\[\003 UrbanDict's Word of the Day\003 \00305\]\003\002 Too far back, sorry.";
return 1;
}
  set definition [getdefinition $url];
  set count [lindex $definition 0];
  if {!$count} {
    puthelp "PRIVMSG $chan :\002\00305\[\003 UrbanDict's Word of the Day\003 \00305\]\003\002 Nothing found for the word of the day.";
    return 1;
  }
  set definition [lindex $definition $id];
  if {[string length $definition] <= 400} {
    puthelp "PRIVMSG $chan :\002\00305\[\003 UrbanDict's Word of the Day\003 \00305\]\003\002 $definition";
    return 0;
  }
  set skip 0;
  set definition [splitmsg $definition];
  set required_lines [llength $definition];
  foreach line $definition {
    puthelp "PRIVMSG $chan :\002\00305\[\003 UrbanDict's Word of the Day\003 \00305\]\003\002 $line";
    if {[incr skip] == 3 && $required_lines != 3} {
      puthelp "PRIVMSG $chan :\002\00305\[\003 UrbanDict's Word of the Day\003 \00305\]\003\002 Truncated output to 3 of $required_lines lines.";
      break;
    }
  }
  return 0;
}

proc urbandict_wotd::decode {content} {
  if {![string match *&* $content]} {
    return $content;
  }
  set escapes {
    &nbsp; \x20 &quot; \x22 &amp; \x26 &apos; \x27 &ndash; \x2D
    &lt; \x3C &gt; \x3E &tilde; \x7E &euro; \x80 &iexcl; \xA1
    &cent; \xA2 &pound; \xA3 &curren; \xA4 &yen; \xA5 &brvbar; \xA6
    &sect; \xA7 &uml; \xA8 &copy; \xA9 &ordf; \xAA &laquo; \xAB
    &not; \xAC &shy; \xAD &reg; \xAE &hibar; \xAF &deg; \xB0
    &plusmn; \xB1 &sup2; \xB2 &sup3; \xB3 &acute; \xB4 &micro; \xB5
    &para; \xB6 &middot; \xB7 &cedil; \xB8 &sup1; \xB9 &ordm; \xBA
    &raquo; \xBB &frac14; \xBC &frac12; \xBD &frac34; \xBE &iquest; \xBF
    &Agrave; \xC0 &Aacute; \xC1 &Acirc; \xC2 &Atilde; \xC3 &Auml; \xC4
    &Aring; \xC5 &AElig; \xC6 &Ccedil; \xC7 &Egrave; \xC8 &Eacute; \xC9
    &Ecirc; \xCA &Euml; \xCB &Igrave; \xCC &Iacute; \xCD &Icirc; \xCE
    &Iuml; \xCF &ETH; \xD0 &Ntilde; \xD1 &Ograve; \xD2 &Oacute; \xD3
    &Ocirc; \xD4 &Otilde; \xD5 &Ouml; \xD6 &times; \xD7 &Oslash; \xD8
    &Ugrave; \xD9 &Uacute; \xDA &Ucirc; \xDB &Uuml; \xDC &Yacute; \xDD
    &THORN; \xDE &szlig; \xDF &agrave; \xE0 &aacute; \xE1 &acirc; \xE2
    &atilde; \xE3 &auml; \xE4 &aring; \xE5 &aelig; \xE6 &ccedil; \xE7
    &egrave; \xE8 &eacute; \xE9 &ecirc; \xEA &euml; \xEB &igrave; \xEC
    &iacute; \xED &icirc; \xEE &iuml; \xEF &eth; \xF0 &ntilde; \xF1
    &ograve; \xF2 &oacute; \xF3 &ocirc; \xF4 &otilde; \xF5 &ouml; \xF6
    &divide; \xF7 &oslash; \xF8 &ugrave; \xF9 &uacute; \xFA &ucirc; \xFB
    &uuml; \xFC &yacute; \xFD &thorn; \xFE &yuml; \xFF
  };
  set content [string map $escapes $content];
  set content [string map [list "\]" "\\\]" "\[" "\\\[" "\$" "\\\$" "\\" "\\\\"] $content];
  regsub -all -- {&#([[:digit:]]{1,5});} $content {[format %c [string trimleft "\1" "0"]]} content;
  regsub -all -- {&#x([[:xdigit:]]{1,4});} $content {[format %c [scan "\1" %x]]} content;
  regsub -all -- {&#?[[:alnum:]]{2,7};} $content "?" content;
  return [subst $content];
}

putlog " - Urban Dictionary: Word of the Day v$urbandict_wotd::version by BeBoo";