wolframalpha_api.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. answer_xpath = '//pod[attribute::primary="true"]/subpod/plaintext'
  19. input_xpath = '//pod[starts-with(attribute::id, "Input")]/subpod/plaintext'
  20. pods_xpath = '//pod'
  21. subpods_xpath = './subpod'
  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 = None
  63. pods = search_results.xpath(pods_xpath)
  64. result_chunks = []
  65. for pod in pods:
  66. pod_id = pod.xpath(pod_id_xpath)[0]
  67. pod_title = pod.xpath(pod_title_xpath)[0]
  68. subpods = pod.xpath(subpods_xpath)
  69. if not subpods:
  70. continue
  71. # Appends either a text or an image, depending on which one is more suitable
  72. for subpod in subpods:
  73. content = subpod.xpath(plaintext_xpath)[0].text
  74. image = subpod.xpath(image_xpath)
  75. if content and pod_id not in image_pods:
  76. # if no input pod was found, title is first plaintext pod
  77. if not infobox_title:
  78. infobox_title = content
  79. content = replace_pua_chars(content)
  80. result_chunks.append({'label': pod_title, 'value': content})
  81. elif image:
  82. result_chunks.append({'label': pod_title,
  83. 'image': {'src': image[0].xpath(img_src_xpath)[0],
  84. 'alt': image[0].xpath(img_alt_xpath)[0]}})
  85. if not result_chunks:
  86. return []
  87. # append infobox
  88. results.append({'infobox': infobox_title,
  89. 'attributes': result_chunks,
  90. 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})
  91. # append link to site
  92. results.append({'url': resp.request.headers['Referer'],
  93. 'title': 'Wolfram|Alpha',
  94. 'content': infobox_title})
  95. return results