astrophysics_data_system.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """.. sidebar:: info
  3. The Astrophysics Data System (ADS) is a digital library portal for researchers in astronomy and physics,
  4. operated by the Smithsonian Astrophysical Observatory (SAO) under a NASA grant.
  5. The engine is adapted from the solr engine.
  6. """
  7. # pylint: disable=global-statement
  8. from datetime import datetime
  9. from json import loads
  10. from urllib.parse import urlencode
  11. from searx.exceptions import SearxEngineAPIException
  12. about = {
  13. "website": 'https://ui.adsabs.harvard.edu/',
  14. "wikidata_id": 'Q752099',
  15. "official_api_documentation": 'https://ui.adsabs.harvard.edu/help/api/api-docs.html',
  16. "use_official_api": True,
  17. "require_api_key": True,
  18. "results": 'JSON',
  19. }
  20. base_url = 'https://api.adsabs.harvard.edu/v1/search'
  21. result_base_url = 'https://ui.adsabs.harvard.edu/abs/'
  22. rows = 10
  23. sort = '' # sorting: asc or desc
  24. field_list = ['bibcode', 'author', 'title', 'abstract', 'doi', 'date'] # list of field names to display on the UI
  25. default_fields = '' # default field to query
  26. query_fields = '' # query fields
  27. paging = True
  28. api_key = 'unset'
  29. def init(_):
  30. if api_key == 'unset':
  31. raise SearxEngineAPIException('missing ADS API key')
  32. def request(query, params):
  33. query_params = {'q': query, 'rows': rows}
  34. if field_list:
  35. query_params['fl'] = ','.join(field_list)
  36. if query_fields:
  37. query_params['qf'] = ','.join(query_fields)
  38. if default_fields:
  39. query_params['df'] = default_fields
  40. if sort:
  41. query_params['sort'] = sort
  42. query_params['start'] = rows * (params['pageno'] - 1)
  43. params['headers']['Authorization'] = f'Bearer {api_key}'
  44. params['url'] = f"{base_url}/query?{urlencode(query_params)}"
  45. return params
  46. def response(resp):
  47. try:
  48. resp_json = loads(resp.text)
  49. except Exception as e:
  50. raise SearxEngineAPIException("failed to parse response") from e
  51. if 'error' in resp_json:
  52. raise SearxEngineAPIException(resp_json['error']['msg'])
  53. resp_json = resp_json["response"]
  54. result_len = resp_json["numFound"]
  55. results = []
  56. for res in resp_json["docs"]:
  57. author = res.get("author")
  58. if author:
  59. author = author[0] + ' et al.'
  60. results.append(
  61. {
  62. 'url': result_base_url + res.get("bibcode") + "/",
  63. 'title': res.get("title")[0],
  64. 'author': author,
  65. 'content': res.get("abstract"),
  66. 'doi': res.get("doi"),
  67. 'publishedDate': datetime.fromisoformat(res.get("date")),
  68. }
  69. )
  70. results.append({'number_of_results': result_len})
  71. return results