.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.wolframalpha_api Namespace Reference

Functions

 request (query, params)
 
 replace_pua_chars (text)
 
 response (resp)
 

Variables

dict about
 
str search_url = 'https://api.wolframalpha.com/v2/query?appid={api_key}&{query}'
 
str site_url = 'https://www.wolframalpha.com/input/?{query}'
 
str api_key = ''
 
str failure_xpath = '/queryresult[attribute::success="false"]'
 
str input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'
 
str pods_xpath = '//pod'
 
str subpods_xpath = './subpod'
 
str pod_primary_xpath = './@primary'
 
str pod_id_xpath = './@id'
 
str pod_title_xpath = './@title'
 
str plaintext_xpath = './plaintext'
 
str image_xpath = './img'
 
str img_src_xpath = './@src'
 
str img_alt_xpath = './@alt'
 
dict image_pods = {'VisualRepresentation', 'Illustration'}
 

Detailed Description

Wolfram|Alpha (Science)

Function Documentation

◆ replace_pua_chars()

searx.engines.wolframalpha_api.replace_pua_chars ( text)

Definition at line 52 of file wolframalpha_api.py.

52def replace_pua_chars(text):
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

Referenced by searx.engines.wolframalpha_api.response().

+ Here is the caller graph for this function:

◆ request()

searx.engines.wolframalpha_api.request ( query,
params )

Definition at line 44 of file wolframalpha_api.py.

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

◆ response()

searx.engines.wolframalpha_api.response ( resp)

Definition at line 72 of file wolframalpha_api.py.

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

References searx.engines.wolframalpha_api.replace_pua_chars().

+ Here is the call graph for this function:

Variable Documentation

◆ about

dict searx.engines.wolframalpha_api.about
Initial value:
1= {
2 "website": 'https://www.wolframalpha.com',
3 "wikidata_id": 'Q207006',
4 "official_api_documentation": 'https://products.wolframalpha.com/api/',
5 "use_official_api": True,
6 "require_api_key": False,
7 "results": 'XML',
8}

Definition at line 11 of file wolframalpha_api.py.

◆ api_key

str searx.engines.wolframalpha_api.api_key = ''

Definition at line 23 of file wolframalpha_api.py.

◆ failure_xpath

str searx.engines.wolframalpha_api.failure_xpath = '/queryresult[attribute::success="false"]'

Definition at line 26 of file wolframalpha_api.py.

◆ image_pods

dict searx.engines.wolframalpha_api.image_pods = {'VisualRepresentation', 'Illustration'}

Definition at line 40 of file wolframalpha_api.py.

◆ image_xpath

str searx.engines.wolframalpha_api.image_xpath = './img'

Definition at line 34 of file wolframalpha_api.py.

◆ img_alt_xpath

str searx.engines.wolframalpha_api.img_alt_xpath = './@alt'

Definition at line 36 of file wolframalpha_api.py.

◆ img_src_xpath

str searx.engines.wolframalpha_api.img_src_xpath = './@src'

Definition at line 35 of file wolframalpha_api.py.

◆ input_xpath

str searx.engines.wolframalpha_api.input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'

Definition at line 27 of file wolframalpha_api.py.

◆ plaintext_xpath

str searx.engines.wolframalpha_api.plaintext_xpath = './plaintext'

Definition at line 33 of file wolframalpha_api.py.

◆ pod_id_xpath

str searx.engines.wolframalpha_api.pod_id_xpath = './@id'

Definition at line 31 of file wolframalpha_api.py.

◆ pod_primary_xpath

str searx.engines.wolframalpha_api.pod_primary_xpath = './@primary'

Definition at line 30 of file wolframalpha_api.py.

◆ pod_title_xpath

str searx.engines.wolframalpha_api.pod_title_xpath = './@title'

Definition at line 32 of file wolframalpha_api.py.

◆ pods_xpath

str searx.engines.wolframalpha_api.pods_xpath = '//pod'

Definition at line 28 of file wolframalpha_api.py.

◆ search_url

str searx.engines.wolframalpha_api.search_url = 'https://api.wolframalpha.com/v2/query?appid={api_key}&{query}'

Definition at line 21 of file wolframalpha_api.py.

◆ site_url

str searx.engines.wolframalpha_api.site_url = 'https://www.wolframalpha.com/input/?{query}'

Definition at line 22 of file wolframalpha_api.py.

◆ subpods_xpath

str searx.engines.wolframalpha_api.subpods_xpath = './subpod'

Definition at line 29 of file wolframalpha_api.py.