core.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """CORE (science)
  4. """
  5. from datetime import datetime
  6. from urllib.parse import urlencode
  7. from searx.exceptions import SearxEngineAPIException
  8. about = {
  9. "website": 'https://core.ac.uk',
  10. "wikidata_id": 'Q22661180',
  11. "official_api_documentation": 'https://core.ac.uk/documentation/api/',
  12. "use_official_api": True,
  13. "require_api_key": True,
  14. "results": 'JSON',
  15. }
  16. categories = ['science', 'scientific publications']
  17. paging = True
  18. nb_per_page = 10
  19. api_key = 'unset'
  20. base_url = 'https://core.ac.uk:443/api-v2/search/'
  21. search_string = '{query}?page={page}&pageSize={nb_per_page}&apiKey={apikey}'
  22. def request(query, params):
  23. if api_key == 'unset':
  24. raise SearxEngineAPIException('missing CORE API key')
  25. search_path = search_string.format(
  26. query=urlencode({'q': query}),
  27. nb_per_page=nb_per_page,
  28. page=params['pageno'],
  29. apikey=api_key,
  30. )
  31. params['url'] = base_url + search_path
  32. return params
  33. def response(resp):
  34. results = []
  35. json_data = resp.json()
  36. for result in json_data['data']:
  37. source = result['_source']
  38. url = None
  39. if source.get('urls'):
  40. url = source['urls'][0].replace('http://', 'https://', 1)
  41. if url is None and source.get('doi'):
  42. # use the DOI reference
  43. url = 'https://doi.org/' + source['doi']
  44. if url is None and source.get('downloadUrl'):
  45. # use the downloadUrl
  46. url = source['downloadUrl']
  47. if url is None and source.get('identifiers'):
  48. # try to find an ark id, see
  49. # https://www.wikidata.org/wiki/Property:P8091
  50. # and https://en.wikipedia.org/wiki/Archival_Resource_Key
  51. arkids = [
  52. identifier[5:] # 5 is the length of "ark:/"
  53. for identifier in source.get('identifiers')
  54. if isinstance(identifier, str) and identifier.startswith('ark:/')
  55. ]
  56. if len(arkids) > 0:
  57. url = 'https://n2t.net/' + arkids[0]
  58. if url is None:
  59. continue
  60. publishedDate = None
  61. time = source['publishedDate'] or source['depositedDate']
  62. if time:
  63. publishedDate = datetime.fromtimestamp(time / 1000)
  64. # sometimes the 'title' is None / filter None values
  65. journals = [j['title'] for j in (source.get('journals') or []) if j['title']]
  66. publisher = source['publisher']
  67. if publisher:
  68. publisher = source['publisher'].strip("'")
  69. results.append(
  70. {
  71. 'template': 'paper.html',
  72. 'title': source['title'],
  73. 'url': url,
  74. 'content': source['description'] or '',
  75. # 'comments': '',
  76. 'tags': source['topics'],
  77. 'publishedDate': publishedDate,
  78. 'type': (source['types'] or [None])[0],
  79. 'authors': source['authors'],
  80. 'editor': ', '.join(source['contributors'] or []),
  81. 'publisher': publisher,
  82. 'journal': ', '.join(journals),
  83. # 'volume': '',
  84. # 'pages' : '',
  85. # 'number': '',
  86. 'doi': source['doi'],
  87. 'issn': [x for x in [source.get('issn')] if x],
  88. 'isbn': [x for x in [source.get('isbn')] if x], # exists in the rawRecordXml
  89. 'pdf_url': source.get('repositoryDocument', {}).get('pdfOrigin'),
  90. }
  91. )
  92. return results