Ponysearch/searx/engines/wolframalpha_noapi.py

79 lines
2 KiB
Python
Raw Normal View History

2015-12-30 03:59:51 +01:00
# WolframAlpha (Maths)
#
# @website http://www.wolframalpha.com/
2016-01-02 05:02:10 +01:00
# @provide-api yes (http://api.wolframalpha.com/v2/)
2015-12-30 03:59:51 +01:00
#
# @using-api no
# @results HTML
2015-12-30 03:59:51 +01:00
# @stable no
# @parse answer
2016-01-02 08:49:32 +01:00
from re import search, sub
from json import loads
2015-12-30 03:59:51 +01:00
from urllib import urlencode
2016-01-02 08:49:32 +01:00
from lxml import html
2015-12-30 03:59:51 +01:00
# search-url
url = 'http://www.wolframalpha.com/'
search_url = url+'input/?{query}'
2016-01-02 08:49:32 +01:00
# xpath variables
scripts_xpath = '//script'
title_xpath = '//title'
failure_xpath = '//p[attribute::class="pfail"]'
2015-12-30 03:59:51 +01:00
# do search-request
def request(query, params):
params['url'] = search_url.format(query=urlencode({'i': query}))
return params
# get response from search-request
def response(resp):
results = []
line = None
2016-01-02 08:49:32 +01:00
dom = html.fromstring(resp.text)
scripts = dom.xpath(scripts_xpath)
# the answer is inside a js function
# answer can be located in different 'pods', although by default it should be in pod_0200
possible_locations = ['pod_0200\.push(.*)\n',
'pod_0100\.push(.*)\n']
2015-12-30 03:59:51 +01:00
2016-01-02 08:49:32 +01:00
# failed result
if dom.xpath(failure_xpath):
return results
2015-12-30 03:59:51 +01:00
# get line that matches the pattern
for pattern in possible_locations:
2016-01-02 08:49:32 +01:00
for script in scripts:
try:
line = search(pattern, script.text_content()).group(1)
break
except AttributeError:
continue
if line:
break
2015-12-30 03:59:51 +01:00
2016-01-02 05:02:10 +01:00
if line:
# extract answer from json
answer = line[line.find('{'):line.rfind('}')+1]
answer = loads(answer.encode('unicode-escape'))
answer = answer['stringified'].decode('unicode-escape')
2016-01-02 08:49:32 +01:00
answer = sub(r'\\', '', answer)
2016-01-02 07:41:14 +01:00
2016-01-02 05:02:10 +01:00
results.append({'answer': answer})
2015-12-30 03:59:51 +01:00
2016-01-02 08:49:32 +01:00
# user input is in first part of title
title = dom.xpath(title_xpath)[0].text
result_url = request(title[:-16], {})['url']
2015-12-30 03:59:51 +01:00
2016-01-02 05:02:10 +01:00
# append result
2016-01-02 08:49:32 +01:00
results.append({'url': result_url,
'title': title})
2015-12-30 03:59:51 +01:00
return results