Mon, 08 Dec 2014 16:12:51 -0500
- now in color!
import math import urllib2 import json import subprocess import re from modulecore import command_error import modulecore as ModuleCore ModuleData = { 'commands': [ { 'name': 'convert', 'description': 'Performs numeric conversion', 'args': '<value> as <valuetype>', 'level': 'normal', }, { 'name': 'ud', 'description': 'Looks up a term in urban dictionary', 'args': '<term...>', 'level': 'normal', }, { 'name': 'commands', 'description': 'Lists commands available to the calling user', 'args': None, 'level': 'normal', }, { 'name': 'help', 'description': 'Prints help about a given command', 'args': '<command>', 'level': 'normal', }, { 'name': 'calc', 'description': 'Calculates a mathematical expression using apcalc', 'args': '<expression...>', 'level': 'normal', }, ] } def cmd_convert (bot, args, replyto, **rest): value = float (args['value']) valuetype = args['valuetype'] if valuetype in ['radians', 'degrees']: if valuetype == 'radians': radvalue = value degvalue = (value * 180.) / math.pi else: radvalue = (value * math.pi) / 180. degvalue = value bot.privmsg (replyto, '%s radians, %s degrees (%s)' % (radvalue, degvalue, degvalue % 360.)) return if valuetype in ['celsius', 'fahrenheit']: if valuetype == 'celsius': celvalue = value fahrvalue = value * 1.8 + 32 else: celvalue = (value - 32) / 1.8 fahrvalue = value bot.privmsg (replyto, '%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) return command_error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + 'celsius, fahrenheit (temperature conversion)' % valuetype) def cmd_ud (bot, args, replyto, **rest): try: url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20')) response = urllib2.urlopen (url).read() data = json.loads (response) if not 'list' in data \ or len(data['list']) == 0 \ or not 'word' in data['list'][0] \ or not 'definition' in data['list'][0]: command_error ("couldn't find a definition of \002%s\002" % args['term']) 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'] bot.privmsg (replyto, "\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) except Exception as e: command_error ('Urban dictionary lookup failed: %s' % e) def cmd_commands (bot, replyto, ident, host, **rest): commandlist = ModuleCore.get_available_commands (ident, host) partitioned=[] while len (commandlist) > 0: partitioned.append (commandlist[0:15]) commandlist = commandlist[15:] for part in partitioned: bot.privmsg (replyto, 'Available commands: %s' % (", ".join (part))) def cmd_help (bot, replyto, ident, host, args, **rest): cmd = ModuleCore.get_command_by_name (args['command']) if not cmd: command_error ('unknown command \'%s\'' % args['command']) if not ModuleCore.is_available (cmd, ident, host): command_error ('you may not use %s' % cmd['name']) bot.privmsg (replyto, '%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) def mathsubstitute (expr, token, value): rex = re.compile (r'^(.*)\b' + token + r'\b(.*)$') match = rex.match (expr) while match: expr = match.group(1) + str (value) + match.group(2) match = rex.match (expr) return expr def cmd_calc (bot, replyto, args, **rest): expr = args['expression'] try: # Substitute some mathematical constants expr = mathsubstitute (expr, 'pi' , 3.14159265358979323846264338327950288419716939937510) expr = mathsubstitute (expr, 'e' , 2.71828182845904523536028747135266249775724709369995) expr = mathsubstitute (expr, 'phi', 1.6180339887498948482) # golden ratio result = subprocess.check_output (['calc', expr], stderr=subprocess.STDOUT) \ .replace ('\t', '') \ .replace ('\n', '') errmatch = re.compile (r'^.*\bError\b.*$').match (result) if errmatch: command_error ('math error') return bot.privmsg (replyto, 'Result: %s' % result) except subprocess.CalledProcessError as e: command_error (e.output.split('\n')[0]) except OSError as e: command_error ('failed to execute calc: ' + e.strerror)