# HG changeset patch # User Teemu Piippo # Date 1410001418 -10800 # Node ID 30257c78904f3f7466282f4a7686e8dcbdaf9a90 # Parent ac59b5ae7a78d2945cd140b56427f6bfb193af59 - added urban dictionary lookup diff -r ac59b5ae7a78 -r 30257c78904f cobalt.py --- a/cobalt.py Sat Aug 30 20:30:54 2014 +0300 +++ b/cobalt.py Sat Sep 06 14:03:38 2014 +0300 @@ -583,8 +583,29 @@ else: raise logical_exception ('unknown valuetype, expected one of: degrees, radians (angle conversion), ' + 'celsius, fahrenheit (temperature conversion)') + elif command == 'urban' or command == 'ud': + try: + if len(args) < 1: + raise logical_exception ('usage: %s ' % command) + + url = 'http://api.urbandictionary.com/v0/define?term=%s' % ('%20'.join (args)) + response = urllib2.urlopen (url).read() + data = json.loads (response) + + if 'list' in data and len(data['list']) > 0 and 'word' in data['list'][0] and 'definition' in data['list'][0]: + word = data['list'][0]['word'] + definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') + up = data['list'][0]['thumbs_up'] + down = data['list'][0]['thumbs_down'] + self.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) + else: + self.privmsg (replyto, "couldn't find a definition of \002%s\002" % args[0]) + except logical_exception as e: + raise e + except Exception as e: + raise logical_exception ('Urban dictionary lookup failed: %s' % `e`) # else: -# raise logical_exception ("unknown command `.%s`" % command) +# raiee logical_exception ("unknown command `.%s`" % command) # # Print a ticket announce to appropriate channels