phenny-1/modules/calc.py

91 lines
2.4 KiB
Python
Raw Normal View History

2008-02-23 07:17:06 -05:00
#!/usr/bin/env python
# coding=utf-8
"""
calc.py - Phenny Calculator Module
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/
"""
import re
import web
r_result = re.compile(r'(?i)<A NAME=results>(.*?)</A>')
r_tag = re.compile(r'<\S+.*?>')
subs = [
(' in ', ' -> '),
(' over ', ' / '),
(u'£', 'GBP '),
(u'', 'EUR '),
('\$', 'USD '),
(r'\bKB\b', 'kilobytes'),
(r'\bMB\b', 'megabytes'),
(r'\bGB\b', 'kilobytes'),
('kbps', '(kilobits / second)'),
('mbps', '(megabits / second)')
]
def calc(phenny, input):
2008-03-10 15:58:28 -04:00
"""Use the Frink online calculator."""
2008-02-23 07:17:06 -05:00
q = input.group(2)
if not q:
return phenny.say('0?')
2008-02-23 07:17:06 -05:00
query = q[:]
for a, b in subs:
query = re.sub(a, b, query)
query = query.rstrip(' \t')
precision = 5
if query[-3:] in ('GBP', 'USD', 'EUR', 'NOK'):
precision = 2
query = web.urllib.quote(query.encode('utf-8'))
uri = 'http://futureboy.us/fsp/frink.fsp?fromVal='
2008-02-23 07:17:06 -05:00
bytes = web.get(uri + query)
m = r_result.search(bytes)
if m:
result = m.group(1)
result = r_tag.sub('', result) # strip span.warning tags
result = result.replace('&gt;', '>')
result = result.replace('(undefined symbol)', '(?) ')
if '.' in result:
try: result = str(round(float(result), precision))
except ValueError: pass
if not result.strip():
result = '?'
elif ' in ' in q:
result += ' ' + q.split(' in ', 1)[1]
phenny.say(q + ' = ' + result[:350])
2008-02-23 07:17:06 -05:00
else: phenny.reply("Sorry, can't calculate that.")
phenny.say('Note that .calc is deprecated, consider using .c')
2008-02-23 07:17:06 -05:00
calc.commands = ['calc']
2008-03-10 15:58:28 -04:00
calc.example = '.calc 5 + 3'
2008-02-23 07:17:06 -05:00
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('<sup>', '^(')
answer = answer.replace('</sup>', ')')
answer = web.decode(answer)
phenny.say(answer)
else: phenny.say('Sorry, no result.')
c.commands = ['c']
c.example = '.c 5 + 3'
2008-02-23 07:17:06 -05:00
if __name__ == '__main__':
print __doc__.strip()