@@ -14,14 +14,24 @@ from lxml import etree
1414# search-url
1515base_url = 'http://api.wolframalpha.com/v2/query'
1616search_url = base_url + '?appid={api_key}&{query}&format=plaintext'
17+site_url = 'http://www.wolframalpha.com/input/?{query}'
18+search_query = ''
1719api_key = ''
1820
21+# xpath variables
22+failure_xpath = '/queryresult[attribute::success="false"]'
23+answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
24+
1925
2026# do search-request
2127def request(query, params):
2228 params['url'] = search_url.format(query=urlencode({'input': query}),
2329 api_key=api_key)
2430
31+ # used in response
32+ global search_query
33+ search_query = query
34+
2535 return params
2636
2737
@@ -45,19 +55,21 @@ def response(resp):
4555 search_results = etree.XML(resp.content)
4656
4757 # return empty array if there are no results
48− if search_results.xpath('/queryresult[attribute::success="false"]'):
58+ if search_results.xpath(failure_xpath):
4959 return []
5060
5161 # parse answer
52− answer = search_results.xpath('//pod[attribute::primary="true"]/subpod/plaintext')
53− if not answer:
54− return results
62+ answer = search_results.xpath(answer_xpath)
63+ if answer:
64+ answer = replace_pua_chars(answer[0].text)
65+
66+ results.append({'answer': answer})
5567
56− answer = replace_pua_chars(answer[0].text)
68+ # result url
69+ result_url = site_url.format(query=urlencode({'i': search_query}))
5770
5871 # append result
59− # TODO: shouldn't it bind the source too?
60− results.append({'answer': answer})
72+ results.append({'url': result_url,
73+ 'title': search_query + ' - Wolfram|Alpha'})
6174
62− # return results
6375 return results