<s> import re <EOL> import requests <EOL> from BeautifulSoup import BeautifulSoup as Soup <EOL> from hippybot . hipchat import HipChatApi <EOL> from hippybot . decorators import directcmd <EOL> try : <EOL> import simplejson as json <EOL> except ImportError : <EOL> import json <EOL> UD_SEARCH_URI = "<STR_LIT>" <EOL> class Plugin ( object ) : <EOL> """<STR_LIT>""" <EOL> global_commands = ( '<STR_LIT>' , ) <EOL> @ directcmd <EOL> def udefine ( self , mess , args ) : <EOL> """<STR_LIT>""" <EOL> self . bot . log . info ( "<STR_LIT>" % mess ) <EOL> term = args . strip ( ) <EOL> req = requests . get ( UD_SEARCH_URI , params = { '<STR_LIT>' : term } ) <EOL> data = req . content <EOL> results = [ ] <EOL> if data : <EOL> data = json . loads ( data . replace ( r'<STR_LIT:\r>' , '<STR_LIT>' ) ) <EOL> if data . get ( '<STR_LIT>' , '<STR_LIT>' ) != '<STR_LIT>' and data . has_key ( '<STR_LIT:list>' ) and len ( data [ '<STR_LIT:list>' ] ) > <NUM_LIT:0> : <EOL> for datum in data [ '<STR_LIT:list>' ] : <EOL> if datum . get ( '<STR_LIT>' , '<STR_LIT>' ) == term : <EOL> definition = datum [ '<STR_LIT>' ] <EOL> re . sub ( r'<STR_LIT>' , '<STR_LIT:U+0020>' , definition ) <EOL> definition = u'<STR_LIT>' . join ( Soup ( definition ) . findAll ( text = True ) ) <EOL> definition = unicode ( Soup ( definition , convertEntities = Soup . HTML_ENTITIES ) ) <EOL> results . append ( definition ) <EOL> if results : <EOL> reply = u"<STR_LIT:\n>" . join ( results ) <EOL> return reply <EOL> else : <EOL> return u'<STR_LIT>' % ( term , ) </s>
