.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 json import loads
7from time import time
8from urllib.parse import urlencode
9
10from searx.network import get as http_get
11
12# about
13about = {
14 "website": 'https://www.wolframalpha.com/',
15 "wikidata_id": 'Q207006',
16 "official_api_documentation": 'https://products.wolframalpha.com/api/',
17 "use_official_api": False,
18 "require_api_key": False,
19 "results": 'JSON',
20}
21
22# search-url
23url = 'https://www.wolframalpha.com/'
24
25search_url = (
26 url + 'input/json.jsp'
27 '?async=false'
28 '&banners=raw'
29 '&debuggingdata=false'
30 '&format=image,plaintext,imagemap,minput,moutput'
31 '&formattimeout=2'
32 '&{query}'
33 '&output=JSON'
34 '&parsetimeout=2'
35 '&proxycode={token}'
36 '&scantimeout=0.5'
37 '&sponsorcategories=true'
38 '&statemethod=deploybutton'
39)
40
41referer_url = url + 'input/?{query}'
42
43token = {'value': '', 'last_updated': None}
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
50# seems, wolframalpha resets its token in every hour
52 update_time = time() - (time() % 3600)
53 try:
54 token_response = http_get('https://www.wolframalpha.com/input/api/v1/code?ts=9999999999999999999', timeout=2.0)
55 token['value'] = loads(token_response.text)['code']
56 token['last_updated'] = update_time
57 except: # pylint: disable=bare-except
58 pass
59 return token
60
61
62def init(engine_settings=None): # pylint: disable=unused-argument
64
65
66# do search-request
67def request(query, params):
68 # obtain token if last update was more than an hour
69 if time() - (token['last_updated'] or 0) > 3600:
71 params['url'] = search_url.format(query=urlencode({'input': query}), token=token['value'])
72 params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query}))
73
74 return params
75
76
77# get response from search-request
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