core.py 3.5 KB

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