# -*- coding: utf-8 -*- # # Copyright (C) 2009-2015 Ben Kurtovic # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. import re import urllib.request import urllib.parse from earwigbot.commands import Command class Calc(Command): """A somewhat advanced calculator: see https://futureboy.us/fsp/frink.fsp for details.""" name = "calc" def process(self, data): if not data.args: self.reply(data, "What do you want me to calculate?") return query = ' '.join(data.args) query = self.cleanup(query) url = "https://futureboy.us/fsp/frink.fsp?fromVal={0}" url = url.format(urllib.parse.quote(query)) result = urllib.request.urlopen(url).read().decode() r_result = re.compile(r'(?i)(.*?)') r_tag = re.compile(r'<\S+.*?>') match = r_result.search(result) if not match: self.reply(data, "Calculation error.") return result = match.group(1) result = r_tag.sub("", result) # strip span.warning tags result = result.replace(">", ">") result = result.replace("(undefined symbol)", "(?) ") result = result.strip() if not result: result = '?' elif " in " in query: result += " " + query.split(" in ", 1)[1] res = "%s = %s" % (query, result) self.reply(data, res) @staticmethod def cleanup(query): fixes = [ (' in ', ' -> '), (' over ', ' / '), ('£', 'GBP '), ('€', 'EUR '), (r'\$', 'USD '), (r'\bKB\b', 'kilobytes'), (r'\bMB\b', 'megabytes'), (r'\bGB\b', 'gigabytes'), ('kbps', '(kilobits / second)'), ('mbps', '(megabits / second)') ] for original, fix in fixes: query = re.sub(original, fix, query) return query.strip()