.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
wolframalpha_api.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2"""Wolfram|Alpha (Science)
3
4"""
5
6from urllib.parse import urlencode
7
8from lxml import etree
9
10# about
11about = {
12 "website": 'https://www.wolframalpha.com',
13 "wikidata_id": 'Q207006',
14 "official_api_documentation": 'https://products.wolframalpha.com/api/',
15 "use_official_api": True,
16 "require_api_key": False,
17 "results": 'XML',
18}
19
20# search-url
21search_url = 'https://api.wolframalpha.com/v2/query?appid={api_key}&{query}'
22site_url = 'https://www.wolframalpha.com/input/?{query}'
23api_key = '' # defined in settings.yml
24
25# xpath variables
26failure_xpath = '/queryresult[attribute::success="false"]'
27input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'
28pods_xpath = '//pod'
29subpods_xpath = './subpod'
30pod_primary_xpath = './@primary'
31pod_id_xpath = './@id'
32pod_title_xpath = './@title'
33plaintext_xpath = './plaintext'
34image_xpath = './img'
35img_src_xpath = './@src'
36img_alt_xpath = './@alt'
37
38# pods to display as image in infobox
39# this pods do return a plaintext, but they look better and are more useful as images
40image_pods = {'VisualRepresentation', 'Illustration'}
41
42
43# do search-request
44def request(query, params):
45 params['url'] = search_url.format(query=urlencode({'input': query}), api_key=api_key)
46 params['headers']['Referer'] = site_url.format(query=urlencode({'i': query}))
47
48 return params
49
50
51# replace private user area characters to make text legible
53 pua_chars = {
54 '\uf522': '\u2192', # right arrow
55 '\uf7b1': '\u2115', # set of natural numbers
56 '\uf7b4': '\u211a', # set of rational numbers
57 '\uf7b5': '\u211d', # set of real numbers
58 '\uf7bd': '\u2124', # set of integer numbers
59 '\uf74c': 'd', # differential
60 '\uf74d': '\u212f', # euler's number
61 '\uf74e': 'i', # imaginary number
62 '\uf7d9': '=',
63 } # equals sign
64
65 for k, v in pua_chars.items():
66 text = text.replace(k, v)
67
68 return text
69
70
71# get response from search-request
72def response(resp):
73 results = []
74
75 search_results = etree.XML(resp.content)
76
77 # return empty array if there are no results
78 if search_results.xpath(failure_xpath):
79 return []
80
81 try:
82 infobox_title = search_results.xpath(input_xpath)[0].text
83 except: # pylint: disable=bare-except
84 infobox_title = ""
85
86 pods = search_results.xpath(pods_xpath)
87 result_chunks = []
88 result_content = ""
89 for pod in pods:
90 pod_id = pod.xpath(pod_id_xpath)[0]
91 pod_title = pod.xpath(pod_title_xpath)[0]
92 pod_is_result = pod.xpath(pod_primary_xpath)
93
94 subpods = pod.xpath(subpods_xpath)
95 if not subpods:
96 continue
97
98 # Appends either a text or an image, depending on which one is more suitable
99 for subpod in subpods:
100 content = subpod.xpath(plaintext_xpath)[0].text
101 image = subpod.xpath(image_xpath)
102
103 if content and pod_id not in image_pods:
104
105 if pod_is_result or not result_content:
106 if pod_id != "Input":
107 result_content = "%s: %s" % (pod_title, content)
108
109 # if no input pod was found, title is first plaintext pod
110 if not infobox_title:
111 infobox_title = content
112
113 content = replace_pua_chars(content)
114 result_chunks.append({'label': pod_title, 'value': content})
115
116 elif image:
117 result_chunks.append(
118 {
119 'label': pod_title,
120 'image': {'src': image[0].xpath(img_src_xpath)[0], 'alt': image[0].xpath(img_alt_xpath)[0]},
121 }
122 )
123
124 if not result_chunks:
125 return []
126
127 title = "Wolfram Alpha (%s)" % infobox_title
128
129 # append infobox
130 results.append(
131 {
132 'infobox': infobox_title,
133 'attributes': result_chunks,
134 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}],
135 }
136 )
137
138 # append link to site
139 results.append({'url': resp.request.headers['Referer'], 'title': title, 'content': result_content})
140
141 return results