crossref.py 2.0 KB

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