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
|
|
|
|
|
|
|
|
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
|
|
|
]
|
|
|
|
|
2013-10-01 02:33:47 -04:00
|
|
|
|
|
|
|
def c(phenny, input):
|
2013-11-05 12:19:02 -05:00
|
|
|
"""DuckDuckGo calculator."""
|
2012-01-03 14:09:34 -05:00
|
|
|
if not input.group(2):
|
|
|
|
return phenny.reply("Nothing to calculate.")
|
|
|
|
q = input.group(2)
|
2013-11-05 12:19:02 -05:00
|
|
|
|
|
|
|
try:
|
|
|
|
r = web.get(
|
|
|
|
'https://api.duckduckgo.com/?q={}&format=json&no_html=1'
|
|
|
|
'&t=mutantmonkey/phenny'.format(web.quote(q)))
|
|
|
|
except web.HTTPError:
|
|
|
|
raise GrumbleError("Couldn't parse the result from DuckDuckGo.")
|
|
|
|
|
|
|
|
data = web.json(r)
|
|
|
|
if data['AnswerType'] == 'calc':
|
|
|
|
answer = data['Answer'].split('=')[-1].strip()
|
|
|
|
else:
|
|
|
|
answer = None
|
|
|
|
|
2013-10-01 02:33:47 -04:00
|
|
|
if answer:
|
2012-01-03 14:09:34 -05:00
|
|
|
phenny.say(answer)
|
2013-10-01 02:33:47 -04:00
|
|
|
else:
|
|
|
|
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
|
|
|
|
|
|
|
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)
|
2012-06-19 21:04:00 -04:00
|
|
|
|
2013-10-01 02:33:47 -04:00
|
|
|
re_output = re.compile(r'{"stringified": "(.*?)",')
|
2012-06-19 21:04:00 -04:00
|
|
|
|
2013-10-01 02:33:47 -04:00
|
|
|
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]
|
2013-11-17 15:37:14 -05:00
|
|
|
for sub in subs:
|
|
|
|
answer = answer.replace(sub[0], sub[1])
|
|
|
|
|
2013-10-01 02:33:47 -04:00
|
|
|
phenny.say(answer)
|
2010-11-06 09:58:51 -04:00
|
|
|
wa.commands = ['wa']
|
2013-11-28 22:23:32 -05:00
|
|
|
wa.example = '.wa answer to life'
|
2013-10-01 02:33:47 -04:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2012-01-03 14:09:34 -05:00
|
|
|
print(__doc__.strip())
|