Updated gcalc.py to use included copy of BeautifulSoup.
This commit is contained in:
parent
6b4042972b
commit
4b7cbb118c
1 changed files with 4 additions and 17 deletions
|
@ -1,7 +1,7 @@
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from util import hook, http, misc
|
from util import hook, http, misc
|
||||||
from BeautifulSoup import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
|
||||||
@hook.command("math")
|
@hook.command("math")
|
||||||
|
@ -9,24 +9,11 @@ from BeautifulSoup import BeautifulSoup
|
||||||
def calc(inp):
|
def calc(inp):
|
||||||
"calc <term> -- Calculate <term> with Google Calc."
|
"calc <term> -- Calculate <term> with Google Calc."
|
||||||
|
|
||||||
white_re = re.compile(r'\s+')
|
|
||||||
|
|
||||||
page = http.get('http://www.google.com/search', q=inp)
|
page = http.get('http://www.google.com/search', q=inp)
|
||||||
|
soup = BeautifulSoup(page, 'lxml')
|
||||||
soup = BeautifulSoup(page)
|
|
||||||
|
|
||||||
response = soup.find('h2', {'class': 'r'})
|
response = soup.find('h2', {'class': 'r'})
|
||||||
|
|
||||||
if response is None:
|
if response is None:
|
||||||
return "Could not calculate " + inp
|
return "Could not calculate '%s'" % inp
|
||||||
|
|
||||||
output = response.renderContents()
|
return http.unescape(response.contents[0])
|
||||||
|
|
||||||
output = ' '.join(output.splitlines())
|
|
||||||
output = output.replace("\xa0", ",")
|
|
||||||
output = white_re.sub(' ', output.strip())
|
|
||||||
|
|
||||||
output = output.decode('utf-8', 'ignore')
|
|
||||||
output = misc.strip_html(output)
|
|
||||||
|
|
||||||
return output
|
|
||||||
|
|
Reference in a new issue