From 91bbc64f2cd5faf025844bed0d06ce9e17dc3eb3 Mon Sep 17 00:00:00 2001 From: mutantmonkey Date: Mon, 30 Sep 2013 23:33:47 -0700 Subject: [PATCH] remove oblique and make .wa local --- modules/calc.py | 56 ++++++++++-------- modules/oblique.py | 119 -------------------------------------- modules/test/test_calc.py | 7 +++ 3 files changed, 38 insertions(+), 144 deletions(-) delete mode 100644 modules/oblique.py diff --git a/modules/calc.py b/modules/calc.py index ea29b4c..128cc55 100644 --- a/modules/calc.py +++ b/modules/calc.py @@ -15,30 +15,31 @@ r_result = re.compile(r'(?i)(.*?)') r_tag = re.compile(r'<\S+.*?>') subs = [ - (' in ', ' -> '), - (' over ', ' / '), - ('£', 'GBP '), - ('€', 'EUR '), - ('\$', 'USD '), - (r'\bKB\b', 'kilobytes'), - (r'\bMB\b', 'megabytes'), - (r'\bGB\b', 'kilobytes'), - ('kbps', '(kilobits / second)'), + (' in ', ' -> '), + (' over ', ' / '), + ('£', 'GBP '), + ('€', 'EUR '), + ('\$', 'USD '), + (r'\bKB\b', 'kilobytes'), + (r'\bMB\b', 'megabytes'), + (r'\bGB\b', 'kilobytes'), + ('kbps', '(kilobits / second)'), ('mbps', '(megabits / second)') ] -def c(phenny, input): + +def c(phenny, input): """Google calculator.""" if not input.group(2): return phenny.reply("Nothing to calculate.") q = input.group(2) - q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5 - q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0 + q = q.replace('\xcf\x95', 'phi') # utf-8 U+03D5 + q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0 uri = 'http://www.google.com/ig/calculator?q=' bytes = web.get(uri + web.quote(q)) parts = bytes.split('",') answer = [p for p in parts if p.startswith('rhs: "')][0][6:] - if answer: + if answer: #answer = ''.join(chr(ord(c)) for c in answer) #answer = answer.decode('utf-8') answer = answer.replace('\\x26#215;', '*') @@ -48,26 +49,31 @@ def c(phenny, input): answer = answer.replace('', ')') answer = web.decode(answer) phenny.say(answer) - else: phenny.reply('Sorry, no result.') + else: + phenny.reply('Sorry, no result.') c.commands = ['c'] c.example = '.c 5 + 3' -def wa(phenny, input): + +def wa(phenny, input): if not input.group(2): return phenny.reply("No search term.") query = input.group(2) - uri = 'http://tumbolia.appspot.com/wa/' - answer = web.get(uri + web.quote(query.replace('+', '%2B'))) - try: - answer = answer.split(';')[1] - except IndexError: - answer = "" + re_output = re.compile(r'{"stringified": "(.*?)",') - if answer: - phenny.say(answer) - else: phenny.reply('Sorry, no result.') + uri = 'http://www.wolframalpha.com/input/?i={}' + out = web.get(uri.format(web.quote(query))) + answers = re_output.findall(out) + if len(answers) <= 0: + phenny.reply("Sorry, no result.") + return + + answer = answers[1] + answer = answer.replace('\\n', ', ') + phenny.say(answer) wa.commands = ['wa'] -if __name__ == '__main__': + +if __name__ == '__main__': print(__doc__.strip()) diff --git a/modules/oblique.py b/modules/oblique.py deleted file mode 100644 index 413a061..0000000 --- a/modules/oblique.py +++ /dev/null @@ -1,119 +0,0 @@ -#!/usr/bin/env python -""" -oblique.py - Web Services Interface -Copyright 2008-9, Sean B. Palmer, inamidst.com -Licensed under the Eiffel Forum License 2. - -http://inamidst.com/phenny/ -""" - -import re -import web - -definitions = 'https://github.com/nslater/oblique/wiki' - -r_item = re.compile(r'(?i)
  • (.*?)
  • ') -r_tag = re.compile(r'<[^>]+>') - -def mappings(uri): - result = {} - bytes = web.get(uri) - for item in r_item.findall(bytes): - item = r_tag.sub('', item).strip(' \t\r\n') - if not ' ' in item: continue - - command, template = item.split(' ', 1) - if not command.isalnum(): continue - if not template.startswith('http://'): continue - result[command] = template.replace('&', '&') - return result - -def service(phenny, input, command, args): - t = o.services[command] - template = t.replace('${args}', web.quote(args, '')) - template = template.replace('${nick}', web.quote(input.nick, '')) - uri = template.replace('${sender}', web.quote(input.sender, '')) - - info = web.head(uri) - if isinstance(info, list): - info = info[0] - if not 'text/plain' in info.get('content-type', '').lower(): - return phenny.reply("Sorry, the service didn't respond in plain text.") - bytes = web.get(uri) - lines = bytes.splitlines() - if not lines: - return phenny.reply("Sorry, the service didn't respond any output.") - try: line = lines[0].encode('utf-8')[:350] - except: line = lines[0][:250] - phenny.say(lines[0][:350]) - -def refresh(phenny): - if hasattr(phenny.config, 'services'): - services = phenny.config.services - else: services = definitions - - old = o.services - o.serviceURI = services - o.services = mappings(o.serviceURI) - return len(o.services), set(o.services) - set(old) - -def o(phenny, input): - """Call a webservice.""" - text = input.group(2) - - if (not o.services) or (text == 'refresh'): - length, added = refresh(phenny) - if text == 'refresh': - msg = 'Okay, found %s services.' % length - if added: - msg += ' Added: ' + ', '.join(sorted(added)[:5]) - if len(added) > 5: msg += ', &c.' - return phenny.reply(msg) - - if not text: - return phenny.reply('Try %s for details.' % o.serviceURI) - - if ' ' in text: - command, args = text.split(' ', 1) - else: command, args = text, '' - command = command.lower() - - if command == 'service': - msg = o.services.get(args, 'No such service!') - return phenny.reply(msg) - - if command not in o.services: - return phenny.reply('Service not found in %s' % o.serviceURI) - - if hasattr(phenny.config, 'external'): - default = phenny.config.external.get('*') - manifest = phenny.config.external.get(input.sender, default) - if manifest: - commands = set(manifest) - if (command not in commands) and (manifest[0] != '!'): - return phenny.reply('Sorry, %s is not whitelisted' % command) - elif (command in commands) and (manifest[0] == '!'): - return phenny.reply('Sorry, %s is blacklisted' % command) - service(phenny, input, command, args) -o.commands = ['o'] -o.example = '.o servicename arg1 arg2 arg3' -o.services = {} -o.serviceURI = None - -def snippet(phenny, input): - if not o.services: - refresh(phenny) - - search = web.quote(input.group(2)) - py = "BeautifulSoup.BeautifulSoup(re.sub('<.*?>|(?<= ) +', '', " + \ - "''.join(chr(ord(c)) for c in " + \ - "eval(urllib.urlopen('http://ajax.googleapis.com/ajax/serv" + \ - "ices/search/web?v=1.0&q=" + search + "').read()" + \ - ".replace('null', 'None'))['responseData']['resul" + \ - "ts'][0]['content'].decode('unicode-escape')).replace(" + \ - "'"', '\x22')), convertEntities=True)" - service(phenny, input, 'py', py) -snippet.commands = ['snippet'] - -if __name__ == '__main__': - print(__doc__.strip()) diff --git a/modules/test/test_calc.py b/modules/test/test_calc.py index cbf80d3..081bccf 100644 --- a/modules/test/test_calc.py +++ b/modules/test/test_calc.py @@ -38,6 +38,13 @@ class TestCalc(unittest.TestCase): '(asked, but not answered, about a general swallow in the '\ '1975 film Monty Python and the Holy Grail)') + def test_wa_math(self): + input = Mock(group=lambda x: '2+2') + wa(self.phenny, input) + + self.phenny.say.assert_called_once_with('4') + + def test_wa_none(self): input = Mock(group=lambda x: "jajoajaj ojewphjqo I!tj") wa(self.phenny, input)