2015-12-30 02:59:51 +00:00
|
|
|
# WolframAlpha (Maths)
|
|
|
|
#
|
|
|
|
# @website http://www.wolframalpha.com/
|
2016-01-02 04:02:10 +00:00
|
|
|
# @provide-api yes (http://api.wolframalpha.com/v2/)
|
2015-12-30 02:59:51 +00:00
|
|
|
#
|
|
|
|
# @using-api no
|
2015-12-30 03:37:48 +00:00
|
|
|
# @results HTML
|
2015-12-30 02:59:51 +00:00
|
|
|
# @stable no
|
|
|
|
# @parse answer
|
|
|
|
|
2016-02-17 16:07:19 +00:00
|
|
|
from cgi import escape
|
2015-12-30 06:53:15 +00:00
|
|
|
from json import loads
|
2016-02-17 16:07:19 +00:00
|
|
|
from time import time
|
2015-12-30 02:59:51 +00:00
|
|
|
from urllib import urlencode
|
2016-02-17 16:07:19 +00:00
|
|
|
|
|
|
|
from searx.poolrequests import get as http_get
|
2015-12-30 02:59:51 +00:00
|
|
|
|
|
|
|
# search-url
|
2016-02-17 16:07:19 +00:00
|
|
|
url = 'https://www.wolframalpha.com/'
|
2016-01-18 17:34:38 +00:00
|
|
|
search_url = url + 'input/?{query}'
|
2016-01-02 07:49:32 +00:00
|
|
|
|
2016-02-17 16:07:19 +00:00
|
|
|
search_url = url + 'input/json.jsp'\
|
|
|
|
'?async=true'\
|
|
|
|
'&banners=raw'\
|
|
|
|
'&debuggingdata=false'\
|
|
|
|
'&format=image,plaintext,imagemap,minput,moutput'\
|
|
|
|
'&formattimeout=2'\
|
|
|
|
'&{query}'\
|
|
|
|
'&output=JSON'\
|
|
|
|
'&parsetimeout=2'\
|
|
|
|
'&proxycode={token}'\
|
|
|
|
'&scantimeout=0.5'\
|
|
|
|
'&sponsorcategories=true'\
|
|
|
|
'&statemethod=deploybutton'
|
|
|
|
|
2016-01-02 07:49:32 +00:00
|
|
|
# xpath variables
|
|
|
|
scripts_xpath = '//script'
|
|
|
|
title_xpath = '//title'
|
|
|
|
failure_xpath = '//p[attribute::class="pfail"]'
|
2016-02-17 16:07:19 +00:00
|
|
|
token = {'value': '',
|
|
|
|
'last_updated': None}
|
|
|
|
|
|
|
|
|
|
|
|
# seems, wolframalpha resets its token in every hour
|
|
|
|
def obtain_token():
|
|
|
|
update_time = time() - (time() % 3600)
|
2016-02-17 16:21:54 +00:00
|
|
|
try:
|
|
|
|
token_response = http_get('https://www.wolframalpha.com/input/api/v1/code?ts=9999999999999999999', timeout=2.0)
|
|
|
|
token['value'] = loads(token_response.text)['code']
|
|
|
|
token['last_updated'] = update_time
|
|
|
|
except:
|
|
|
|
pass
|
2016-02-17 16:07:19 +00:00
|
|
|
return token
|
|
|
|
|
|
|
|
|
|
|
|
obtain_token()
|
2015-12-30 02:59:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
# do search-request
|
|
|
|
def request(query, params):
|
2016-02-17 16:07:19 +00:00
|
|
|
# obtain token if last update was more than an hour
|
|
|
|
if time() - token['last_updated'] > 3600:
|
|
|
|
obtain_token()
|
|
|
|
params['url'] = search_url.format(query=urlencode({'input': query}), token=token['value'])
|
|
|
|
params['headers']['Referer'] = 'https://www.wolframalpha.com/input/?i=' + query
|
2015-12-30 02:59:51 +00:00
|
|
|
|
|
|
|
return params
|
|
|
|
|
|
|
|
|
2015-12-30 03:11:49 +00:00
|
|
|
# get response from search-request
|
|
|
|
def response(resp):
|
2016-02-17 16:07:19 +00:00
|
|
|
resp_json = loads(resp.text)
|
|
|
|
|
|
|
|
if not resp_json['queryresult']['success']:
|
|
|
|
return []
|
|
|
|
|
|
|
|
# TODO handle resp_json['queryresult']['assumptions']
|
|
|
|
result_chunks = []
|
|
|
|
for pod in resp_json['queryresult']['pods']:
|
|
|
|
pod_title = pod.get('title', '')
|
|
|
|
if 'subpods' not in pod:
|
|
|
|
continue
|
|
|
|
for subpod in pod['subpods']:
|
|
|
|
if 'img' in subpod:
|
|
|
|
result_chunks.append(u'<p>{0}<br /><img src="{1}" alt="{2}" /></p>'
|
|
|
|
.format(escape(pod_title or subpod['img']['alt']),
|
|
|
|
escape(subpod['img']['src']),
|
|
|
|
escape(subpod['img']['alt'])))
|
|
|
|
|
|
|
|
if not result_chunks:
|
|
|
|
return []
|
|
|
|
|
2016-02-25 13:55:26 +00:00
|
|
|
return [{'url': resp.request.headers['Referer'].decode('utf-8'),
|
2016-02-17 16:07:19 +00:00
|
|
|
'title': 'Wolframalpha',
|
|
|
|
'content': ''.join(result_chunks)}]
|