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 = [
|
2012-01-03 14:09:34 -05:00
|
|
|
(' in ', ' -> '),
|
|
|
|
(' over ', ' / '),
|
|
|
|
('£', 'GBP '),
|
|
|
|
('€', 'EUR '),
|
|
|
|
('\$', 'USD '),
|
|
|
|
(r'\bKB\b', 'kilobytes'),
|
|
|
|
(r'\bMB\b', 'megabytes'),
|
|
|
|
(r'\bGB\b', 'kilobytes'),
|
|
|
|
('kbps', '(kilobits / second)'),
|
|
|
|
('mbps', '(megabits / second)')
|
2008-02-23 07:17:06 -05:00
|
|
|
]
|
|
|
|
|
2010-11-06 08:52:35 -04:00
|
|
|
def c(phenny, input):
|
2012-01-03 14:09:34 -05:00
|
|
|
"""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
|
|
|
|
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:
|
|
|
|
#answer = ''.join(chr(ord(c)) for c in answer)
|
|
|
|
#answer = answer.decode('utf-8')
|
2012-06-05 21:24:43 -04:00
|
|
|
answer = answer.replace('\\x26#215;', '*')
|
|
|
|
answer = answer.replace('\\x3c', '<')
|
|
|
|
answer = answer.replace('\\x3e', '>')
|
2012-01-03 14:09:34 -05:00
|
|
|
answer = answer.replace('<sup>', '^(')
|
|
|
|
answer = answer.replace('</sup>', ')')
|
|
|
|
answer = web.decode(answer)
|
|
|
|
phenny.say(answer)
|
2012-06-19 21:04:00 -04:00
|
|
|
else: phenny.reply('Sorry, no result.')
|
2010-11-06 08:52:35 -04:00
|
|
|
c.commands = ['c']
|
|
|
|
c.example = '.c 5 + 3'
|
|
|
|
|
2010-11-06 09:58:51 -04:00
|
|
|
def wa(phenny, input):
|
2012-01-03 14:09:34 -05:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("No search term.")
|
|
|
|
query = input.group(2)
|
|
|
|
uri = 'http://tumbolia.appspot.com/wa/'
|
2012-06-19 21:04:00 -04:00
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
answer = web.get(uri + web.quote(query.replace('+', '%2B')))
|
2012-06-19 21:04:00 -04:00
|
|
|
try:
|
|
|
|
answer = answer.split(';')[1]
|
|
|
|
except IndexError:
|
|
|
|
answer = ""
|
|
|
|
|
2012-01-03 14:09:34 -05:00
|
|
|
if answer:
|
|
|
|
phenny.say(answer)
|
|
|
|
else: phenny.reply('Sorry, no result.')
|
2010-11-06 09:58:51 -04:00
|
|
|
wa.commands = ['wa']
|
|
|
|
|
2008-02-23 07:17:06 -05:00
|
|
|
if __name__ == '__main__':
|
2012-01-03 14:09:34 -05:00
|
|
|
print(__doc__.strip())
|