wolframalpha_api.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_primary_xpath = './@primary'
  23. pod_id_xpath = './@id'
  24. pod_title_xpath = './@title'
  25. plaintext_xpath = './plaintext'
  26. image_xpath = './img'
  27. img_src_xpath = './@src'
  28. img_alt_xpath = './@alt'
  29. # pods to display as image in infobox
  30. # this pods do return a plaintext, but they look better and are more useful as images
  31. image_pods = {'VisualRepresentation',
  32. 'Illustration'}
  33. # do search-request
  34. def request(query, params):
  35. params['url'] = search_url.format(query=urlencode({'input': query}),
  36. api_key=api_key)
  37. params['headers']['Referer'] = site_url.format(query=urlencode({'i': query}))
  38. return params
  39. # replace private user area characters to make text legible
  40. def replace_pua_chars(text):
  41. pua_chars = {u'\uf522': u'\u2192', # rigth arrow
  42. u'\uf7b1': u'\u2115', # set of natural numbers
  43. u'\uf7b4': u'\u211a', # set of rational numbers
  44. u'\uf7b5': u'\u211d', # set of real numbers
  45. u'\uf7bd': u'\u2124', # set of integer numbers
  46. u'\uf74c': 'd', # differential
  47. u'\uf74d': u'\u212f', # euler's number
  48. u'\uf74e': 'i', # imaginary number
  49. u'\uf7d9': '='} # equals sign
  50. for k, v in pua_chars.iteritems():
  51. text = text.replace(k, v)
  52. return text
  53. # get response from search-request
  54. def response(resp):
  55. results = []
  56. search_results = etree.XML(resp.content)
  57. # return empty array if there are no results
  58. if search_results.xpath(failure_xpath):
  59. return []
  60. try:
  61. infobox_title = search_results.xpath(input_xpath)[0].text
  62. except:
  63. infobox_title = None
  64. pods = search_results.xpath(pods_xpath)
  65. result = ""
  66. result_chunks = []
  67. for pod in pods:
  68. pod_id = pod.xpath(pod_id_xpath)[0]
  69. pod_title = pod.xpath(pod_title_xpath)[0]
  70. pod_is_result = pod.xpath(pod_primary_xpath)
  71. subpods = pod.xpath(subpods_xpath)
  72. if not subpods:
  73. continue
  74. # Appends either a text or an image, depending on which one is more suitable
  75. for subpod in subpods:
  76. content = subpod.xpath(plaintext_xpath)[0].text
  77. image = subpod.xpath(image_xpath)
  78. if content and pod_id not in image_pods:
  79. if pod_is_result:
  80. result = 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. # append infobox
  93. results.append({'infobox': infobox_title,
  94. 'attributes': result_chunks,
  95. 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer'].decode('utf8')}]})
  96. # append link to site
  97. results.append({'url': resp.request.headers['Referer'].decode('utf8'),
  98. 'title': infobox_title + ' - Wolfram|Alpha',
  99. 'content': result})
  100. return results