.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
wolframalpha_noapi.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""
3 Wolfram|Alpha (Science)
4"""
5
6from __future__ import annotations
7
8from json import loads
9from urllib.parse import urlencode
10
11from searx.network import get as http_get
12from searx.enginelib import EngineCache
13
14# about
15about = {
16 "website": 'https://www.wolframalpha.com/',
17 "wikidata_id": 'Q207006',
18 "official_api_documentation": 'https://products.wolframalpha.com/api/',
19 "use_official_api": False,
20 "require_api_key": False,
21 "results": 'JSON',
22}
23
24# search-url
25url = 'https://www.wolframalpha.com/'
26
27search_url = (
28 url + 'input/json.jsp'
29 '?async=false'
30 '&banners=raw'
31 '&debuggingdata=false'
32 '&format=image,plaintext,imagemap,minput,moutput'
33 '&formattimeout=2'
34 '&{query}'
35 '&output=JSON'
36 '&parsetimeout=2'
37 '&proxycode={token}'
38 '&scantimeout=0.5'
39 '&sponsorcategories=true'
40 '&statemethod=deploybutton'
41)
42
43referer_url = url + 'input/?{query}'
44
45# pods to display as image in infobox
46# this pods do return a plaintext, but they look better and are more useful as images
47image_pods = {'VisualRepresentation', 'Illustration', 'Symbol'}
48
49
50CACHE: EngineCache
51"""Persistent (SQLite) key/value cache that deletes its values after ``expire``
52seconds."""
53
54
55def init(engine_settings):
56 global CACHE # pylint: disable=global-statement
57 CACHE = EngineCache(engine_settings["name"]) # type:ignore
58
59
60def obtain_token() -> str:
61 token = CACHE.get(key="token")
62 if token is None:
63 resp = http_get('https://www.wolframalpha.com/input/api/v1/code?ts=9999999999999999999', timeout=2.0)
64 token = resp.json()["code"]
65 # seems, wolframalpha resets its token in every hour
66 CACHE.set(key="code", value=token, expire=3600)
67 return token
68
69
70def request(query, params):
71 token = obtain_token()
72 params['url'] = search_url.format(query=urlencode({'input': query}), token=token)
73 params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query}))
74
75 return params
76
77
78def response(resp):
79 results = []
80
81 resp_json = loads(resp.text)
82
83 if not resp_json['queryresult']['success']:
84 return []
85
86 # handle resp_json['queryresult']['assumptions']?
87 result_chunks = []
88 infobox_title = ""
89 result_content = ""
90 for pod in resp_json['queryresult']['pods']:
91 pod_id = pod.get('id', '')
92 pod_title = pod.get('title', '')
93 pod_is_result = pod.get('primary', None)
94
95 if 'subpods' not in pod:
96 continue
97
98 if pod_id == 'Input' or not infobox_title:
99 infobox_title = pod['subpods'][0]['plaintext']
100
101 for subpod in pod['subpods']:
102 if subpod['plaintext'] != '' and pod_id not in image_pods:
103 # append unless it's not an actual answer
104 if subpod['plaintext'] != '(requires interactivity)':
105 result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})
106
107 if pod_is_result or not result_content:
108 if pod_id != "Input":
109 result_content = pod_title + ': ' + subpod['plaintext']
110
111 elif 'img' in subpod:
112 result_chunks.append({'label': pod_title, 'image': subpod['img']})
113
114 if not result_chunks:
115 return []
116
117 results.append(
118 {
119 'infobox': infobox_title,
120 'attributes': result_chunks,
121 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}],
122 }
123 )
124
125 results.append(
126 {
127 'url': resp.request.headers['Referer'],
128 'title': 'Wolfram|Alpha (' + infobox_title + ')',
129 'content': result_content,
130 }
131 )
132
133 return results