Sat, 06 Sep 2014 14:03:38 +0300
- added urban dictionary lookup
cobalt.py | file | annotate | diff | comparison | revisions |
--- 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 <word>' % 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