123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
-
-
-
-
-
-
-
-
-
-
- from cgi import escape
- from json import loads
- from time import time
- from urllib import urlencode
- from lxml.etree import XML
-
- from searx.poolrequests import get as http_get
-
-
- url = 'https://www.wolframalpha.com/'
-
- 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'
-
- referer_url = url + 'input/?{query}'
-
- token = {'value': '',
- 'last_updated': None}
-
-
- success_xpath = '/pod[attribute::error="false"]'
- plaintext_xpath = './plaintext'
- title_xpath = './@title'
- image_xpath = './img'
- img_src_xpath = './img/@src'
- img_alt_xpath = './img/@alt'
-
-
-
- image_pods = {'Visual representation',
- 'Manipulatives illustration',
- 'Symbol'}
-
-
-
- def obtain_token():
- update_time = time() - (time() % 3600)
- 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
- return token
-
-
- obtain_token()
-
-
-
- def request(query, params):
-
- if time() - token['last_updated'] > 3600:
- obtain_token()
- params['url'] = search_url.format(query=urlencode({'input': query}), token=token['value'])
- params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query}))
-
- return params
-
-
-
-
- def get_async_pod(url):
- pod = {'subpods': []}
-
- try:
- resp = http_get(url, timeout=2.0)
-
- resp_pod = XML(resp.content)
- if resp_pod.xpath(success_xpath):
-
- for subpod in resp_pod:
- plaintext = subpod.xpath(plaintext_xpath)[0].text
- if plaintext:
- pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
- 'plaintext': plaintext})
- elif subpod.xpath(image_xpath):
- pod['subpods'].append({'title': subpod.xpath(title_xpath)[0],
- 'plaintext': '',
- 'img': {'src': subpod.xpath(img_src_xpath)[0],
- 'alt': subpod.xpath(img_alt_xpath)[0]}})
- except:
- pass
-
- return pod
-
-
-
- def response(resp):
- results = []
-
- resp_json = loads(resp.text)
-
- if not resp_json['queryresult']['success']:
- return []
-
-
- result_chunks = []
- infobox_title = None
- for pod in resp_json['queryresult']['pods']:
- pod_title = pod.get('title', '')
-
- if 'subpods' not in pod:
-
- if pod['async']:
- result = get_async_pod(pod['async'])
- if result:
- pod = result
- else:
- continue
-
-
- if pod_title.startswith('Input') or not infobox_title:
- try:
- infobox_title = pod['subpods'][0]['plaintext']
- except:
- infobox_title = ''
- pass
-
- for subpod in pod['subpods']:
- if subpod['plaintext'] != '' and pod_title not in image_pods:
-
- if subpod['plaintext'] != '(requires interactivity)':
- result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})
-
- elif 'img' in subpod:
- result_chunks.append({'label': pod_title, 'image': subpod['img']})
-
- if not result_chunks:
- return []
-
- results.append({'infobox': infobox_title,
- 'attributes': result_chunks,
- 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})
-
- results.append({'url': resp.request.headers['Referer'],
- 'title': 'Wolfram|Alpha',
- 'content': infobox_title})
-
- return results
|