Skip to content

Instantly share code, notes, and snippets.

@e000
Created January 20, 2011 04:07
Show Gist options
  • Save e000/787373 to your computer and use it in GitHub Desktop.
Save e000/787373 to your computer and use it in GitHub Desktop.
ud module
from Core.ZalgoBase.BaseModule import BaseModule # I need this, it's the base module to extend off of.
from twisted.web.client import getPage # asynchronous page getter
from twisted.internet.defer import inlineCallbacks # write async code as synchronous code
from urllib import urlencode # escape url args
from Core.ZalgoUtil.ModUtil import xhtml_unescape as unescape, utf8 # turn xhtml elements into actual characters, convert utf8 to string to send to irc
from Core.ZalgoUtil.CmdWraps import stripColors, needsArg, timeout, command # convenicnce decorators
from Core.ZalgoUtil.DeferredCache import deferred_lfu_cache # cache the results from UD for a while.
import re
class Module(BaseModule):
parseCommands = True # tell it to parse module for @command decorators
prefix = "\x02\x034,01[\x037U\x038rban\x037D\x038ictionary\x034]"
_rmRegex = re.compile(r'[\r\n\t]')
_match = re.compile("<div><b>(.*?)<\/b><\/div><div>(.*?)<\/div>")
@deferred_lfu_cache(maxsize=15)
def getDefinition(self, term):
def gotDefinition(result):
if not result or 'Service Temporarily Unavailable' in result:
raise # if I raise an exception, the cache will not cache the results. handy if we don't have anything to cache.
elif "isn't defined yet." in result:
return term, None
else:
return [utf8(unescape(e).replace('<b>', '\x02').replace('</b>', '\x02'))
for e in
self._match.match(
self._rmRegex.sub(
'', result.replace("<br/>", " ").strip()
)
).groups()]
return getPage('http://www.urbandictionary.com/tooltip.php?%s' % urlencode({'term': term})).addCallback(gotDefinition)
@command('!ud') # this function is a command that will be called when "!ud" is said.
@stripColors() # strip colors from the line before calling function
@needsArg() # i need at least one argument, (non blank line)
@timeout(1) # only allow this command to be called once per second.
@inlineCallbacks
def ud(self, cmd, user, channel, line, params):
"""
@syntax: %(command)s [term]
@description: Looks stuff up on Urban Dictionary
""" # docstr to the actual command help.
try:
(query, result), isCached = yield self.getDefinition(line)
if not result:
self.bot.privmsg(channel, '%s \x0313%s\x034 is not defined yet.' % (self.prefix, line))
else:
self.bot.privmsg(channel, '%s \x0313%s\x0310 --> \x0307%s' % (self.prefix, query, result)) # turn into string and send.
except:
self.bot.privmsg(channel, '%s Service Unavailable -- Try again later!' % self.prefix)
# below here are just some debug related features.
@command('.urbandictionary-cachestats', level = 1)
def stats(self, cmd, user, channel, line, params):
f = self.getDefinition
self.bot.privmsg(channel,
"[\x02UrbanDictionary\x02] deferred_lfu_cache hits: %s, misses: %s, size: %s, waiting: %s, maxsize: %s" %
(f.hits, f.misses, f.size(), f.waiting(), f.maxsize))
@command('.urbandictionary-cacheclear', level = 1)
def clear(self, cmd, user, channel, line, params):
self.getDefinition.clear()
self.bot.privmsg(channel, "[\x02UrbanDictionary\x02] deferred_lfu_cache cleared.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment