wolframalpha_api.py 4.2 KB

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