scanr_structures.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. ScanR Structures (Science)
  4. """
  5. from json import loads, dumps
  6. from searx.utils import html_to_text
  7. # about
  8. about = {
  9. "website": 'https://scanr.enseignementsup-recherche.gouv.fr',
  10. "wikidata_id": 'Q44105684',
  11. "official_api_documentation": 'https://scanr.enseignementsup-recherche.gouv.fr/opendata',
  12. "use_official_api": True,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. # engine dependent config
  17. categories = ['science']
  18. paging = True
  19. page_size = 20
  20. # search-url
  21. url = 'https://scanr.enseignementsup-recherche.gouv.fr/'
  22. search_url = url + 'api/structures/search'
  23. # do search-request
  24. def request(query, params):
  25. params['url'] = search_url
  26. params['method'] = 'POST'
  27. params['headers']['Content-type'] = "application/json"
  28. params['data'] = dumps(
  29. {
  30. "query": query,
  31. "searchField": "ALL",
  32. "sortDirection": "ASC",
  33. "sortOrder": "RELEVANCY",
  34. "page": params['pageno'],
  35. "pageSize": page_size,
  36. }
  37. )
  38. return params
  39. # get response from search-request
  40. def response(resp):
  41. results = []
  42. search_res = loads(resp.text)
  43. # return empty array if there are no results
  44. if search_res.get('total', 0) < 1:
  45. return []
  46. # parse results
  47. for result in search_res['results']:
  48. if 'id' not in result:
  49. continue
  50. # is it thumbnail or img_src??
  51. thumbnail = None
  52. if 'logo' in result:
  53. thumbnail = result['logo']
  54. if thumbnail[0] == '/':
  55. thumbnail = url + thumbnail
  56. content = None
  57. if 'highlights' in result:
  58. content = result['highlights'][0]['value']
  59. # append result
  60. results.append(
  61. {
  62. 'url': url + 'structure/' + result['id'],
  63. 'title': result['label'],
  64. 'thumbnail': thumbnail,
  65. 'content': html_to_text(content),
  66. }
  67. )
  68. # return results
  69. return results