2015-12-30 03:59:51 +01:00
|
|
|
# WolframAlpha (Maths)
|
|
|
|
#
|
|
|
|
# @website http://www.wolframalpha.com/
|
|
|
|
#
|
|
|
|
# @using-api no
|
2015-12-30 04:37:48 +01:00
|
|
|
# @results HTML
|
2015-12-30 03:59:51 +01:00
|
|
|
# @stable no
|
|
|
|
# @parse answer
|
|
|
|
|
2015-12-30 07:53:15 +01:00
|
|
|
from re import search
|
|
|
|
from json import loads
|
2015-12-30 03:59:51 +01: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 04:11:49 +01:00
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
|
|
|
results = []
|
2015-12-30 07:53:15 +01:00
|
|
|
webpage = resp.text
|
|
|
|
line = None
|
2015-12-30 04:37:48 +01:00
|
|
|
|
2015-12-30 04:11:49 +01: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 03:59:51 +01:00
|
|
|
|
|
|
|
# get line that matches the pattern
|
2015-12-30 04:11:49 +01:00
|
|
|
for pattern in possible_locations:
|
2015-12-30 03:59:51 +01:00
|
|
|
try:
|
2015-12-30 07:53:15 +01:00
|
|
|
line = search(pattern, webpage).group(1)
|
2015-12-30 04:11:49 +01:00
|
|
|
break
|
2015-12-30 03:59:51 +01:00
|
|
|
except AttributeError:
|
|
|
|
continue
|
|
|
|
|
2015-12-30 04:11:49 +01:00
|
|
|
if not line:
|
|
|
|
return results
|
2015-12-30 03:59:51 +01:00
|
|
|
|
2015-12-30 04:11:49 +01:00
|
|
|
# extract answer from json
|
2015-12-30 04:37:48 +01:00
|
|
|
answer = line[line.find('{'):line.rfind('}')+1]
|
2015-12-30 07:53:15 +01:00
|
|
|
answer = loads(answer.encode('unicode-escape'))
|
2015-12-30 04:11:49 +01:00
|
|
|
answer = answer['stringified'].decode('unicode-escape')
|
2015-12-30 03:59:51 +01:00
|
|
|
|
2015-12-30 04:11:49 +01:00
|
|
|
results.append({'answer': answer})
|
2015-12-30 04:37:48 +01:00
|
|
|
|
2015-12-30 03:59:51 +01:00
|
|
|
return results
|