2015-12-30 02:59:51 +00:00
|
|
|
# WolframAlpha (Maths)
|
|
|
|
#
|
|
|
|
# @website http://www.wolframalpha.com/
|
|
|
|
#
|
|
|
|
# @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
|
|
|
|
|
2015-12-30 06:53:15 +00:00
|
|
|
from re import search
|
|
|
|
from json import loads
|
2015-12-30 02:59:51 +00:00
|
|
|
from urllib import urlencode
|
|
|
|
|
|
|
|
# search-url
|
|
|
|
url = 'http://www.wolframalpha.com/'
|
|
|
|
search_url = url+'input/?{query}'
|
|
|
|
|
|
|
|
|
|
|
|
# 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
|
|
|
webpage = resp.text
|
|
|
|
line = None
|
2015-12-30 03:37:48 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
# get line that matches the pattern
|
2015-12-30 03:11:49 +00:00
|
|
|
for pattern in possible_locations:
|
2015-12-30 02:59:51 +00:00
|
|
|
try:
|
2015-12-30 06:53:15 +00:00
|
|
|
line = search(pattern, webpage).group(1)
|
2015-12-30 03:11:49 +00:00
|
|
|
break
|
2015-12-30 02:59:51 +00:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
2015-12-30 03:11:49 +00:00
|
|
|
if not line:
|
|
|
|
return results
|
2015-12-30 02:59:51 +00:00
|
|
|
|
2015-12-30 03:11:49 +00:00
|
|
|
# extract answer from json
|
2015-12-30 03:37:48 +00:00
|
|
|
answer = line[line.find('{'):line.rfind('}')+1]
|
2015-12-30 06:53:15 +00:00
|
|
|
answer = loads(answer.encode('unicode-escape'))
|
2015-12-30 03:11:49 +00:00
|
|
|
answer = answer['stringified'].decode('unicode-escape')
|
2015-12-30 02:59:51 +00:00
|
|
|
|
2015-12-30 03:11:49 +00:00
|
|
|
results.append({'answer': answer})
|
2015-12-30 03:37:48 +00:00
|
|
|
|
2015-12-30 02:59:51 +00:00
|
|
|
return results
|