wolframalpha_noapi.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Wolfram|Alpha (Science)
  4. """
  5. from json import loads
  6. from time import time
  7. from urllib.parse import urlencode
  8. from searx.poolrequests import get as http_get
  9. # about
  10. about = {
  11. "website": 'https://www.wolframalpha.com/',
  12. "wikidata_id": 'Q207006',
  13. "official_api_documentation": 'https://products.wolframalpha.com/api/',
  14. "use_official_api": False,
  15. "require_api_key": False,
  16. "results": 'JSON',
  17. }
  18. # search-url
  19. url = 'https://www.wolframalpha.com/'
  20. search_url = url + 'input/json.jsp'\
  21. '?async=false'\
  22. '&banners=raw'\
  23. '&debuggingdata=false'\
  24. '&format=image,plaintext,imagemap,minput,moutput'\
  25. '&formattimeout=2'\
  26. '&{query}'\
  27. '&output=JSON'\
  28. '&parsetimeout=2'\
  29. '&proxycode={token}'\
  30. '&scantimeout=0.5'\
  31. '&sponsorcategories=true'\
  32. '&statemethod=deploybutton'
  33. referer_url = url + 'input/?{query}'
  34. token = {'value': '',
  35. 'last_updated': None}
  36. # pods to display as image in infobox
  37. # this pods do return a plaintext, but they look better and are more useful as images
  38. image_pods = {'VisualRepresentation',
  39. 'Illustration',
  40. 'Symbol'}
  41. # seems, wolframalpha resets its token in every hour
  42. def obtain_token():
  43. update_time = time() - (time() % 3600)
  44. try:
  45. token_response = http_get('https://www.wolframalpha.com/input/api/v1/code?ts=9999999999999999999', timeout=2.0)
  46. token['value'] = loads(token_response.text)['code']
  47. token['last_updated'] = update_time
  48. except:
  49. pass
  50. return token
  51. def init(engine_settings=None):
  52. obtain_token()
  53. # do search-request
  54. def request(query, params):
  55. # obtain token if last update was more than an hour
  56. if time() - (token['last_updated'] or 0) > 3600:
  57. obtain_token()
  58. params['url'] = search_url.format(query=urlencode({'input': query}), token=token['value'])
  59. params['headers']['Referer'] = referer_url.format(query=urlencode({'i': query}))
  60. return params
  61. # get response from search-request
  62. def response(resp):
  63. results = []
  64. resp_json = loads(resp.text)
  65. if not resp_json['queryresult']['success']:
  66. return []
  67. # TODO handle resp_json['queryresult']['assumptions']
  68. result_chunks = []
  69. infobox_title = ""
  70. result_content = ""
  71. for pod in resp_json['queryresult']['pods']:
  72. pod_id = pod.get('id', '')
  73. pod_title = pod.get('title', '')
  74. pod_is_result = pod.get('primary', None)
  75. if 'subpods' not in pod:
  76. continue
  77. if pod_id == 'Input' or not infobox_title:
  78. infobox_title = pod['subpods'][0]['plaintext']
  79. for subpod in pod['subpods']:
  80. if subpod['plaintext'] != '' and pod_id not in image_pods:
  81. # append unless it's not an actual answer
  82. if subpod['plaintext'] != '(requires interactivity)':
  83. result_chunks.append({'label': pod_title, 'value': subpod['plaintext']})
  84. if pod_is_result or not result_content:
  85. if pod_id != "Input":
  86. result_content = pod_title + ': ' + subpod['plaintext']
  87. elif 'img' in subpod:
  88. result_chunks.append({'label': pod_title, 'image': subpod['img']})
  89. if not result_chunks:
  90. return []
  91. results.append({'infobox': infobox_title,
  92. 'attributes': result_chunks,
  93. 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})
  94. results.append({'url': resp.request.headers['Referer'],
  95. 'title': 'Wolfram|Alpha (' + infobox_title + ')',
  96. 'content': result_content})
  97. return results