arxiv.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # SPDX-License-Identifier: AGPL-3.0-or-later
  2. """
  3. ArXiV (Scientific preprints)
  4. """
  5. from lxml import html
  6. from datetime import datetime
  7. from searx.utils import eval_xpath_list, eval_xpath_getindex
  8. # about
  9. about = {
  10. "website": 'https://arxiv.org',
  11. "wikidata_id": 'Q118398',
  12. "official_api_documentation": 'https://arxiv.org/help/api',
  13. "use_official_api": True,
  14. "require_api_key": False,
  15. "results": 'XML-RSS',
  16. }
  17. categories = ['science']
  18. paging = True
  19. base_url = 'https://export.arxiv.org/api/query?search_query=all:'\
  20. + '{query}&start={offset}&max_results={number_of_results}'
  21. # engine dependent config
  22. number_of_results = 10
  23. def request(query, params):
  24. # basic search
  25. offset = (params['pageno'] - 1) * number_of_results
  26. string_args = dict(query=query,
  27. offset=offset,
  28. number_of_results=number_of_results)
  29. params['url'] = base_url.format(**string_args)
  30. return params
  31. def response(resp):
  32. results = []
  33. dom = html.fromstring(resp.content)
  34. for entry in eval_xpath_list(dom, '//entry'):
  35. title = eval_xpath_getindex(entry, './/title', 0).text
  36. url = eval_xpath_getindex(entry, './/id', 0).text
  37. content_string = '{doi_content}{abstract_content}'
  38. abstract = eval_xpath_getindex(entry, './/summary', 0).text
  39. # If a doi is available, add it to the snipppet
  40. doi_element = eval_xpath_getindex(entry, './/link[@title="doi"]', 0, default=None)
  41. doi_content = doi_element.text if doi_element is not None else ''
  42. content = content_string.format(doi_content=doi_content, abstract_content=abstract)
  43. if len(content) > 300:
  44. content = content[0:300] + "..."
  45. # TODO: center snippet on query term
  46. publishedDate = datetime.strptime(eval_xpath_getindex(entry, './/published', 0).text, '%Y-%m-%dT%H:%M:%SZ')
  47. res_dict = {'url': url,
  48. 'title': title,
  49. 'publishedDate': publishedDate,
  50. 'content': content}
  51. results.append(res_dict)
  52. return results