@@ -8,60 +8,71 @@
88# @stable no
99# @parse answer
1010
11−from re import search
11+from re import search, sub
1212from json import loads
1313from urllib import urlencode
14+from lxml import html
1415
1516# search-url
1617url = 'http://www.wolframalpha.com/'
1718search_url = url+'input/?{query}'
18−search_query = ''
19+
20+# xpath variables
21+scripts_xpath = '//script'
22+title_xpath = '//title'
23+failure_xpath = '//p[attribute::class="pfail"]'
1924
2025
2126# do search-request
2227def request(query, params):
2328 params['url'] = search_url.format(query=urlencode({'i': query}))
2429
25− # used in response
26− global search_query
27− search_query = query
28−
2930 return params
3031
3132
3233# get response from search-request
3334def response(resp):
3435 results = []
35− webpage = resp.text
3636 line = None
3737
38+ dom = html.fromstring(resp.text)
39+ scripts = dom.xpath(scripts_xpath)
40+
3841 # the answer is inside a js function
3942 # answer can be located in different 'pods', although by default it should be in pod_0200
4043 possible_locations = ['pod_0200\.push(.*)\n',
4144 'pod_0100\.push(.*)\n']
4245
46+ # failed result
47+ if dom.xpath(failure_xpath):
48+ return results
49+
4350 # get line that matches the pattern
4451 for pattern in possible_locations:
45− try:
46− line = search(pattern, webpage).group(1)
52+ for script in scripts:
53+ try:
54+ line = search(pattern, script.text_content()).group(1)
55+ break
56+ except AttributeError:
57+ continue
58+ if line:
4759 break
48− except AttributeError:
49− continue
5060
5161 if line:
5262 # extract answer from json
5363 answer = line[line.find('{'):line.rfind('}')+1]
5464 answer = loads(answer.encode('unicode-escape'))
5565 answer = answer['stringified'].decode('unicode-escape')
66+ answer = sub(r'\\', '', answer)
5667
5768 results.append({'answer': answer})
5869
59− # failed result
60− elif search('pfail', webpage):
61− return results
70+ # user input is in first part of title
71+ title = dom.xpath(title_xpath)[0].text
72+ result_url = request(title[:-16], {})['url']
6273
6374 # append result
64− results.append({'url': request(search_query, {})['url'],
65− 'title': search_query + ' - Wolfram|Alpha'})
75+ results.append({'url': result_url,
76+ 'title': title})
6677
6778 return results