1
0
mirror of https://github.com/searxng/searxng.git synced 2024-11-09 14:40:11 +01:00
searxng/searx/engines/wolframalpha_noapi.py

56 lines
1.3 KiB
Python
Raw Normal View History

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