mirror of
https://github.com/searxng/searxng
synced 2024-01-01 18:24:07 +00:00
Wolfram Alpha (no API needed now)
This commit is contained in:
parent
d4b6ab2be4
commit
b51ba32f61
66
searx/engines/wolframalpha_noapi.py
Normal file
66
searx/engines/wolframalpha_noapi.py
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
# WolframAlpha (Maths)
|
||||||
|
#
|
||||||
|
# @website http://www.wolframalpha.com/
|
||||||
|
#
|
||||||
|
# @using-api no
|
||||||
|
# @results HTML, JS
|
||||||
|
# @stable no
|
||||||
|
# @parse answer
|
||||||
|
|
||||||
|
import re
|
||||||
|
import json
|
||||||
|
from urllib import urlencode
|
||||||
|
from lxml import html
|
||||||
|
from searx.engines.xpath import extract_text
|
||||||
|
|
||||||
|
# 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
|
||||||
|
|
||||||
|
|
||||||
|
# tries to find answer under the pattern given
|
||||||
|
def extract_answer(script_list, pattern):
|
||||||
|
answer = None
|
||||||
|
|
||||||
|
# get line that matches the pattern
|
||||||
|
for script in script_list:
|
||||||
|
try:
|
||||||
|
line = re.search(pattern, script.text_content()).group(1)
|
||||||
|
except AttributeError:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# extract answer from json
|
||||||
|
answer = line[line.find('{') : line.rfind('}')+1]
|
||||||
|
answer = json.loads(answer.encode('unicode-escape'))
|
||||||
|
answer = answer['stringified'].decode('unicode-escape')
|
||||||
|
|
||||||
|
return answer
|
||||||
|
|
||||||
|
|
||||||
|
# get response from search-request
|
||||||
|
def response(resp):
|
||||||
|
|
||||||
|
dom = html.fromstring(resp.text)
|
||||||
|
|
||||||
|
# the answer is inside a js script
|
||||||
|
scripts = dom.xpath('//script')
|
||||||
|
|
||||||
|
results = []
|
||||||
|
|
||||||
|
# answer can be located in different 'pods', although by default it should be in pod_0200
|
||||||
|
answer = extract_answer(scripts, 'pod_0200\.push(.*)\n')
|
||||||
|
if not answer:
|
||||||
|
answer = extract_answer(scripts, 'pod_0100\.push(.*)\n')
|
||||||
|
if answer:
|
||||||
|
results.append({'answer': answer})
|
||||||
|
else:
|
||||||
|
results.append({'answer': answer})
|
||||||
|
|
||||||
|
return results
|
@ -302,11 +302,11 @@ engines:
|
|||||||
|
|
||||||
# You can use the engine using the official stable API, but you need an API key
|
# You can use the engine using the official stable API, but you need an API key
|
||||||
# See : http://products.wolframalpha.com/api/
|
# See : http://products.wolframalpha.com/api/
|
||||||
# - name : wolframalpha
|
- name : wolframalpha
|
||||||
# shortcut : wa
|
shortcut : wa
|
||||||
# engine : wolframalpha_api
|
engine : wolframalpha_noapi
|
||||||
# api_key: 'apikey' # required!
|
# api_key: 'apikey' # required!
|
||||||
# timeout: 6.0
|
timeout: 6.0
|
||||||
|
|
||||||
#The blekko technology and team have joined IBM Watson! -> https://blekko.com/
|
#The blekko technology and team have joined IBM Watson! -> https://blekko.com/
|
||||||
# - name : blekko images
|
# - name : blekko images
|
||||||
|
Loading…
Reference in New Issue
Block a user