@@ -1,23 +1,23 @@
1−# WolframAlpha (Maths)
1+# Wolfram|Alpha (Science)
22#
3−# @website http://www.wolframalpha.com/
4−# @provide-api yes (http://api.wolframalpha.com/v2/)
3+# @website https://www.wolframalpha.com/
4+# @provide-api yes (https://api.wolframalpha.com/v2/)
55#
66# @using-api no
7−# @results HTML
7+# @results JSON
88# @stable no
9−# @parse answer
9+# @parse url, infobox
1010
1111from cgi import escape
1212from json import loads
1313from time import time
1414from urllib import urlencode
15+from lxml.etree import XML
1516
1617from searx.poolrequests import get as http_get
1718
1819# search-url
1920url = 'https://www.wolframalpha.com/'
20−search_url = url + 'input/?{query}'
2121
2222search_url = url + 'input/json.jsp'\
2323 '?async=true'\
@@ -33,13 +33,25 @@ search_url = url + 'input/json.jsp'\
3333 '&sponsorcategories=true'\
3434 '&statemethod=deploybutton'
3535
36−# xpath variables
37−scripts_xpath = '//script'
38−title_xpath = '//title'
39−failure_xpath = '//p[attribute::class="pfail"]'
36+referer_url = url + 'input/?{query}'
37+
4038token = {'value': '',
4139 'last_updated': None}
4240
41+# xpath variables
42+success_xpath = '/pod[attribute::error="false"]'
43+plaintext_xpath = './plaintext'
44+title_xpath = './@title'
45+image_xpath = './img'
46+img_src_xpath = './img/@src'
47+img_alt_xpath = './img/@alt'
48+
49+# pods to display as image in infobox
50+# this pods do return a plaintext, but they look better and are more useful as images
51+image_pods = {'Visual representation',
52+ 'Manipulatives illustration',
53+ 'Symbol'}
54+
4355
4456# seems, wolframalpha resets its token in every hour
4557def obtain_token():
@@ -62,13 +74,42 @@ def request(query, params):
6274 if time() - token['last_updated'] > 3600:
6375 obtain_token()
6476 params['url'] = search_url.format(query=urlencode({'input': query}), token=token['value'])
65− params['headers']['Referer'] = 'https://www.wolframalpha.com/input/?i=' + query
77+ params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query}))
6678
6779 return params
6880
6981
82+# get additional pod
83+# NOTE: this makes an additional requests to server, so the response will take longer and might reach timeout
84+def get_async_pod(url):
85+ pod = {'subpods': []}
86+
87+ try:
88+ resp = http_get(url, timeout=2.0)
89+
90+ resp_pod = XML(resp.content)
91+ if resp_pod.xpath(success_xpath):
92+
93+ for subpod in resp_pod:
94+ plaintext = subpod.xpath(plaintext_xpath)[0].text
95+ if plaintext:
96+ pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
97+ 'plaintext': plaintext})
98+ elif subpod.xpath(image_xpath):
99+ pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
100+ 'plaintext': '',
101+ 'img': {'src': subpod.xpath(img_src_xpath)[0],
102+ 'alt': subpod.xpath(img_alt_xpath)[0]}})
103+ except:
104+ pass
105+
106+ return pod
107+
108+
70109# get response from search-request
71110def response(resp):
111+ results = []
112+
72113 resp_json = loads(resp.text)
73114
74115 if not resp_json['queryresult']['success']:
@@ -76,20 +117,45 @@ def response(resp):
76117
77118 # TODO handle resp_json['queryresult']['assumptions']
78119 result_chunks = []
120+ infobox_title = None
79121 for pod in resp_json['queryresult']['pods']:
80122 pod_title = pod.get('title', '')
123+
81124 if 'subpods' not in pod:
82− continue
125+ # comment this section if your requests always reach timeout
126+ if pod['async']:
127+ result = get_async_pod(pod['async'])
128+ if result:
129+ pod = result
130+ else:
131+ continue
132+
133+ # infobox title is input or text content on first pod
134+ if pod_title.startswith('Input') or not infobox_title:
135+ try:
136+ infobox_title = pod['subpods'][0]['plaintext']
137+ except:
138+ infobox_title = ''
139+ pass
140+
83141 for subpod in pod['subpods']:
84− if 'img' in subpod:
85− result_chunks.append(u'<p>{0}<br /><img src="{1}" alt="{2}" /></p>'
86− .format(escape(pod_title or subpod['img']['alt']),
87− escape(subpod['img']['src']),
88− escape(subpod['img']['alt'])))
142+ if subpod['plaintext'] != '' and pod_title not in image_pods:
143+ # append unless it's not an actual answer
144+ if subpod['plaintext'] != '(requires interactivity)':
145+ result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})
146+
147+ elif 'img' in subpod:
148+ result_chunks.append({'label': pod_title, 'image': subpod['img']})
89149
90150 if not result_chunks:
91151 return []
92152
93− return [{'url': resp.request.headers['Referer'].decode('utf-8'),
94− 'title': 'Wolframalpha',
95− 'content': ''.join(result_chunks)}]
153+ results.append({'infobox': infobox_title,
154+ 'attributes': result_chunks,
155+ 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})
156+
157+ results.append({'url': resp.request.headers['Referer'],
158+ 'title': 'Wolfram|Alpha',
159+ 'content': infobox_title})
160+
161+ return results