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