2015-12-30 02:59:51 +00:00
|
|
|
# WolframAlpha (Maths)
|
|
|
|
#
|
|
|
|
# @website http://www.wolframalpha.com/
|
2016-01-02 04:02:10 +00:00
|
|
|
# @provide-api yes (http://api.wolframalpha.com/v2/)
|
2015-12-30 02:59:51 +00:00
|
|
|
#
|
|
|
|
# @using-api no
|
2015-12-30 03:37:48 +00:00
|
|
|
# @results HTML
|
2015-12-30 02:59:51 +00:00
|
|
|
# @stable no
|
|
|
|
# @parse answer
|
|
|
|
|
2016-01-02 07:49:32 +00:00
|
|
|
from re import search, sub
|
2015-12-30 06:53:15 +00:00
|
|
|
from json import loads
|
2015-12-30 02:59:51 +00:00
|
|
|
from urllib import urlencode
|
2016-01-02 07:49:32 +00:00
|
|
|
from lxml import html
|
2016-01-03 21:58:01 +00:00
|
|
|
import HTMLParser
|
2015-12-30 02:59:51 +00:00
|
|
|
|
|
|
|
# search-url
|
|
|
|
url = 'http://www.wolframalpha.com/'
|
|
|
|
search_url = url+'input/?{query}'
|
2016-01-02 07:49:32 +00:00
|
|
|
|
|
|
|
# xpath variables
|
|
|
|
scripts_xpath = '//script'
|
|
|
|
title_xpath = '//title'
|
|
|
|
failure_xpath = '//p[attribute::class="pfail"]'
|
2015-12-30 02:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
|
|
|
params['url'] = search_url.format(query=urlencode({'i': query}))
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
2015-12-30 03:11:49 +00:00
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
2015-12-30 06:53:15 +00:00
|
|
|
line = None
|
2015-12-30 03:37:48 +00:00
|
|
|
|
2016-01-02 07:49:32 +00:00
|
|
|
dom = html.fromstring(resp.text)
|
|
|
|
scripts = dom.xpath(scripts_xpath)
|
|
|
|
|
2015-12-30 03:11:49 +00:00
|
|
|
# the answer is inside a js function
|
|
|
|
# answer can be located in different 'pods', although by default it should be in pod_0200
|
|
|
|
possible_locations = ['pod_0200\.push(.*)\n',
|
|
|
|
'pod_0100\.push(.*)\n']
|
2015-12-30 02:59:51 +00:00
|
|
|
|
2016-01-02 07:49:32 +00:00
|
|
|
# failed result
|
|
|
|
if dom.xpath(failure_xpath):
|
|
|
|
return results
|
|
|
|
|
2015-12-30 02:59:51 +00:00
|
|
|
# get line that matches the pattern
|
2015-12-30 03:11:49 +00:00
|
|
|
for pattern in possible_locations:
|
2016-01-02 07:49:32 +00:00
|
|
|
for script in scripts:
|
|
|
|
try:
|
|
|
|
line = search(pattern, script.text_content()).group(1)
|
|
|
|
break
|
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
if line:
|
2015-12-30 03:11:49 +00:00
|
|
|
break
|
2015-12-30 02:59:51 +00:00
|
|
|
|
2016-01-02 04:02:10 +00:00
|
|
|
if line:
|
|
|
|
# extract answer from json
|
|
|
|
answer = line[line.find('{'):line.rfind('}')+1]
|
|
|
|
answer = loads(answer.encode('unicode-escape'))
|
2016-01-03 21:58:01 +00:00
|
|
|
answer = answer['stringified']
|
|
|
|
|
|
|
|
# clean plaintext answer
|
|
|
|
h = HTMLParser.HTMLParser()
|
|
|
|
answer = h.unescape(answer.decode('unicode-escape'))
|
2016-01-02 07:49:32 +00:00
|
|
|
answer = sub(r'\\', '', answer)
|
2016-01-02 06:41:14 +00:00
|
|
|
|
2016-01-02 04:02:10 +00:00
|
|
|
results.append({'answer': answer})
|
2015-12-30 02:59:51 +00:00
|
|
|
|
2016-01-02 07:49:32 +00:00
|
|
|
# user input is in first part of title
|
|
|
|
title = dom.xpath(title_xpath)[0].text
|
|
|
|
result_url = request(title[:-16], {})['url']
|
2015-12-30 02:59:51 +00:00
|
|
|
|
2016-01-02 04:02:10 +00:00
|
|
|
# append result
|
2016-01-02 07:49:32 +00:00
|
|
|
results.append({'url': result_url,
|
|
|
|
'title': title})
|
2015-12-30 03:37:48 +00:00
|
|
|
|
2015-12-30 02:59:51 +00:00
|
|
|
return results
|