From 9ae58d0a353fc610e9affc1216b2cdc77e8cf89c Mon Sep 17 00:00:00 2001 From: "Sean B. Palmer" Date: Sat, 6 Nov 2010 12:52:35 +0000 Subject: New calculator function, as tested by yano and jasondavies! --- Makefile | 2 +- modules/calc.py | 22 +++++++++++++++++++++- web.py | 18 +++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 75636b5..715735e 100644 --- a/Makefile +++ b/Makefile @@ -15,4 +15,4 @@ log: ; git graph sync: ; - rsync -avz --delete ./ pubble:opt/phenny/ + rsync -avz ./ pubble:opt/phenny/ diff --git a/modules/calc.py b/modules/calc.py index e664cb2..c4b304d 100755 --- a/modules/calc.py +++ b/modules/calc.py @@ -43,7 +43,7 @@ def calc(phenny, input): precision = 2 query = web.urllib.quote(query.encode('utf-8')) - uri = 'http://futureboy.homeip.net/fsp/frink.fsp?fromVal=' + uri = 'http://futureboy.us/fsp/frink.fsp?fromVal=' bytes = web.get(uri + query) m = r_result.search(bytes) if m: @@ -63,8 +63,28 @@ def calc(phenny, input): phenny.say(q + ' = ' + result[:350]) else: phenny.reply("Sorry, can't calculate that.") + phenny.say('Note that .calc is deprecated, consider using .c') calc.commands = ['calc'] calc.example = '.calc 5 + 3' +def c(phenny, input): + """Google calculator.""" + q = input.group(2).encode('utf-8') + q = q.replace('\xcf\x86', 'phi') # utf-8 U+03C6 + q = q.replace('\xcf\x80', 'pi') # utf-8 U+03C0 + uri = 'http://www.google.com/ig/calculator?q=' + bytes = web.get(uri + web.urllib.quote(q)) + parts = bytes.split('",') + answer = [p for p in parts if p.startswith('rhs: "')][0][6:] + if answer: + answer = answer.decode('unicode-escape') + answer = answer.replace('', '^(') + answer = answer.replace('', ')') + answer = web.decode(answer) + phenny.say(answer) + else: phenny.say('Sorry, no result.') +c.commands = ['c'] +c.example = '.c 5 + 3' + if __name__ == '__main__': print __doc__.strip() diff --git a/web.py b/web.py index 31366f4..1a2b5e8 100755 --- a/web.py +++ b/web.py @@ -5,7 +5,8 @@ Author: Sean B. Palmer, inamidst.com About: http://inamidst.com/phenny/ """ -import urllib +import re, urllib +from htmlentitydefs import name2codepoint class Grab(urllib.URLopener): def __init__(self, *args): @@ -40,5 +41,20 @@ def post(uri, query): u.close() return bytes +r_entity = re.compile(r'&([^;\s]+);') + +def entity(match): + value = match.group(1).lower() + if value.startswith('#x'): + return unichr(int(value[2:], 16)) + elif value.startswith('#'): + return unichr(int(value[1:])) + elif name2codepoint.has_key(value): + return unichr(name2codepoint[value]) + return '[' + value + ']' + +def decode(html): + return r_entity.sub(entity, html) + if __name__=="__main__": main() -- cgit v1.2.3-1-g7c22