Mon, 08 Dec 2014 16:12:51 -0500
- now in color!
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
1 | import math |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
2 | import urllib2 |
76 | 3 | import json |
105 | 4 | import subprocess |
5 | import re | |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
6 | from modulecore import command_error |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
7 | import modulecore as ModuleCore |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
8 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
9 | ModuleData = { |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
10 | 'commands': |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
11 | [ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
12 | { |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
13 | 'name': 'convert', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
14 | 'description': 'Performs numeric conversion', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
15 | 'args': '<value> as <valuetype>', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
16 | 'level': 'normal', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
17 | }, |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
18 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
19 | { |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
20 | 'name': 'ud', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
21 | 'description': 'Looks up a term in urban dictionary', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
22 | 'args': '<term...>', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
23 | 'level': 'normal', |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
24 | }, |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
25 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
26 | { |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
27 | 'name': 'commands', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
28 | 'description': 'Lists commands available to the calling user', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
29 | 'args': None, |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
30 | 'level': 'normal', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
31 | }, |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
32 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
33 | { |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
34 | 'name': 'help', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
35 | 'description': 'Prints help about a given command', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
36 | 'args': '<command>', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
37 | 'level': 'normal', |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
38 | }, |
105 | 39 | |
40 | { | |
41 | 'name': 'calc', | |
42 | 'description': 'Calculates a mathematical expression using apcalc', | |
43 | 'args': '<expression...>', | |
44 | 'level': 'normal', | |
45 | }, | |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
46 | ] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
47 | } |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
48 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
49 | def cmd_convert (bot, args, replyto, **rest): |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
50 | value = float (args['value']) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
51 | valuetype = args['valuetype'] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
52 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
53 | if valuetype in ['radians', 'degrees']: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
54 | if valuetype == 'radians': |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
55 | radvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
56 | degvalue = (value * 180.) / math.pi |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
57 | else: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
58 | radvalue = (value * math.pi) / 180. |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
59 | degvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
60 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
61 | bot.privmsg (replyto, '%s radians, %s degrees (%s)' % |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
62 | (radvalue, degvalue, degvalue % 360.)) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
63 | return |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
64 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
65 | if valuetype in ['celsius', 'fahrenheit']: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
66 | if valuetype == 'celsius': |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
67 | celvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
68 | fahrvalue = value * 1.8 + 32 |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
69 | else: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
70 | celvalue = (value - 32) / 1.8 |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
71 | fahrvalue = value |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
72 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
73 | bot.privmsg (replyto, '%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
74 | return |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
75 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
76 | command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
77 | 'celsius, fahrenheit (temperature conversion)' % valuetype) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
78 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
79 | def cmd_ud (bot, args, replyto, **rest): |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
80 | try: |
76 | 81 | url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20')) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
82 | response = urllib2.urlopen (url).read() |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
83 | data = json.loads (response) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
84 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
85 | if not 'list' in data \ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
86 | or len(data['list']) == 0 \ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
87 | or not 'word' in data['list'][0] \ |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
88 | or not 'definition' in data['list'][0]: |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
89 | command_error ("couldn't find a definition of \002%s\002" % args['term']) |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
90 | |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
91 | word = data['list'][0]['word'] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
92 | definition = data['list'][0]['definition'].replace ('\r', ' ').replace ('\n', ' ').replace (' ', ' ') |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
93 | up = data['list'][0]['thumbs_up'] |
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
94 | down = data['list'][0]['thumbs_down'] |
76 | 95 | bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) |
73
d67cc4fbc3f1
- modularization complete!!
Teemu Piippo <crimsondusk64@gmail.com>
parents:
diff
changeset
|
96 | except Exception as e: |
102
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
97 | command_error ('Urban dictionary lookup failed: %s' % e) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
98 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
99 | def cmd_commands (bot, replyto, ident, host, **rest): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
100 | commandlist = ModuleCore.get_available_commands (ident, host) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
101 | partitioned=[] |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
102 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
103 | while len (commandlist) > 0: |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
104 | partitioned.append (commandlist[0:15]) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
105 | commandlist = commandlist[15:] |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
106 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
107 | for part in partitioned: |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
108 | bot.privmsg (replyto, 'Available commands: %s' % (", ".join (part))) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
109 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
110 | def cmd_help (bot, replyto, ident, host, args, **rest): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
111 | cmd = ModuleCore.get_command_by_name (args['command']) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
112 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
113 | if not cmd: |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
114 | command_error ('unknown command \'%s\'' % args['command']) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
115 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
116 | if not ModuleCore.is_available (cmd, ident, host): |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
117 | command_error ('you may not use %s' % cmd['name']) |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
118 | |
2bad379cd416
- added .help and .commands
Teemu Piippo <crimsondusk64@gmail.com>
parents:
76
diff
changeset
|
119 | bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) |
105 | 120 | |
106
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
121 | def mathsubstitute (expr, token, value): |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
122 | rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$') |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
123 | match = rex.match (expr) |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
124 | |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
125 | while match: |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
126 | expr = match.group(1) + str (value) + match.group(2) |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
127 | match = rex.match (expr) |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
128 | |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
129 | return expr |
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
130 | |
105 | 131 | def cmd_calc (bot, replyto, args, **rest): |
132 | expr = args['expression'] | |
133 | ||
134 | try: | |
106
31583e5b2f49
- added e and phi to .calc
Teemu Piippo <crimsondusk64@gmail.com>
parents:
105
diff
changeset
|
135 | # Substitute some mathematical constants |
107
14e1d18d2a2a
- add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
136 | expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) |
14e1d18d2a2a
- add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
137 | expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995) |
14e1d18d2a2a
- add more decimals to the mathematical constants
Teemu Piippo <crimsondusk64@gmail.com>
parents:
106
diff
changeset
|
138 | expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio |
105 | 139 | |
140 | result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \ | |
141 | .replace ('\t', '') \ | |
142 | .replace ('\n', '') | |
143 | ||
144 | errmatch = re.compile (r'^.*\bError\b.*$').match (result) | |
145 | ||
146 | if errmatch: | |
147 | command_error ('math error') | |
148 | return | |
149 | ||
150 | bot.privmsg (replyto, 'Result: %s' % result) | |
151 | except subprocess.CalledProcessError as e: | |
152 | command_error (e.output.split('\n')[0]) | |
153 | except OSError as e: | |
154 | command_error ('failed to execute calc: ' + e.strerror) |