wolframalpha_api.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 urllib import urlencode
  11. from lxml import etree
  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}),
  35. api_key=api_key)
  36. params['headers']['Referer'] = site_url.format(query=urlencode({'i': query}))
  37. return params
  38. # replace private user area characters to make text legible
  39. def replace_pua_chars(text):
  40. pua_chars = {u'\uf522': u'\u2192', # rigth arrow
  41. u'\uf7b1': u'\u2115', # set of natural numbers
  42. u'\uf7b4': u'\u211a', # set of rational numbers
  43. u'\uf7b5': u'\u211d', # set of real numbers
  44. u'\uf7bd': u'\u2124', # set of integer numbers
  45. u'\uf74c': 'd', # differential
  46. u'\uf74d': u'\u212f', # euler's number
  47. u'\uf74e': 'i', # imaginary number
  48. u'\uf7d9': '='} # equals sign
  49. for k, v in pua_chars.iteritems():
  50. text = text.replace(k, v)
  51. return text
  52. # get response from search-request
  53. def response(resp):
  54. results = []
  55. search_results = etree.XML(resp.content)
  56. # return empty array if there are no results
  57. if search_results.xpath(failure_xpath):
  58. return []
  59. try:
  60. infobox_title = search_results.xpath(input_xpath)[0].text
  61. except:
  62. infobox_title = ""
  63. pods = search_results.xpath(pods_xpath)
  64. result_chunks = []
  65. result_content = ""
  66. for pod in pods:
  67. pod_id = pod.xpath(pod_id_xpath)[0]
  68. pod_title = pod.xpath(pod_title_xpath)[0]
  69. pod_is_result = pod.xpath(pod_primary_xpath)
  70. subpods = pod.xpath(subpods_xpath)
  71. if not subpods:
  72. continue
  73. # Appends either a text or an image, depending on which one is more suitable
  74. for subpod in subpods:
  75. content = subpod.xpath(plaintext_xpath)[0].text
  76. image = subpod.xpath(image_xpath)
  77. if content and pod_id not in image_pods:
  78. if pod_is_result or not result_content:
  79. if pod_id != "Input":
  80. result_content = "%s: %s" % (pod_title, content)
  81. # if no input pod was found, title is first plaintext pod
  82. if not infobox_title:
  83. infobox_title = content
  84. content = replace_pua_chars(content)
  85. result_chunks.append({'label': pod_title, 'value': content})
  86. elif image:
  87. result_chunks.append({'label': pod_title,
  88. 'image': {'src': image[0].xpath(img_src_xpath)[0],
  89. 'alt': image[0].xpath(img_alt_xpath)[0]}})
  90. if not result_chunks:
  91. return []
  92. title = "Wolfram|Alpha (%s)" % infobox_title
  93. # append infobox
  94. results.append({'infobox': infobox_title,
  95. 'attributes': result_chunks,
  96. 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]})
  97. # append link to site
  98. results.append({'url': resp.request.headers['Referer'].decode('utf8'),
  99. 'title': title,
  100. 'content': result_content})
  101. return results