logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

wolframalpha_api.py (4198B)


  1. # Wolfram Alpha (Science)
  2. #
  3. # @website https://www.wolframalpha.com
  4. # @provide-api yes (https://api.wolframalpha.com/v2/)
  5. #
  6. # @using-api yes
  7. # @results XML
  8. # @stable yes
  9. # @parse url, infobox
  10. from lxml import etree
  11. from searx.url_utils import urlencode
  12. # search-url
  13. search_url = 'https://api.wolframalpha.com/v2/query?appid={api_key}&{query}'
  14. site_url = 'https://www.wolframalpha.com/input/?{query}'
  15. api_key = '' # defined in settings.yml
  16. # xpath variables
  17. failure_xpath = '/queryresult[attribute::success="false"]'
  18. input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'
  19. pods_xpath = '//pod'
  20. subpods_xpath = './subpod'
  21. pod_primary_xpath = './@primary'
  22. pod_id_xpath = './@id'
  23. pod_title_xpath = './@title'
  24. plaintext_xpath = './plaintext'
  25. image_xpath = './img'
  26. img_src_xpath = './@src'
  27. img_alt_xpath = './@alt'
  28. # pods to display as image in infobox
  29. # this pods do return a plaintext, but they look better and are more useful as images
  30. image_pods = {'VisualRepresentation',
  31. 'Illustration'}
  32. # do search-request
  33. def request(query, params):
  34. params['url'] = search_url.format(query=urlencode({'input': query}), api_key=api_key)
  35. params['headers']['Referer'] = site_url.format(query=urlencode({'i': query}))
  36. return params
  37. # replace private user area characters to make text legible
  38. def replace_pua_chars(text):
  39. pua_chars = {u'\uf522': u'\u2192', # rigth arrow
  40. u'\uf7b1': u'\u2115', # set of natural numbers
  41. u'\uf7b4': u'\u211a', # set of rational numbers
  42. u'\uf7b5': u'\u211d', # set of real numbers
  43. u'\uf7bd': u'\u2124', # set of integer numbers
  44. u'\uf74c': 'd', # differential
  45. u'\uf74d': u'\u212f', # euler's number
  46. u'\uf74e': 'i', # imaginary number
  47. u'\uf7d9': '='} # equals sign
  48. for k, v in pua_chars.items():
  49. text = text.replace(k, v)
  50. return text
  51. # get response from search-request
  52. def response(resp):
  53. results = []
  54. search_results = etree.XML(resp.text)
  55. # return empty array if there are no results
  56. if search_results.xpath(failure_xpath):
  57. return []
  58. try:
  59. infobox_title = search_results.xpath(input_xpath)[0].text
  60. except:
  61. infobox_title = ""
  62. pods = search_results.xpath(pods_xpath)
  63. result_chunks = []
  64. result_content = ""
  65. for pod in pods:
  66. pod_id = pod.xpath(pod_id_xpath)[0]
  67. pod_title = pod.xpath(pod_title_xpath)[0]
  68. pod_is_result = pod.xpath(pod_primary_xpath)
  69. subpods = pod.xpath(subpods_xpath)
  70. if not subpods:
  71. continue
  72. # Appends either a text or an image, depending on which one is more suitable
  73. for subpod in subpods:
  74. content = subpod.xpath(plaintext_xpath)[0].text
  75. image = subpod.xpath(image_xpath)
  76. if content and pod_id not in image_pods:
  77. if pod_is_result or not result_content:
  78. if pod_id != "Input":
  79. result_content = "%s: %s" % (pod_title, content)
  80. # if no input pod was found, title is first plaintext pod
  81. if not infobox_title:
  82. infobox_title = content
  83. content = replace_pua_chars(content)
  84. result_chunks.append({'label': pod_title, 'value': content})
  85. elif image:
  86. result_chunks.append({'label': pod_title,
  87. 'image': {'src': image[0].xpath(img_src_xpath)[0],
  88. 'alt': image[0].xpath(img_alt_xpath)[0]}})
  89. if not result_chunks:
  90. return []
  91. title = "Wolfram|Alpha (%s)" % infobox_title
  92. # append infobox
  93. results.append({'infobox': infobox_title,
  94. 'attributes': result_chunks,
  95. 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})
  96. # append link to site
  97. results.append({'url': resp.request.headers['Referer'],
  98. 'title': title,
  99. 'content': result_content})
  100. return results