core.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """CORE (science)
  4. """
  5. # pylint: disable=missing-function-docstring
  6. from json import loads
  7. from datetime import datetime
  8. from urllib.parse import urlencode
  9. from searx import logger
  10. from searx.exceptions import SearxEngineAPIException
  11. logger = logger.getChild('CORE engine')
  12. about = {
  13. "website": 'https://core.ac.uk',
  14. "wikidata_id": 'Q22661180',
  15. "official_api_documentation": 'https://core.ac.uk/documentation/api/',
  16. "use_official_api": True,
  17. "require_api_key": True,
  18. "results": 'JSON',
  19. }
  20. categories = ['science']
  21. paging = True
  22. nb_per_page = 10
  23. api_key = 'unset'
  24. logger = logger.getChild('CORE engine')
  25. base_url = 'https://core.ac.uk:443/api-v2/search/'
  26. search_string = '{query}?page={page}&pageSize={nb_per_page}&apiKey={apikey}'
  27. def request(query, params):
  28. if api_key == 'unset':
  29. raise SearxEngineAPIException('missing CORE API key')
  30. search_path = search_string.format(
  31. query = urlencode({'q': query}),
  32. nb_per_page = nb_per_page,
  33. page = params['pageno'],
  34. apikey = api_key,
  35. )
  36. params['url'] = base_url + search_path
  37. logger.debug("query_url --> %s", params['url'])
  38. return params
  39. def response(resp):
  40. results = []
  41. json_data = loads(resp.text)
  42. for result in json_data['data']:
  43. source = result['_source']
  44. time = source['publishedDate'] or source['depositedDate']
  45. if time :
  46. date = datetime.fromtimestamp(time / 1000)
  47. else:
  48. date = None
  49. metadata = []
  50. if source['publisher'] and len(source['publisher']) > 3:
  51. metadata.append(source['publisher'])
  52. if source['topics']:
  53. metadata.append(source['topics'][0])
  54. if source['doi']:
  55. metadata.append(source['doi'])
  56. metadata = ' / '.join(metadata)
  57. results.append({
  58. 'url': source['urls'][0].replace('http://', 'https://', 1),
  59. 'title': source['title'],
  60. 'content': source['description'],
  61. 'publishedDate': date,
  62. 'metadata' : metadata,
  63. })
  64. return results