|
1 import math |
|
2 import urllib2 |
|
3 from modulecore import command_error |
|
4 |
|
5 ModuleData = { |
|
6 'commands': |
|
7 [ |
|
8 { |
|
9 'name': 'convert', |
|
10 'description': 'Performs numeric conversion', |
|
11 'args': '<value> as <valuetype>', |
|
12 'level': 'normal', |
|
13 }, |
|
14 |
|
15 { |
|
16 'name': 'ud', |
|
17 'description': 'Looks up a term in urban dictionary', |
|
18 'args': '<term...>', |
|
19 'level': 'normal', |
|
20 }, |
|
21 ] |
|
22 } |
|
23 |
|
24 def cmd_convert (bot, args, replyto, **rest): |
|
25 value = float (args['value']) |
|
26 valuetype = args['valuetype'] |
|
27 |
|
28 if valuetype in ['radians', 'degrees']: |
|
29 if valuetype == 'radians': |
|
30 radvalue = value |
|
31 degvalue = (value * 180.) / math.pi |
|
32 else: |
|
33 radvalue = (value * math.pi) / 180. |
|
34 degvalue = value |
|
35 |
|
36 bot.privmsg (replyto, '%s radians, %s degrees (%s)' % |
|
37 (radvalue, degvalue, degvalue % 360.)) |
|
38 return |
|
39 |
|
40 if valuetype in ['celsius', 'fahrenheit']: |
|
41 if valuetype == 'celsius': |
|
42 celvalue = value |
|
43 fahrvalue = value * 1.8 + 32 |
|
44 else: |
|
45 celvalue = (value - 32) / 1.8 |
|
46 fahrvalue = value |
|
47 |
|
48 bot.privmsg (replyto, '%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) |
|
49 return |
|
50 |
|
51 command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + |
|
52 'celsius, fahrenheit (temperature conversion)' % valuetype) |
|
53 |
|
54 def cmd_ud (bot, args, replyto, **rest): |
|
55 try: |
|
56 url = 'http://api.urbandictionary.com/v0/define?term=%s' % ('%20'.join (args)) |
|
57 response = urllib2.urlopen (url).read() |
|
58 data = json.loads (response) |
|
59 |
|
60 if not 'list' in data \ |
|
61 or len(data['list']) == 0 \ |
|
62 or not 'word' in data['list'][0] \ |
|
63 or not 'definition' in data['list'][0]: |
|
64 command_error ("couldn't find a definition of \002%s\002" % args['term']) |
|
65 |
|
66 word = data['list'][0]['word'] |
|
67 definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') |
|
68 up = data['list'][0]['thumbs_up'] |
|
69 down = data['list'][0]['thumbs_down'] |
|
70 bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % |
|
71 (word, definition, up, down)) |
|
72 except Exception as e: |
|
73 command_error ('Urban dictionary lookup failed: %s' % e) |