Tue, 18 Aug 2015 14:38:54 +0300
Use python3 in the shebang
''' Copyright 2014-2015 Teemu Piippo All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. ''' from __future__ import print_function import math import json import re import modulecore import utility import calc import urllib.parse @modulecore.irc_command (args='<value> as <valuetype>') def convert (bot, args, reply, error, **rest): '''Performs numeric conversion.''' try: value = float (args['value']) except Exception as e: error (str (e)) 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 reply ('%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 reply ('%s degrees celsius, %s degrees fahrenheit' % (celvalue, fahrvalue)) return error ('unknown valuetype %s, expected one of: degrees, radians (angle conversion), ' + 'celsius, fahrenheit (temperature conversion)' % valuetype) @modulecore.irc_command (args='<term...>') def ud (bot, args, reply, error, **rest): '''Looks up a term in urban dictionary.''' try: url = 'http://api.urbandictionary.com/v0/define?term=%s' % (args['term'].replace (' ', '%20')) response = utility.read_url (url) 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]: 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'] reply ("\002%s\002: %s\0033 %d\003 up,\0035 %d\003 down" % (word, definition, up, down)) except Exception as e: error ('Urban dictionary lookup failed: %s' % e) @modulecore.irc_command() def commands (bot, reply, ident, host, **rest): '''Lists commands available to the calling user.''' commandlist = modulecore.get_available_commands (ident, host) partitioned=[] while len (commandlist) > 0: partitioned.append (commandlist[0:15]) commandlist = commandlist[15:] for part in partitioned: reply ('\002Available commands\002: %s' % (", ".join (part))) @modulecore.irc_command (args='<command>') def help (bot, reply, ident, host, args, error, **rest): '''Prints help about a given command.''' cmd = modulecore.get_command_by_name (args['command']) if not cmd: error ('unknown command \'%s\'' % args['command']) if not modulecore.is_available (cmd, ident, host): error ('you may not use %s' % cmd['name']) reply ('%s %s: %s' % (cmd['name'], cmd['args'], cmd['description'])) @modulecore.irc_command() def calcfunctions (bot, reply, **rest): '''Lists the functions supported by .calc.''' reply ('Available functions for .calc: %s' % \ ', '.join (sorted ([name for name, data in calc.Functions.items()]))) @modulecore.irc_command (args='<expression...>') def calc (bot, reply, args, **rest): '''Calculates a mathematical expression.''' reply (calc.Calculator().calc (args['expression'])) @modulecore.irc_command() def more (commandObject, **rest): '''Prints subsequent command result pages.''' modulecore.print_responses (commandObject) @modulecore.irc_command() def yes (**k): '''Confirms the previous command.''' modulecore.confirm (k, True) @modulecore.irc_command() def no (**k): '''Unconfirms the previous command.''' modulecore.confirm (k, False) @modulecore.irc_command (args='<link...>') def bitly (reply, args, **k): '''Shortens a link using bit.ly.''' reply ('Result: %s' % utility.shorten_link (args['link'])) @modulecore.irc_command (args='<command...>') def py (reply, args, **rest): '''Evaluates the given Python string using appspot.com.''' url = 'http://eval.appspot.com/eval?statement=' + urllib.parse.quote (args['command']) result = utility.read_url (url, timeout=15).splitlines() if not result: reply ('No output.') return # \x0f is the 'reset colors' code, prepended to all reply lines to prevent other bots from # reacting to this .py call. if result[0] == 'Traceback (most recent call last):': reply ('\x0f' + result[-1]) else: for line in result: reply ('\x0f' + line)