microsoft_academic.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. Microsoft Academic (Science)
  4. """
  5. from json import dumps, loads
  6. from searx.utils import html_to_text
  7. # about
  8. about = {
  9. "website": 'https://academic.microsoft.com',
  10. "wikidata_id": 'Q28136779',
  11. "official_api_documentation": 'http://ma-graph.org/',
  12. "use_official_api": False,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. categories = ['images']
  17. paging = True
  18. search_url = 'https://academic.microsoft.com/api/search'
  19. _paper_url = 'https://academic.microsoft.com/paper/{id}/reference'
  20. def request(query, params):
  21. params['url'] = search_url
  22. params['method'] = 'POST'
  23. params['headers']['content-type'] = 'application/json; charset=utf-8'
  24. params['data'] = dumps({
  25. 'query': query,
  26. 'queryExpression': '',
  27. 'filters': [],
  28. 'orderBy': 0,
  29. 'skip': (params['pageno'] - 1) * 10,
  30. 'sortAscending': True,
  31. 'take': 10,
  32. 'includeCitationContexts': False,
  33. 'profileId': '',
  34. })
  35. return params
  36. def response(resp):
  37. results = []
  38. response_data = loads(resp.text)
  39. if not response_data:
  40. return results
  41. for result in response_data['pr']:
  42. if 'dn' not in result['paper']:
  43. continue
  44. title = result['paper']['dn']
  45. content = _get_content(result['paper'])
  46. url = _paper_url.format(id=result['paper']['id'])
  47. results.append({
  48. 'url': url,
  49. 'title': html_to_text(title),
  50. 'content': html_to_text(content),
  51. })
  52. return results
  53. def _get_content(result):
  54. if 'd' in result:
  55. content = result['d']
  56. if len(content) > 300:
  57. return content[:300] + '...'
  58. return content
  59. return ''