80 lines
1.8 KiB
Python
80 lines
1.8 KiB
Python
#!/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 ', ' / '),
|
|
('£', 'GBP '),
|
|
('€', 'EUR '),
|
|
('\$', 'USD '),
|
|
(r'\bKB\b', 'kilobytes'),
|
|
(r'\bMB\b', 'megabytes'),
|
|
(r'\bGB\b', 'kilobytes'),
|
|
('kbps', '(kilobits / second)'),
|
|
('mbps', '(megabits / second)')
|
|
]
|
|
|
|
|
|
def c(phenny, input):
|
|
"""DuckDuckGo calculator."""
|
|
if not input.group(2):
|
|
return phenny.reply("Nothing to calculate.")
|
|
q = input.group(2)
|
|
|
|
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
|
|
|
|
if answer:
|
|
phenny.say(answer)
|
|
else:
|
|
phenny.reply('Sorry, no result.')
|
|
c.commands = ['c']
|
|
c.example = '.c 5 + 3'
|
|
|
|
|
|
def wa(phenny, input):
|
|
if not input.group(2):
|
|
return phenny.reply("No search term.")
|
|
query = input.group(2)
|
|
|
|
re_output = re.compile(r'{"stringified": "(.*?)",')
|
|
|
|
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]
|
|
answer = answer.replace('\\n', ', ')
|
|
phenny.say(answer)
|
|
wa.commands = ['wa']
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print(__doc__.strip())
|