crossref.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. # lint: pylint
  3. """Semantic Scholar (Science)
  4. """
  5. # pylint: disable=use-dict-literal
  6. from urllib.parse import urlencode
  7. from searx.utils import html_to_text
  8. about = {
  9. "website": 'https://www.crossref.org/',
  10. "wikidata_id": 'Q5188229',
  11. "official_api_documentation": 'https://github.com/CrossRef/rest-api-doc',
  12. "use_official_api": False,
  13. "require_api_key": False,
  14. "results": 'JSON',
  15. }
  16. categories = ['science', 'scientific publications']
  17. paging = True
  18. search_url = 'https://api.crossref.org/works'
  19. def request(query, params):
  20. params['url'] = search_url + '?' + urlencode(dict(query=query, offset=20 * (params['pageno'] - 1)))
  21. return params
  22. def response(resp):
  23. res = resp.json()
  24. results = []
  25. for record in res['message']['items']:
  26. record_type = record['type']
  27. if record_type == 'book-chapter':
  28. title = record['container-title'][0]
  29. if record['title'][0].lower().strip() != title.lower().strip():
  30. title = html_to_text(title) + ' (' + html_to_text(record['title'][0]) + ')'
  31. journal = None
  32. else:
  33. title = html_to_text(record['title'][0])
  34. journal = record.get('container-title', [None])[0]
  35. url = record.get('resource', {}).get('primary', {}).get('URL') or record['URL']
  36. authors = [author.get('given', '') + ' ' + author.get('family', '') for author in record.get('author', [])]
  37. isbn = record.get('isbn') or [i['value'] for i in record.get('isbn-type', [])]
  38. results.append(
  39. {
  40. 'template': 'paper.html',
  41. 'url': url,
  42. 'title': title,
  43. 'journal': journal,
  44. 'volume': record.get('volume'),
  45. 'type': record['type'],
  46. 'content': html_to_text(record.get('abstract', '')),
  47. 'publisher': record.get('publisher'),
  48. 'authors': authors,
  49. 'doi': record['DOI'],
  50. 'isbn': isbn,
  51. }
  52. )
  53. return results