logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

pubmed.py (3161B)


  1. #!/usr/bin/env python
  2. """
  3. PubMed (Scholar publications)
  4. @website https://www.ncbi.nlm.nih.gov/pubmed/
  5. @provide-api yes (https://www.ncbi.nlm.nih.gov/home/develop/api/)
  6. @using-api yes
  7. @results XML
  8. @stable yes
  9. @parse url, title, publishedDate, content
  10. More info on api: https://www.ncbi.nlm.nih.gov/books/NBK25501/
  11. """
  12. from flask_babel import gettext
  13. from lxml import etree
  14. from datetime import datetime
  15. from searx.url_utils import urlencode
  16. from searx.poolrequests import get
  17. categories = ['science']
  18. base_url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/esearch.fcgi'\
  19. + '?db=pubmed&{query}&retstart={offset}&retmax={hits}'
  20. # engine dependent config
  21. number_of_results = 10
  22. pubmed_url = 'https://www.ncbi.nlm.nih.gov/pubmed/'
  23. def request(query, params):
  24. # basic search
  25. offset = (params['pageno'] - 1) * number_of_results
  26. string_args = dict(query=urlencode({'term': query}),
  27. offset=offset,
  28. hits=number_of_results)
  29. params['url'] = base_url.format(**string_args)
  30. return params
  31. def response(resp):
  32. results = []
  33. # First retrieve notice of each result
  34. pubmed_retrieve_api_url = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?'\
  35. + 'db=pubmed&retmode=xml&id={pmids_string}'
  36. pmids_results = etree.XML(resp.content)
  37. pmids = pmids_results.xpath('//eSearchResult/IdList/Id')
  38. pmids_string = ''
  39. for item in pmids:
  40. pmids_string += item.text + ','
  41. retrieve_notice_args = dict(pmids_string=pmids_string)
  42. retrieve_url_encoded = pubmed_retrieve_api_url.format(**retrieve_notice_args)
  43. search_results_xml = get(retrieve_url_encoded).content
  44. search_results = etree.XML(search_results_xml).xpath('//PubmedArticleSet/PubmedArticle/MedlineCitation')
  45. for entry in search_results:
  46. title = entry.xpath('.//Article/ArticleTitle')[0].text
  47. pmid = entry.xpath('.//PMID')[0].text
  48. url = pubmed_url + pmid
  49. try:
  50. content = entry.xpath('.//Abstract/AbstractText')[0].text
  51. except:
  52. content = gettext('No abstract is available for this publication.')
  53. # If a doi is available, add it to the snipppet
  54. try:
  55. doi = entry.xpath('.//ELocationID[@EIdType="doi"]')[0].text
  56. content = 'DOI: {doi} Abstract: {content}'.format(doi=doi, content=content)
  57. except:
  58. pass
  59. if len(content) > 300:
  60. content = content[0:300] + "..."
  61. # TODO: center snippet on query term
  62. res_dict = {'url': url,
  63. 'title': title,
  64. 'content': content}
  65. try:
  66. publishedDate = datetime.strptime(entry.xpath('.//DateCreated/Year')[0].text
  67. + '-' + entry.xpath('.//DateCreated/Month')[0].text
  68. + '-' + entry.xpath('.//DateCreated/Day')[0].text, '%Y-%m-%d')
  69. res_dict['publishedDate'] = publishedDate
  70. except:
  71. pass
  72. results.append(res_dict)
  73. return results