scrape calc results from google since ddg is broken
parent
7dc5ece29b
commit
7c67f09074
|
@ -10,6 +10,7 @@ http://inamidst.com/phenny/
|
||||||
|
|
||||||
import re
|
import re
|
||||||
import web
|
import web
|
||||||
|
from modules.search import generic_google
|
||||||
|
|
||||||
subs = [
|
subs = [
|
||||||
('£', 'GBP '),
|
('£', 'GBP '),
|
||||||
|
@ -20,30 +21,55 @@ subs = [
|
||||||
(r'\/', '/'),
|
(r'\/', '/'),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
r_google_calc = re.compile(r'calculator-40.gif.*? = (.*?)<')
|
||||||
|
r_google_calc_exp = re.compile(r'calculator-40.gif.*? = (.*?)<sup>(.*?)<')
|
||||||
|
|
||||||
def c(phenny, input):
|
def c(phenny, input):
|
||||||
"""DuckDuckGo calculator."""
|
"""Google calculator."""
|
||||||
if not input.group(2):
|
if not input.group(2):
|
||||||
return phenny.reply("Nothing to calculate.")
|
return phenny.reply("Nothing to calculate.")
|
||||||
q = input.group(2)
|
q = input.group(2)
|
||||||
|
bytes = generic_google(q)
|
||||||
|
m = r_google_calc_exp.search(bytes)
|
||||||
|
if not m:
|
||||||
|
m = r_google_calc.search(bytes)
|
||||||
|
|
||||||
try:
|
if not m:
|
||||||
r = web.get(
|
num = None
|
||||||
'https://api.duckduckgo.com/?q={}&format=json&no_html=1'
|
elif m.lastindex == 1:
|
||||||
'&t=mutantmonkey/phenny'.format(web.quote(q)))
|
num = web.decode(m.group(1))
|
||||||
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:
|
else:
|
||||||
answer = None
|
num = "^".join((web.decode(m.group(1)), web.decode(m.group(2))))
|
||||||
|
|
||||||
if answer:
|
if num:
|
||||||
phenny.say(answer)
|
phenny.say(num)
|
||||||
else:
|
else:
|
||||||
phenny.reply('Sorry, no result.')
|
phenny.reply("Sorry, no result.")
|
||||||
|
|
||||||
|
|
||||||
|
# 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.commands = ['c']
|
||||||
c.example = '.c 5 + 3'
|
c.example = '.c 5 + 3'
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ class TestCalc(unittest.TestCase):
|
||||||
input = Mock(group=lambda x: '2^64')
|
input = Mock(group=lambda x: '2^64')
|
||||||
c(self.phenny, input)
|
c(self.phenny, input)
|
||||||
|
|
||||||
self.phenny.say.assert_called_once_with('1.84467440737096 * 10^19')
|
self.phenny.say.assert_called_once_with('1.84467441 × 10^19')
|
||||||
|
|
||||||
def test_c_none(self):
|
def test_c_none(self):
|
||||||
input = Mock(group=lambda x: 'aif')
|
input = Mock(group=lambda x: 'aif')
|
||||||
|
|
Loading…
Reference in New Issue