Files
searxng/searx/engines/wolframalpha_noapi.py
T

133 lines
3.6 KiB
Python
Raw Normal View History

2021-01-13 11:31:25 +01:00
# SPDX-License-Identifier: AGPL-3.0-or-later
"""
Wolfram|Alpha (Science)
2021-01-13 11:31:25 +01:00
"""
2015-12-29 20:59:51 -06:00
from json import loads
from urllib.parse import urlencode
2016-02-17 17:07:19 +01:00
from searx.network import get as http_get
from searx.enginelib import EngineCache
2015-12-29 20:59:51 -06:00
2021-01-13 11:31:25 +01:00
# about
about = {
"website": 'https://www.wolframalpha.com/',
"wikidata_id": 'Q207006',
"official_api_documentation": 'https://products.wolframalpha.com/api/',
"use_official_api": False,
"require_api_key": False,
"results": 'JSON',
}
2015-12-29 20:59:51 -06:00
# search-url
2016-02-17 17:07:19 +01:00
url = 'https://www.wolframalpha.com/'
2016-01-02 01:49:32 -06:00
search_url = (
url + 'input/json.jsp'
'?async=false'
'&banners=raw'
'&debuggingdata=false'
'&format=image,plaintext,imagemap,minput,moutput'
'&formattimeout=2'
'&{query}'
'&output=JSON'
'&parsetimeout=2'
'&proxycode={token}'
'&scantimeout=0.5'
'&sponsorcategories=true'
2016-02-17 17:07:19 +01:00
'&statemethod=deploybutton'
)
2016-02-17 17:07:19 +01:00
2016-02-27 19:06:44 -06:00
referer_url = url + 'input/?{query}'
# pods to display as image in infobox
# this pods do return a plaintext, but they look better and are more useful as images
image_pods = {'VisualRepresentation', 'Illustration', 'Symbol'}
2016-02-27 19:06:44 -06:00
2016-02-17 17:07:19 +01:00
CACHE: EngineCache
"""Persistent (SQLite) key/value cache that deletes its values after ``expire``
seconds."""
2016-02-17 17:07:19 +01:00
def init(engine_settings):
global CACHE # pylint: disable=global-statement
CACHE = EngineCache(engine_settings["name"]) # type:ignore
def obtain_token() -> str:
token = CACHE.get(key="token")
if token is None:
resp = http_get('https://www.wolframalpha.com/input/api/v1/code?ts=9999999999999999999', timeout=2.0)
token = resp.json()["code"]
# seems, wolframalpha resets its token in every hour
CACHE.set(key="code", value=token, expire=3600)
return token
2015-12-29 20:59:51 -06:00
def request(query, params):
token = obtain_token()
params['url'] = search_url.format(query=urlencode({'input': query}), token=token)
2016-02-27 19:06:44 -06:00
params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query}))
2015-12-29 20:59:51 -06:00
return params
def response(resp):
2016-02-27 19:06:44 -06:00
results = []
2016-02-17 17:07:19 +01:00
resp_json = loads(resp.text)
if not resp_json['queryresult']['success']:
return []
# handle resp_json['queryresult']['assumptions']?
2016-02-17 17:07:19 +01:00
result_chunks = []
2016-07-07 19:41:33 -04:00
infobox_title = ""
result_content = ""
2016-02-17 17:07:19 +01:00
for pod in resp_json['queryresult']['pods']:
2016-02-28 00:47:36 -06:00
pod_id = pod.get('id', '')
2016-02-17 17:07:19 +01:00
pod_title = pod.get('title', '')
2016-07-06 17:29:40 -05:00
pod_is_result = pod.get('primary', None)
2016-02-27 19:06:44 -06:00
2016-02-17 17:07:19 +01:00
if 'subpods' not in pod:
continue
2016-02-27 19:06:44 -06:00
2016-02-28 00:47:36 -06:00
if pod_id == 'Input' or not infobox_title:
infobox_title = pod['subpods'][0]['plaintext']
2016-02-27 19:06:44 -06:00
2016-02-17 17:07:19 +01:00
for subpod in pod['subpods']:
2016-02-28 00:47:36 -06:00
if subpod['plaintext'] != '' and pod_id not in image_pods:
2016-02-27 19:06:44 -06:00
# append unless it's not an actual answer
if subpod['plaintext'] != '(requires interactivity)':
result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})
2016-07-07 19:41:33 -04:00
if pod_is_result or not result_content:
if pod_id != "Input":
result_content = pod_title + ': ' + subpod['plaintext']
2016-07-06 17:29:40 -05:00
2016-02-27 19:06:44 -06:00
elif 'img' in subpod:
result_chunks.append({'label': pod_title, 'image': subpod['img']})
2016-02-17 17:07:19 +01:00
if not result_chunks:
return []
results.append(
{
'infobox': infobox_title,
'attributes': result_chunks,
'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}],
}
)
results.append(
{
'url': resp.request.headers['Referer'],
'title': 'Wolfram|Alpha (' + infobox_title + ')',
'content': result_content,
}
)
2016-02-27 19:06:44 -06:00
return results