mod_util.py

changeset 76
a2fe9ba3041a
parent 73
d67cc4fbc3f1
child 102
2bad379cd416
equal deleted inserted replaced
75:1818d56c8ff5 76:a2fe9ba3041a
1 import math 1 import math
2 import urllib2 2 import urllib2
3 import json
3 from modulecore import command_error 4 from modulecore import command_error
4 5
5 ModuleData = { 6 ModuleData = {
6 'commands': 7 'commands':
7 [ 8 [
51 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + 52 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' +
52 'celsius, fahrenheit (temperature conversion)' % valuetype) 53 'celsius, fahrenheit (temperature conversion)' % valuetype)
53 54
54 def cmd_ud (bot, args, replyto, **rest): 55 def cmd_ud (bot, args, replyto, **rest):
55 try: 56 try:
56 url = 'http://api.urbandictionary.com/v0/define?term=%s' % ('%20'.join (args)) 57 url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20'))
57 response = urllib2.urlopen (url).read() 58 response = urllib2.urlopen (url).read()
58 data = json.loads (response) 59 data = json.loads (response)
59 60
60 if not 'list' in data \ 61 if not 'list' in data \
61 or len(data['list']) == 0 \ 62 or len(data['list']) == 0 \
65 66
66 word = data['list'][0]['word'] 67 word = data['list'][0]['word']
67 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') 68 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ')
68 up = data['list'][0]['thumbs_up'] 69 up = data['list'][0]['thumbs_up']
69 down = data['list'][0]['thumbs_down'] 70 down = data['list'][0]['thumbs_down']
70 bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % 71 bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down))
71 (word, definition, up, down))
72 except Exception as e: 72 except Exception as e:
73 command_error ('Urban dictionary lookup failed: %s' % e) 73 command_error ('Urban dictionary lookup failed: %s' % e)

mercurial