microsoft_academic.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. {
  26. 'query': query,
  27. 'queryExpression': '',
  28. 'filters': [],
  29. 'orderBy': 0,
  30. 'skip': (params['pageno'] - 1) * 10,
  31. 'sortAscending': True,
  32. 'take': 10,
  33. 'includeCitationContexts': False,
  34. 'profileId': '',
  35. }
  36. )
  37. return params
  38. def response(resp):
  39. results = []
  40. response_data = loads(resp.text)
  41. if not response_data:
  42. return results
  43. for result in response_data.get('pr', {}):
  44. if 'dn' not in result['paper']:
  45. continue
  46. title = result['paper']['dn']
  47. content = _get_content(result['paper'])
  48. url = _paper_url.format(id=result['paper']['id'])
  49. results.append(
  50. {
  51. 'url': url,
  52. 'title': html_to_text(title),
  53. 'content': html_to_text(content),
  54. }
  55. )
  56. return results
  57. def _get_content(result):
  58. if 'd' in result:
  59. content = result['d']
  60. if len(content) > 300:
  61. return content[:300] + '...'
  62. return content
  63. return ''