wolframalpha_api.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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::title, "Input")]/subpod/plaintext'
  20. pods_xpath = '//pod'
  21. subpods_xpath = './subpod'
  22. pod_title_xpath = './@title'
  23. plaintext_xpath = './plaintext'
  24. image_xpath = './img'
  25. img_src_xpath = './@src'
  26. img_alt_xpath = './@alt'
  27. # pods to display as image in infobox
  28. # this pods do return a plaintext, but they look better and are more useful as images
  29. image_pods = {'Visual representation',
  30. 'Manipulatives illustration'}
  31. # do search-request
  32. def request(query, params):
  33. params['url'] = search_url.format(query=urlencode({'input': query}),
  34. 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',
  40. u'\uf7b1': u'\u2115',
  41. u'\uf7b4': u'\u211a',
  42. u'\uf7b5': u'\u211d',
  43. u'\uf7bd': u'\u2124',
  44. u'\uf74c': 'd',
  45. u'\uf74d': u'\u212f',
  46. u'\uf74e': 'i',
  47. u'\uf7d9': '='}
  48. for k, v in pua_chars.iteritems():
  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.content)
  55. # return empty array if there are no results
  56. if search_results.xpath(failure_xpath):
  57. return []
  58. infobox_title = search_results.xpath(input_xpath)
  59. if infobox_title:
  60. infobox_title = replace_pua_chars(infobox_title[0].text)
  61. pods = search_results.xpath(pods_xpath)
  62. result_chunks = []
  63. for pod in pods:
  64. pod_title = replace_pua_chars(pod.xpath(pod_title_xpath)[0])
  65. subpods = pod.xpath(subpods_xpath)
  66. if not subpods:
  67. continue
  68. for subpod in subpods:
  69. content = subpod.xpath(plaintext_xpath)[0].text
  70. image = subpod.xpath(image_xpath)
  71. if content and pod_title not in image_pods:
  72. content = replace_pua_chars(content)
  73. result_chunks.append({'label': pod_title, 'value': content})
  74. # if there's no input pod, infobox_title is content of first pod
  75. if not infobox_title:
  76. infobox_title = content
  77. elif image:
  78. result_chunks.append({'label': pod_title,
  79. 'image': {'src': image[0].xpath(img_src_xpath)[0],
  80. 'alt': image[0].xpath(img_alt_xpath)[0]}})
  81. if not result_chunks:
  82. return []
  83. results.append({'infobox': infobox_title,
  84. 'attributes': result_chunks,
  85. 'urls': [{'title': 'Wolfram|Alpha', 'url': resp.request.headers['Referer']}]})
  86. # append link to site
  87. results.append({'url': resp.request.headers['Referer'],
  88. 'title': 'Wolfram|Alpha',
  89. 'content': infobox_title})
  90. return results