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
|
2017-05-28 00:42:44 -04:00
|
|
|
|
from modules.search import generic_google
|
2008-02-23 07:17:06 -05:00
|
|
|
|
|
|
|
|
|
subs = [
|
2013-10-01 02:33:47 -04:00
|
|
|
|
('£', 'GBP '),
|
|
|
|
|
('€', 'EUR '),
|
|
|
|
|
('\$', 'USD '),
|
2013-11-17 15:37:14 -05:00
|
|
|
|
(r'\n', '; '),
|
|
|
|
|
('°', '°'),
|
|
|
|
|
(r'\/', '/'),
|
2008-02-23 07:17:06 -05:00
|
|
|
|
]
|
|
|
|
|
|
2017-05-28 00:42:44 -04:00
|
|
|
|
r_google_calc = re.compile(r'calculator-40.gif.*? = (.*?)<')
|
2017-07-07 00:28:10 -04:00
|
|
|
|
r_google_calc_exp = re.compile(r'calculator-40.gif.*? = (.*?)<sup>(.*?)</sup></h2>')
|
2013-10-01 02:33:47 -04:00
|
|
|
|
|
|
|
|
|
def c(phenny, input):
|
2017-05-28 00:42:44 -04:00
|
|
|
|
"""Google calculator."""
|
2012-01-03 14:09:34 -05:00
|
|
|
|
if not input.group(2):
|
|
|
|
|
return phenny.reply("Nothing to calculate.")
|
|
|
|
|
q = input.group(2)
|
2017-05-28 00:42:44 -04:00
|
|
|
|
bytes = generic_google(q)
|
|
|
|
|
m = r_google_calc_exp.search(bytes)
|
|
|
|
|
if not m:
|
|
|
|
|
m = r_google_calc.search(bytes)
|
2017-05-29 17:27:20 -04:00
|
|
|
|
|
2017-05-28 00:42:44 -04:00
|
|
|
|
if not m:
|
|
|
|
|
num = None
|
|
|
|
|
elif m.lastindex == 1:
|
|
|
|
|
num = web.decode(m.group(1))
|
2013-11-05 12:19:02 -05:00
|
|
|
|
else:
|
2017-05-28 00:42:44 -04:00
|
|
|
|
num = "^".join((web.decode(m.group(1)), web.decode(m.group(2))))
|
2013-11-05 12:19:02 -05:00
|
|
|
|
|
2017-05-28 00:42:44 -04:00
|
|
|
|
if num:
|
2017-05-29 17:27:20 -04:00
|
|
|
|
num = num.replace('×', '*')
|
2017-05-28 00:42:44 -04:00
|
|
|
|
phenny.say(num)
|
2013-10-01 02:33:47 -04:00
|
|
|
|
else:
|
2017-05-28 00:42:44 -04:00
|
|
|
|
phenny.reply("Sorry, no result.")
|
2010-11-06 08:52:35 -04:00
|
|
|
|
c.commands = ['c']
|
|
|
|
|
c.example = '.c 5 + 3'
|
|
|
|
|
|
2013-10-01 02:33:47 -04:00
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2012-01-03 14:09:34 -05:00
|
|
|
|
print(__doc__.strip())
|