core.py 2.1 KB

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